CodePark/ questions / Data fetching & caching
START PRACTICINGPRACTICE
Frontend question · Next.js › Data fetching & caching

Posts are fetched with { next: { tags: ["posts"] } } on several pages. After a Server Action creates a post, what is the most targeted way to refresh all those pages?

Call revalidateTag("posts") in the action

Calling revalidateTag("posts") in the action is correct. It invalidates every cached fetch with that tag, on whichever route uses it. The tempting router.refresh() is wrong here. It is a method on the router object that the useRouter() hook returns, so it only works in client code, not inside a Server Action. It also does not clear the Data Cache. redirect navigates to another page but does not invalidate any cached data.