Next.js Discord

Discord Forum

How to revalidate path on client side?

Answered
Broken Nokia posted this in #help-forum
Open in Discord
I'm getting an error when using revalidatePath() in a client component. What is the correct function to use for this inside of client components?
Answered by Anay-208
You can use a api route or a server action
View full answer

18 Replies

I could be wrong, but i believe this is caused due to revalidatePath() only being applicable to server components
You can create a api route for revalidating
@Anay-208 You can create a api route for revalidating
i'll give this a try, is there a function specifically for the above purpose that can be used client side?
@Broken Nokia i'll give this a try, is there a function specifically for the above purpose that can be used client side?
no, I wouldn't recommend it, as malicious actors can then clear cache of all the pages, which can result in increased DB costs
You can use a api route or a server action
Answer
with regards to server actions:
I'm assuming i create a function that runs revalidatePath() and call that function via my client component, correct?
also could you please elaborate on your previous message, i want to understand how DB costs go up
@Broken Nokia also could you please elaborate on your previous message, i want to understand how DB costs go up
So if you’re using a pay as you go db, a bad actor can abuse the function, because it can basically revalidate any part.

So you might be having a route /, which caches content. This cache can be completely removed by the bad actor
Summary:
- You can use server actions or api routes to Create revalidate Functions
- Don't let the user specify the path to be revalidated, as it can be abused by bad actors
Mark the above message as a solution
sorry for the delay
thank you @Anay-208 ❤️
Orinoco Crocodile
im using auth.js and when im retrieving session on a client-side, revalidatePath doesnt fire
however, if im retrieving session from a server-side component, revalidation is successfull.