Next.js Discord

Discord Forum

nextjs making fetch request regardless of revalidate

Unanswered
Sage Thrasher posted this in #help-forum
Open in Discord
Sage ThrasherOP
For some reason, this api request to /guilds/featured keeps getting made seconds apart from each other though theres a 300 second revalidate on it? This is also happening to multiple other api endpoints. it's an external api and this function is called from a server file

import 'server-only';

import { APIKey } from "@/lib/data/APIKey";
import type { GuildResultData, PaginationOptions } from "@/typing";

export async function fetchFeaturedGuilds(options?: PaginationOptions<'sample'>): Promise<GuildResultData[]> {
    const url = new URL(`${process.env.API_ENDPOINT}/guilds/featured`);
    if (options?.limit) url.searchParams.set('limit', options.limit.toString());
    if (options?.offset) url.searchParams.set('offset', options.offset.toString());
    if (options?.sample) url.searchParams.set('sample', options.sample.toString());

    const response = await fetch(url.toString(), {
        headers: APIKey,
        next: {
            revalidate: 300
        }
    });
    
    if (!response.ok) {
        return [];
    }

    const data = await response.json();
    if (typeof data === 'undefined' || !Array.isArray(data)) return [];

    return data;
}

1 Reply

Sage ThrasherOP
bump