Next.js Discord

Discord Forum

How do I invalidate the Router Cache and then navigate to a different route?

Unanswered
Red carpenter ant posted this in #help-forum
Open in Discord
Red carpenter antOP
Hi all,

I am currently working on a CRUD application using NextJS. On the client I am using mutations (with tRPC / React-Query) to create and update profiles (stored in a database). After the user creates a new record, I want to invalidate the router cache and then immediately redirect the user to the detail page of the newly created profile.

I tried doing this:

router.refresh();
return router.push(`/profiles/${profileId}`);


But this does not seem to work. I expect that router.refresh invalidates the Router Cache, but this does not happen. I tested this by creating a new profile, after which I get redirected to the new profile page. When I am on this new page, I click on a link back to the page that I came from (the page that shows all the profiles) and the new profile is NOT on that page. It only shows up after a hard refresh or after 30 seconds (which is the duration of the Router Cache). The documentation explains that router.refresh should immediately invalidate the router cache, but it doesn't.

How can I fix this?

0 Replies