Add revalidatingUrl: URL
argument to ShouldRevalidateFunctionArgs
to better support generic revalidation logic (like opting into pre-single fetch behavior!)
#10398
Replies: 2 comments 1 reply
-
Hey 👋 I've posted the same discussion after migrating from Remix to React Router in that repo. |
Beta Was this translation helpful? Give feedback.
-
I think shared revalidation logic is really important in most large applications, so I agree with the intent of this proposal, and in need of a solution too! In general the ergonomics of using URLs to understand the route structure seems unnatural given the
Also, I wish there was another mechanism to get the current route ID/path without passing the With that said, I think the proposal here makes a lot of sense given the current signature of |
Beta Was this translation helpful? Give feedback.
-
Problem
With the update to single fetch, when navigating child routes, the parent is also revalidated by default.
For the following example navigation,
/animals/dog
to/animals/cat
This is a huge behavior shift which has reasons—that's fine, it's probably a good default for most apps. Our app is very data/server intensive and we intentionally followed this hierarchy to make navigations fast and efficient. The challenge we face, however, is that the old behavior is difficult to generically opt back into.
Consider the
ShouldRevalidateFunctionArgs
:https://github.com/remix-run/react-router/blob/1518f7d5d58553859eb2bc3490609a47ded3060f/packages/react-router/lib/router/utils.ts#L168-L182
For a navigation, you can look at
nextUrl
andcurrentUrl
. However, there's no argument indicating what therevalidatingUrl
(or current route, the route where the shouldRevalidate function is defined) is. I cannot create a shared, genericshouldRevalidate
function that all of my routes can import and re-export. To opt into the previous behavior, I have to pass in the route that is callingshouldRevalidate
. Something likeThis simple example is fine, but what happens when you're now nested many routes deep? What happens when there's path variables? Now it can't be a
startsWith
, it has to parse the variables to check for path matching. Also, what happens if I reorganize my routes? I would have to update every shouldRevalidate manually.Proposed Solution
The bottom line is that we need an easy way to identify that a navigation request is a child route navigation and be able to say
shouldRevalidate
is false for this situation.I think the best way to do this would be to add a
revalidatingUrl: URL
argument toShouldRevalidateFunctionArgs
. This would be the url of the current route file.For the following example navigation,
/animals/dog
to/animals/cat
, these are what theShouldRevalidateFunctionArgs
would look like for the two routes.currentUrl
nextUrl
revalidatingUrl
This is a very generic and powerful solution. I think this would provide a lot of value for the future and provides flexibility for all sorts of generic shouldRevalidate use cases. I also hope this would be fairly easy to implement, although I have not looked at any of the code to know for sure.
Other things I considered
defaultShouldRevalidateLegacy
boolean which mimics the pre single-fetch behavior. Yuck.isChildRouteNavigation
boolean... Yuck.shouldRevalidate
is so close to being an elegant solution.Conclusion
I've seen a few people in discord bring up this issue. I'm hoping this official proposal can get us working towards a solution!
Beta Was this translation helpful? Give feedback.
All reactions