Next.js Discord

Discord Forum

Vercel won't clear build cache, only works if I delete the project and re-create it

Answered
୧ʕ•̀ᴥ•́ʔ୨ posted this in #help-forum
Open in Discord
Avatar
I have a Next.js project which statically generates some pages (content is fetched), but on rebuild (e.g. via webhook or manually), it doesn't clear the .next cache, so it never updates. Even when I manually redeploy and make sure not to check the "use existing build cache" checkbox, it still reuses it. I also tried the VERCEL_FORCE_NO_BUILD_CACHE=1 env variable and adding forceNew=1 to the http endpoint that triggers the redeployment, still reuses the cache.

If I delete the entire project and create a new one from the same repository, it correctly rebuilds it.

Any ideas how to force the cache to be cleared on rebuild?
Image
Answered by ୧ʕ•̀ᴥ•́ʔ୨
I found the issue, it was due to accessing searchParams on a statically generated page
View full answer

47 Replies

Avatar
there is a data cache on the setting page, could you try purge it from there?
Image
Avatar
That works, but ideally I want to be able to do that automatically, e.g. when I rebuild via webhook when content is updated (e.g. triggered from a CMS)
Avatar
you could create a webhook to revalidate the data for it
there is an example from doc, let me try to find it
you could add a key in to query and only revalidate with correct key provided
Avatar
Thanks, I'll try that. It does seem like a bug though that it's not possible to clear the cache with the usual methods.
I also just tried adding forceNew=1 to the v13/deployments http endpoint that triggers the redeployment (as per the docs here https://vercel.com/docs/deployments/troubleshoot-a-build#managing-build-cache), that didn't work either
Avatar
well its for build cache
not data cache
they are different
Avatar
Ah I see, I assumed it's the same thing, I guess not
Avatar
well but it should refetch the data and update the cache when you rebuild
Avatar
So I added a tag to my fetch
res = await fetch(path, { cache: "force-cache", next: { tags: ["test"] } });

And I have this in a route.ts:
import { NextRequest, NextResponse } from "next/server";
import { revalidateTag } from "next/cache";

export async function GET(request: NextRequest) {
    revalidateTag("test");
    return NextResponse.json({ revalidated: true, now: Date.now() });
}

I manually run the route handler in the browser and then trigger a rebuild, bit it doesn't refetch 🙁
Avatar
have you refresh the page?
Avatar
Yep! The page is statically generated with generateStaticParams() if that makes any difference, that's why I'm doing a full rebuild. I tried adding revalidateTag("test") at the top of generateStaticParams, but it still doesn't work
It works on my local machine actually, when I build the project, just not on Vercel Nevermind, doesn't work
Avatar
you shouldn't call revalidateTag("test") in there
Image
Avatar
So it actually seems to work on my local machine after more tests
If I do npm run build / npm run start it updates after every rebuild
Avatar
yes on every build it will refetch the data
revalidateTag/revalidatePath is for updating the data cache without rebuilding the site
Avatar
Well my issue is on Vercel, the data doesn't get updated even after a full rebuild
Avatar
can you share a github repo?
Avatar
It's private but I can set up a demo and share that
Avatar
yeah please do that so I can test on my side
Avatar
I found the issue, it was due to accessing searchParams on a statically generated page
Answer
Avatar
Although it did work at one point, doesn't seem to anymore
In the past I was able to force the page to be server rendered dynamically by adding a search param to the URL
But now this breaks the static generation somehow, it no longer updates, while the page with the param is still dynamic
I wanted to have a page that is static, but to force a dynamic preview by adding ?preview=true at the end
Avatar
when your page use the searchParams object, the page cannot be static generated
Avatar
It seems like with draft mode I can't have the same-ish URL as the original page
I wanted to have mysite.com/mypage/ be the static page and mysite.com/mypage?preview be the preview page
I wonder if I can make another route like /mypage/preview, maybe that would work, as I'll only generate /mypage/ statically, and mypage/preview is a different route, so can be dynamic
Avatar
yeah you could try it
just don't use searchParams on the page you want to be static generated
Image
Avatar
Anyway, thanks for your help 🙂
Avatar
np hope you get it fixed soon
Avatar
I hope Next start supporting using searchParams on static pages (and force them to be dynamic), it kind of works already
I could just erase the project and recreate it via the vercel http API each time I want to rebuild, and it would work
The fact that the content doesn't update seems like a bug they haven't looked into since they don't officially support the behavior
Avatar
well it hard as the doc said searchParams can be any object