Next.js Discord

Discord Forum

revalidatePath doesn't work in production

Unanswered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
I have a server action that does revalidatePath() at the end for two different routes.
Both routes contain server components.
One of them gets rendered as static HTML at build time.
The other one is dynamic.

Locally everything works fine (npm run dev and npm run build && npm run start).
But after deploying to Vercel the revalidatePath() for the (above mentioned) static path doesn't work anymore:
The newly added items don't get added to the page.

But revalidatePath() is actually meant to work on static routes as well, right?
Any ideas why the revalidatePath() for my static HTML route doesn't work after deployment?

85 Replies

Spectacled bearOP
When I make the route dynamic by adding export const dynamic = 'force-dynamic' to the respective page.tsx, revalidatePath() in my server action suddenly works for that route too.
@Ray were you running `revalidatePath()` in a GET handler?
Spectacled bearOP
No in a server action.

That's what it looks like:
export async function createAd(formPath, prevState, formData){
    let newAd;
    // ...

    try {
        newAd = await Ad.create({
            // ...
        })
    } catch (error) {
        // ...
    }

    revalidatePath(`/anzeigen`)
    revalidatePath(`/anzeigen/${category}`)
    redirect(`/anzeigen/${category}/${newAd._id}`)

    return newState
}


The
revalidatePath(`/anzeigen`)
is the static route and doesn't get updated after deploying to vercel.
Spectacled bearOP
Of course it should.😅
That's why it's so frustrating.😖
Especially because the dynamic route works fine:
 revalidatePath(`/anzeigen/${category}`)
it does update for me
@Ray it does update for me
Spectacled bearOP
locally it does also update for me.
just not on vercel.
it does update on vercel
Spectacled bearOP
and your updating route is static?
yes
try it
Spectacled bearOP
on /update nothing happens for me
refresh again
@Ray refresh again
Spectacled bearOP
when I click update on /update nothing happens.
also not after i refresh the page
because I didn't call redirect there, just revalidatePath
/update is running revalidatePath("/")
@Ray Click to see attachment
Spectacled bearOP
but this does only mean that the the cache gets updated as soon as the page gets visited the next time - not only if it's the following page, right?
/ revalidate right after you press update
@Ray `/` revalidate right after you press update
Spectacled bearOP
makes sense.
this works for me too.
I expected something popping up on /update
and if you press update on /update, "/" only revalidate on next visit
@Spectacled bear makes sense. this works for me too. I expected something popping up on /update
it would if I do
revalidatePath("/")
redirect("/")
Spectacled bearOP
@Ray check the log on vercel if there is any error
Spectacled bearOP
only 401 HTTPs occasionally.
Doesn't seem to be related to this.
@Ray I don't understand the word, can't test it <:lol:897094737801150496>
Spectacled bearOP
when you click the button in the upper right corner to create an ad.
then you choose a category (and remember it), a title, description + fake email.
click the button below the form.

go to the home page, click the category title:
you'll see the created ad.

go to home page, click the 'Alle Kleinanzeigen' button:
there you should find all ads.
But the new one isn't there
however, when i redeploy the site it fetches all data from the DB correctly and displays it in the categories and in the 'all ads' page.
did you redeploy?
it works
there is a 5min router cache for static page so if you navigate to it, it might not show updated data
but if you refresh, it should show updated data
@Ray did you redeploy?
Spectacled bearOP
no.
then it seems to work for you.
just not for me😅
refresh the page still doesnt work?
i saw you added a record
Spectacled bearOP
it doesn't work when I refresh the page. only when I close the page and open it in a new window again.
then i see the updated records
refresh twice?
what browser are you using lol
Spectacled bearOP
Firefox and Chrome.
Same thing with both.
Now it works when I refresh the page (in Chrome) - not when I visit it, but only after I visit and the refresh.
@Ray there is a 5min router cache for static page so if you navigate to it, it might not show updated data
yeah when you navigate by the link, you may hit the router cache
and it is 5min for static page
Spectacled bearOP
what do you mean?
for example, after you create the record, if you press back button and press Alle Kleinanzeigen
Spectacled bearOP
then i don't see the new record.
yes you will have to wait 5min
Spectacled bearOP
only when I do this + refreshing the page
because of router cache
after 5min, if you navigate to it again
it should show new data
unless you refresh the page
Spectacled bearOP
okay.
so what's a good way to deal with this behavior then from your point of view?
redirect to it after the mutation in server action
Spectacled bearOP
but UX-wise I already redirect to the created ad - which is the most logical thing to do, don't you think so?
but in your case, I dont know since you are redirecting you /anzeigen/[id]
yes so I don't know lol
Spectacled bearOP
this is super weird.
It's actually a bug if you ask me.
router cache is causing headache for everyone
well not bug, it is documented
Spectacled bearOP
so you say if I create an ad and wait 5 mins before i visit the all ads page it should be updated?
or try this
export async function createAd(formPath, prevState, formData){
    let newAd;
    // ...

    try {
        newAd = await Ad.create({
            // ...
        })
    } catch (error) {
        // ...
    }

    revalidatePath(`/anzeigen`)
    fetch("https://yourdomain/anzeigen")
    revalidatePath(`/anzeigen/${category}`)
    redirect(`/anzeigen/${category}/${newAd._id}`)

    return newState
}
make a GET request in the server action
not sure if it works
Spectacled bearOP
wouldn't it then be more straight forward just to make the route dynamic? with export const dynamic = 'force-dynamic'
then your page is not static generated
I think you want static page?
Spectacled bearOP
static would be best in terms of speed, SEO etc. for sure.
I just wonder if there's no next-specific way to handle such a situation.
That's why I labeled it as bug.
I dont know, it is how it work atm
Maybe you could share this usecase on their github
https://github.com/vercel/next.js/discussions
or use export const dynamic = 'force-dynamic' and data cache
instead of static page
@Ray or use `export const dynamic = 'force-dynamic'` and data cache
Spectacled bearOP
what do you mean by using data cache?
tbh I'm just super confused that it works different locally compared to production.
because locally after build it works fine.
but on vercel it doesn't
Spectacled bearOP
now again it doesn't work for me - even after waiting 5 mins and refreshing the page
@Ray I dont know, it is how it work atm Maybe you could share this usecase on their github https://github.com/vercel/next.js/discussions
Spectacled bearOP
I'll start a discussion about this as you suggested