最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to handle API changes with Next.js revalidate when timing is unpredictable? - Stack Overflow

programmeradmin4浏览0评论

I'm working on a Next.js project that uses an external API for a marketplace feature. The marketplace automatically closes at a specific time, and when it does, the API response changes from: { success: true } → { success: false }

To improve performance, I set revalidate: 600 in my fetch to cache the response for 10 minutes. However, after the marketplace closes, the API response remains { success: true }, likely due to the cache.

From what I’ve found, possible solutions include disabling caching or using On-Demand Revalidation. But since the API doesn't provide any way to know when the marketplace will close, I can’t proactively trigger revalidation.

Is there a better approach to handle this situation?

Update

To provide more context, here’s the TypeScript code for fetching the marketplace status:

const getMarketspace = async ({
  shopId,
}: {
  shopId: string;
}): Promise<APIResponse> => {
  const res = await fetch("/v/layouts", {
    method: "POST",
    headers: {
      ...defaultHeaders,
    },
    body: JSON.stringify({
      shopId,
    }),
    next: {
       revalidate: 600,
    }
  });
  // ... Other Code
}

For example, if the marketplace closes at February 17, 2025, 23:59, and I make a request at February 18, 00:00, the API correctly responds with:

{ "success": false }

However, even if I call the same fetch hours later, such as at February 18, 12:00, the response is still:

{ "success": true }

This happens even though the cache should have expired long ago based on the revalidate: 600 setting.

发布评论

评论列表(0)

  1. 暂无评论