Replies: 2 comments 3 replies
-
|
You need to create two nested routes, this way you can have a route loader with only public data and another with only private data. Then add a clientLoader to the public route, this will make that route be separated from the rest of the routes, so you can cache it individually. This will only work for non-SSR requests, as SSR is a single request always, the only option you would have in that case would be to SSR with only public data, or with only private data, and then fetch the other one client-side. That said, using a server-side cache would be better, cache the public data in a shared cache key using something like Redis or Cloudflare Worker KV, then read from there. This will also let you easily invalidate the data (you can just purgue your cache), and even share it across users/devices. You could also cache the private data, just ensure the cache key somehow identifies the user so other users won't reuse the cache. |
Beta Was this translation helpful? Give feedback.
-
|
A public/private split like this is a bit against the original goal of single fetch which was to more closely align how document/data requests work. We found the default behavior pre-single-fetch meant that folks very often messed up the caching for one scenario or the other. If we were to add a split like this we're right back into the same boat where you can't cache document requests but you can partially cache data requests. As noted above, My suggestion for this scenario through would be to just use a fetcher against a resource route for the private data. This has a number of benefits:
And the main tradeoff is that the request initiation is delayed slightly because it has to happen on the client. On client side navs, you could manually prefetch the fetcher URL from your own link hover/viewport logic to eliminate this delay. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
We're starting to experiment with HTTP Cache (
Cache-Controlheader) and CDN cache (in our system, also works with a HTTP header), and have seem quite some improvements for the client in terms of performance, mainly in combination with<link rel="prefetch">to eagerly request those pages and cache them in the browser.The problem now comes when the route handles authenticated data - session, cookies, etc. Here, we obviously can't use CDN caching, but instead a short HTTP cache, at best. The problem is that this route also requests static/public data, which could be cached.
Is there a way React Router provides to split a route loader into two? One for the "private" data, which goes uncached, and another for the "public" data, that can be cached?
Example:
Let's say we have a product page for an e-commerce. Part of the data is normal product data - images, name, price, etc. These rarely change, so we could have, say, 5 minutes of cache in CDN. On top of that, we want to display the delivery time of the product, which depends on the address of the logged-in user. For this, we'd use a different endpoint to get that.
So once the user clicks on a
<Link>to that product page, the does 2 requests:/my-product.public.data: Gets the public product data/my-product.private.data: Gets the "private", user-specific product dataBefore:
After:
Main goals
The main advantages and goals for this are:
Considered approaches
Problem: Doesn't run on server-side render, and only happens after first endpoint finished
clientLoaderto fetch additional endpointProblem: Doesn't run on server-side render, and only happens after JS of the Component is fetched
Problem: HTTP headers still can't be separate due to Single Fetch.
Beta Was this translation helpful? Give feedback.
All reactions