I Can't Disable Vercel Data Cache? #10117
-
Problem DescriptionNote: this is a repost of this discussion since there are more people here in vercel/next.js than there is in vercel/vercel Im trying to get around its “automatic caching” system since its giving me stale data every time. I’ve set cache: no-store and no-cache and that didnt work I've added this at the top lvl of the module & in my specific route: export const fetchCache = 'force-no-store'; I've done the following for my specific fetch: const res = await fetch('api/example', { cache: 'no-store' });
const res = await fetch('api/example', { cache: 'no-cache' });
const res = await fetch('api/example', { next: { revalidate: 0 });
const res = await fetch('api/example', { cache: 'no-store', next: { revalidate: 0 });
const res = await fetch('api/example', { cache: 'no-cache', next: { revalidate: 0 }); Checked their docs for Vercel Caching and did the following for my backend: let cacheControl = 'public, s-maxage=1000, stale-while-revalidate=500'
if (toCache.has(slug[0])) cacheControl = 'public, s-maxage=3600, stale-while-revalidate=1800'
return new Response(stringifyResult, {
status: 200,
headers: {
'Content-Type': 'application/json',
'Cache-Control': cacheControl,
'Vercel-CDN-Cache-Control': 's-maxage=1000',
'CDN-Cache-Control': 's-maxage=1000'
}
}) Even though I've set it to simply a 1000s, I'd get stale data still by the next day. Set to 0 to see if it'll do anything: still the same result I've also added the following vercel.json file: {
"headers": [
{
"source": "/api/INSERT_ROUTE/route.ts",
"headers": [
{
"key": "Cache-Control",
"value": "public, s-maxage=3600, stale-while-revalidate=1800"
}
]
},
{
"source": "/api/INSERT_ROUTE2/route.ts",
"headers": [
{
"key": "Cache-Control",
"value": "public, s-maxage=3600, stale-while-revalidate=1800"
}
]
}
]
} Im still getting stale data Ive purged the vercel data cache (which worked temporarily as expected) but i’d get stale data the next day or whenever new data is supposed to come |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments 8 replies
-
It seems like you're experiencing issues with caching and getting stale data in your Next.js application deployed on Vercel. Caching can be a complex topic, but I'll try to provide some guidance to help you troubleshoot the issue. cache and next options in fetch: Cache-Control headers: 'Cache-Control': 'public, no-cache' Additionally, make sure that your backend responses are being cached correctly. You can verify this by checking the Cache-Control header in the network tab of your browser's developer tools. Vercel configuration: Purging the cache: Client-side caching: |
Beta Was this translation helpful? Give feedback.
-
After starting from scratch and making sure that there are no other variables, I've re-implemented the "solutions" to my problem. // i.e.
const res = await fetch('api/example', { cache: 'no-store' });
const res = await fetch('api/example', { cache: 'no-cache' });
const res = await fetch('api/example', { next: { revalidate: 0 });
const res = await fetch('api/example', { cache: 'no-store', next: { revalidate: 0 });
const res = await fetch('api/example', { cache: 'no-cache', next: { revalidate: 0 }); I found that
However, this doesn't opt out of fetch caching. I'm not sure if I'm just doing something wrong or the documentation is, but all I know that is the solution provided didn't work. Does anyone have this problem? |
Beta Was this translation helpful? Give feedback.
-
I tried every option under the sun and the only way I managed to get fresh data is to: Example:
import { NextResponse } from 'next/server'
export async function GET() {
const someData = { "reasonsICryMyselfToSleepAtNight": ["caching"] }
return NextResponse.json(someData)
}
export const revalidate = 0; |
Beta Was this translation helpful? Give feedback.
-
What worked for me so far is "forcing" the revalidation of the route with an utility function like this : import { revalidatePath } from "next/cache";
import { NextRequest } from "next/server";
export const forceRevalidate = (request: NextRequest) => {
const path = request.nextUrl.searchParams.get("path") || "/";
revalidatePath(path);
}; And then calling it first in the route handlers : export const GET = async (request: NextRequest) => {
forceRevalidate(request);
// YOUR-CODE
} |
Beta Was this translation helpful? Give feedback.
-
Caching by opt-out instead of opt-in is like jumping out of an airplane without parachute "by default" and hoping to not miss the one you threw out earlier on your way down. So from now on we can use next only for small hobbyist pages outside of business context. Everything else is too dangerous because of data loss when the user works on stale data and saves that or tries to save and may not because of OCC. There needs to be exactly 1 masterswitch to configure caching as opt-in by default. Honestly no experienced professional can take next seriously anymore until this is implemented. |
Beta Was this translation helpful? Give feedback.
-
This is awful. |
Beta Was this translation helpful? Give feedback.
-
If you are trying to opt-out of caching inside of Next.js (and thus the Vercel Data Cache when deployed to Vercel), you can use the route segment configuration options. export const fetchCache = 'force-no-store' Here's the documentation which is accurate. However, it sounds like there is some confusion about the different layers of caching in Next.js. We know this isn't as clear as it could be right now and we're working to improve this in the documentation. Please see this PR for more details: vercel/next.js#52514 |
Beta Was this translation helpful? Give feedback.
-
After trying many solutions without any effect, none of them worked. Then I added a timestamp at the end of the request, and it worked.
|
Beta Was this translation helpful? Give feedback.
-
I just lost half a day to this as well. I've been using the latest Next.js with the Neon database and adapter. I was banging my head against the wall trying to figure out why the database was saying one thing but the Next.js app was returning something completely different. I don't think this kind of thing should be enabled by default. |
Beta Was this translation helpful? Give feedback.
-
never have this problem before, but when you do, its definitely worse than installing windows 11. |
Beta Was this translation helpful? Give feedback.
-
Caching has also been a headache for me on my latest project (ic-768/next-blogs). Specifically when hosting on vercel. The issue is when I try to create a new blog on my server, then navigate to the /dashboard route to see a list of all blogs (which should be updated with the new entry).
I've tried all of the 'opt-out' mechanisms, route segment config, etc. Nothing seems to work. |
Beta Was this translation helpful? Give feedback.
If you are trying to opt-out of caching inside of Next.js (and thus the Vercel Data Cache when deployed to Vercel), you can use the route segment configuration options.
Here's the documentation which is accurate. However, it sounds like there is some confusion about the different layers of caching in Next.js.
We know this isn't as clear as it could be right now and we're working to improve this in the documentation.
Please see this PR for more details: vercel/next.js#52514