Next.js Discord

Discord Forum

Fetch API caching behave different for next dev and next build & next start

Unanswered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
const SHOP_API_REVALIDATE_TIME = 300; // 5 minutes

export async function getShopProductCatalog({
  shopId,
  localeCode,
  pageNumber = 1,
  pageSize = 20,
  search = '',
  tags,
  accessToken,
}: {
  readonly shopId: string;
  readonly localeCode: string;
  readonly pageNumber?: number;
  readonly pageSize?: number;
  readonly search?: string;
  readonly tags?: ReadonlyArray<string>;
  readonly accessToken?: string;
}): Promise<IProductCatalog> {
  const searchParams = new URLSearchParams([
    ['locale', localeCode],
    ['page', '' + pageNumber],
    ['perPage', '' + pageSize],
  ]);

  if (search?.trim()?.length > 2) {
    searchParams.append('search', search.trim());
  }

  tags?.forEach(tag => searchParams.append('tags', tag));

  const response = await fetch(
    `${SHOPS_API_BASE_URL}/${shopId}/products/?${searchParams.toString()}`,
    {
      cache: 'force-cache',
      next: { revalidate: SHOP_API_REVALIDATE_TIME },
      headers: {
        Authorization: accessToken ? `Bearer ${accessToken}` : '',
      },
    },
  );

  const productCatalog = await handleApiResponse<IProductCatalogApi>(response);
  return parseProductCatalog(productCatalog);
}


I'm retrieving product catalog from dynamic page, because it's depends on multiple parameters you can also see it in my code above.

Issue is when I run it on developement mode, and turn on logging from next config, I can see that my requests hits the data cache and I'm getting data from there.

When I build and start server, I wrap my fetch request with console.time to mesure time that it spend to get data, and it's always close to same amout, but my expectations are to get it faster starting from second request as long as it's gonna be cached.

Either I understand something wrong about caching in nextJS or idk maybe miss configuration

Relevant Packages:
next: 15.3.0 // Latest available version is detected (15.3.0).
react: 19.0.0
react-dom: 19.0.0
typescript: 5.6.3

0 Replies