Next.js Discord

Discord Forum

What is the difference between redirect after mutating in server action and revalidatePath ?

Answered
Chub mackerel posted this in #help-forum
Open in Discord
Chub mackerelOP
So basically both seem identical in my app, for example adding a review in a movie page that triggers a server action which mutates and then redirects to the referrer so basically a "refresh" and new data comes in, so why use revalidatePath ?

For example /movies/3/Spiderman here i add the review but i use redirect to go to the same page not on other URL and it works even with redirect, why use revalidatePath on this url when redirects also works want to understand the difference and best practice here
Answered by James4u
yeah, even they are doing the same thing, revalidatePath() will be the correct usage
View full answer

17 Replies

@Chub mackerel the reason we use revalidatePath or revalidateTag is to purdge the cache after data mutation.
that's because there's a full route cache which is similar to browser b/f cache
so when you navigate through <Link /> tag, those pages will be cached even its' dynamic
that's why we need to revalidate after data mutation
ofc if you hard-refresh the page, you will see the updated content
Chub mackerelOP
@James4u yeah but in my case i used redirect and it didn't refresh hard reload just worked so was like what is the difference, i add a review to movies/5/spiderman it doesn't hard reload just gives me updated content with redirect same functionality with revalidatePath, the docs say that use redirect when you are trying to go to another URL adding a post after adding it go to the post but this is same page
okay so you mean you add a review in the same page, right?
@James4u okay so you mean you add a review in the same page, right?
Chub mackerelOP
yes same page
or upvotes downvotes to the review anything to the same page
then revalidatePath() and router.refresh()
Chub mackerelOP
why router.refresh ? i do everything there on server action it adds completes mutation then goes to the page ?
oh sorry my bad I guess revalidatePath() should be enough
oh @Chub mackerel I now understand your question
I think in your case, both should work and there is no difference
as you are in the same page, they are doing the same thing I guess
@James4u as you are in the same page, they are doing the same thing I guess
Chub mackerelOP
checked also the network tab on revalidatePath vs redirect same stuff identical i think, will keep revalidatePath cause i see in 99% of project in nextjs seems more "meta" redirect i saw is mostly used for other url changes
yeah, even they are doing the same thing, revalidatePath() will be the correct usage
Answer