why time revalidate need request twice to get the fresh data? #69685
-
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
This is the popular cache invalidation behavior called stale-while-revalidate, see https://datatracker.ietf.org/doc/html/rfc5861#section-3. It's a design choice made by the Next.js team. If it doesn't fit your scenario then you'll have to find a third-party solution |
Beta Was this translation helpful? Give feedback.
-
|
If you don't want the stale-while-revalidate behaviour, you can use import { unstable_cache } from "next/cache";
async function getData() {
const revalidate = 10;
// This `key` will change every 10 seconds
const key = Math.floor(new Date().valueOf() / (1000 * revalidate)).toString();
return unstable_cache(async () => {
// await db.select().from(...)
await new Promise((resolve) => setTimeout(resolve, 2000));
return new Date().toISOString();
}, [key])();
}
export default async function Page() {
const data = await getData();
return <div>{data}</div>;
}
// Make the page dynamic, so that getData() always get called.
// Only the getData() gets called every request, the expensive data query is cached.
export const revalidate = 0; |
Beta Was this translation helpful? Give feedback.
-
|
Seems like I have the same issue with revlidate in Next.js 15, I tried to change stale time from 300s to 60s, but it doesn't help. |
Beta Was this translation helpful? Give feedback.

This is the popular cache invalidation behavior called stale-while-revalidate, see https://datatracker.ietf.org/doc/html/rfc5861#section-3.
It's a design choice made by the Next.js team. If it doesn't fit your scenario then you'll have to find a third-party solution