Improve loader data prefetching #7898
Replies: 3 comments 1 reply
-
I pushed up a PR which demonstrates how 2. might be implemented #7903 |
Beta Was this translation helpful? Give feedback.
-
Remix is pretty bullish on "using the platform" so I don't know that we want to forego the browser prefetch cache in favor of maintaining our own internal in-memory cache. I think this feels like a behavior of the prefetch cache that maybe can be explicitly called out in our docs so folks know in which scenarios it might not be appropriate to use the
Thanks for taking a stab at this! My main concerns with this approach in #7903 would be introducing over-prefetching and whether it works cross browser. You may be able to implement this in user-land as well via |
Beta Was this translation helpful? Give feedback.
-
You can use the Vary header, combined with a dummy cookie that contains some random value (it could even be just |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem
Putting loader data in the browser's prefetch cache can easily cause your UI to become out of sync with the server because Chrome (and others?) aggressively cache for 5 minutes, ignoring all cache control headers other than 'no-store' -- even if you say "max-age=10" or "no-cache, must-revalidate" it'll keep it for 5 minutes.
This makes
<Link prefetch />
unusable for any pages which cannot tolerate being stale for 5 minutes (i.e. any page which returns data that can change as the result of a mutation).For example, imagine this common ecom scenario:
EXPECT: Product detail page to indicate it's in your wishlist
ACTUAL: Product detail page does not indicate it's in your wishlist because it rendered stale loader data from the prefetch cache 😬
It's therefore critical for loaders which cannot tolerate stale responses to return
Cache-Control: no-store
just in case somebody accidentally links to it with a prefetch link, making prefetching useless 😔Possible Solutions
Given the prefetch cache doesn't have an API to invalidate the cache (AFAIK), I think it would be worth avoiding the browser prefetch cache for loader data and instead caching it in memory so that Remix can decide when to invalidate / revalidate the cache entry after a mutation.
Alternatively, it might be reasonable for Remix to re-prefetch after a mutation, as I believe browsers will use the latest prefetch, even if in-flight.
prefetch="intent"
effectively does this because you naturally hover over the link again in step 4.Beta Was this translation helpful? Give feedback.
All reactions