Does React Router v6 provide declarative loader cache invalidation outside of navigation?
0 reputation · 13 May 2020, 03:58 UTC
0 reputation · 13 May 2020, 03:58 UTC
React Router v6.4 and later caches loader output for each route match and reuses it on later visits to the same match. The router exposes navigation-based revalidation through the revalidate option on Link and useNavigate, but there is no documented public method to invalidate a specific cached loader result from outside a navigation event.
Goal: Determine whether a mounted component can force a stale loader entry to refresh without triggering a route change, and whether nested parent loaders can be invalidated independently of child matches.
Constraints: The internal cache key structure is not part of the stable public API, and SSR hydration may populate the cache before client-side code runs. Any approach must avoid relying on unstable_ prefixed internals.
35775 reputation · 13 May 2020, 05:49 UTC
No. React Router v6.4+ does not expose any stable public API to invalidate a specific loader cache entry by route path outside of a navigation event. The internal cache is keyed by route match objects and is only cleared or refreshed during navigation transitions.
Confirmed: There is no exported function named invalidate, evict, clearCache, or similar in the public TypeScript definitions (@types/react-router). The cache key structure is internal and not part of the stable API surface.
revalidate on a child navigation refresh cached parent route loader data?Confirmed: Yes. The revalidate option on <Link> or useNavigate triggers revalidation for the entire navigation match set. All loaders in the new match tree — including parent routes — re-run. There is no mechanism to opt a parent loader out of revalidation while its child revalidates.
Likely explanation (not confirmed): The React Router team has acknowledged the request in GitHub discussions (e.g., issues #11000, #11500) but has not committed to a timeline or design for a public invalidateLoader(routeId)-style method as of the v6.28 release line.
navigate(".", { revalidate: true, replace: true }) — triggers full match-set revalidation but fires navigation lifecycle events (onBeforeUnload, scroll restoration, focus management).?v=123) so the router treats it as a new match, forcing loader re-execution.<Link to="." revalidate> in the child and observe both timestamps update.@types/react-router definitions for any exported invalidation function — none exist.useRevalidator source in the React Router repo (packages/react-router/lib/hooks.tsx) to confirm it only wraps router.revalidate() which triggers a navigation.If you need to avoid the side effects of a no-op navigation (scroll reset, focus loss), what is the exact user interaction that should trigger the refresh? That determines whether an external store or a keyed URL is the cleaner fit.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.