Clear Router Cache without refetching current route #73603
chriskrogh
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
Background
Problem
Currently, all of the methods of invalidating the Router Cache involve refetching / re-(server)-rendering the current route (
revalidateTag
andrevalidatePath
do this even though it's not mentioned in the docs).This is a problem for us at Faire because some routes can be "expensive" if refetched often.
Use case
Faire is an e-commerce marketplace. We recently migrated most of our critical user journeys (home, search, categories, cart) to be rendered primarily using server components. When users add items to their cart while browsing, we want to invalidate the Router Cache so that subsequent visits to the cart page won't be stale.
However, if we were to do so right now, next would refetch the current route (e.g. "search") every time the cart is updated which is undesired.
Here's our workaround
Whenever we update the cart, we set a global client state marker (
shouldRefreshCartPage
) totrue
. At the top of the cart page'slayout
, we passlastUpdatedAt={Date.now()}
to a client component so we can keep track of how old the cart page's router cace entry is. IfshouldRefreshCartPage
istrue
, and we haven't just loaded the cart page, then callrouter.refresh
This isn't that great since we wait until the cart page starts hydrating to refresh. Clearing the router cache would allow us to prefetch the cart page on subsequent navigations each time it was invalidated.
Proposal
Short term
To preserve the existing functionality as much as possible, it would be nice to add an optional argument to
router.refresh
, andrevalidateTag
e.g.{ refetchCurrentRoute?: boolean }
that'strue
by default, but can be set tofalse
. If set tofalse
, next would clear the router cache for all other pages except the current page.Followup
If we can skip refetching the current route from
revalidateTag
, then hopefully we can invalidate only a specific route in the router cache (for us this would mean only invalidating the cart page).Long term
I know the team is working on some big changes to caching for PPR #72875 but I'm hoping that one day we can clear or refetch the cache for segments individually (similar to react query's clear and invalidate with
refetchType
).Beta Was this translation helpful? Give feedback.
All reactions