Next.js Discord

Discord Forum

When using ISR, how can I trigger an "early" revalidation?

Answered
Swyftey posted this in #help-forum
Open in Discord
Let's say the page for editing a user profile revalidates every 10 seconds. What if the user specifically presses a button which immediately updates data to the database?

What can I do so that if the data is updated, the changes are visible immediately? If it helps to provide an answer any, the "update" API endpoint would return the updated data in the response.
Answered by joulev
revalidatePath/revalidateTag. the change will be available immediately following the button click.
View full answer

3 Replies

Answer
Thank you!
@Swyftey Thank you!
hold on, i forgot to add some important clarifications:

* it must be in a server action. if you use a route handler instead, you can do like
const router = useRouter();
async function onClick() {
  await fetch(...);
  router.refresh();
}


* it works (in a server action) by actually rerunning the server component and returning the result in the response body. so the updated data from the "update" API endpoint won't be used anywhere