Revalidation for data using next.js 15 route handlers
I've developed an application using Next.js 15, utilizing API routes for the backend. However, I'm encountering an issue where, after posting data, the new information only appears upon manually refreshing the page. Despite looking at official documentation, watching tutorials, and reading articles, I haven't found a solution. I'm beginning to think that to achieve active refresh upon table updates, I might need to use Server Actions instead of API route handlers, since whereever I looked at for revalidation, everyone was using server components.
// DataTable.tsx
// Form.tsx
4 Replies
Upon someone's suggestion, I used router.refresh() but that didnt work as well. The only thing that seems to work is window.location.reload() which ends up doing a hard refresh.
Have you tried to keep revalidateTag on the server side (on the API route) ?
Since it is a cache related function, it should be called on the server side
It only works for server components from what I've read.
I was able to come up with a solution, it looks like the way I was fetching the data was wrong:
here is the updated code:
They work everywhere on the server side, especially with server actions and API routes. Often we use revalidate (revalidateTag or revalidatePath) after a mutation. Mutations generally don't happen in server components. Because they are mostly used to fetch data when needed, not to mutate data.
I have used revalidateTag in both server actions and API routes and they both work
Anyways, glad to see you have already found a way to solve the problem 👍