How to Cache and Selectively Refetch Data in a Remix Application? #10457
Replies: 2 comments 9 replies
-
A few options:
I think 2 is the best solution, as you will have more control over your cache, you could invalidate it from any action, you could invalidate the cache from another request made by another user, or in a background job or scheduled task The next best one is the 3, while you won't have that much control over the client-cache (unless you add more other things to let the server tell clients to invalidate the cache) it will still gives you the option to fetch on a single loader (client loader in this case) instead of fetching on every component, you won't have SSR but you can control how you load data in parallel. The worst one (it's not bad just not as good as the other ones) is the 1, in that case you will probably need to drop Remix loaders/actions and use React Query, so you won't get any of the benefits of using Remix loaders/action. |
Beta Was this translation helpful? Give feedback.
-
The static data are stored in Redis, but in my loader, I perform some operations on it.
Yes, 30k users could visit my app daily, and this data will be massive because it will be analytics-related data. Currently, my Node server is not behind a CDN. Is it secure to cache private user data in a CDN? If so, then I could just setup cdn and return max-age & s-maxage from the loader and it will cache my processed data and that's it, right? I'm carious if cdn will handle to cache big amount of data. |
Beta Was this translation helpful? Give feedback.
-
Hello there!
I have multiple forms and charts on the home page (/ route). (I use react router v7 with a node server.)
The loader for this route fetches data from the backend for various tables, charts, and other components. Each table/chart requires a separate API request.
I also have table-specific filters. When I apply a filter to a specific table (e.g., filtering by a certain value), I update the URL search parameters using useSearchParams. In my loader, I read these search parameters to determine if a specific key exists and adjust the request accordingly.
This works fine, but the issue is that I don’t want to reload the entire loader when I change a filter for just one table. Reloading everything leads to unnecessary API calls for unrelated data. How can I split these executions so that only the relevant table’s data is re-fetched? Or, within the loader, how can I determine when to refetch specific data from the backend?
Additionally, what is the best approach for caching data on the client? I’m looking for something similar to React Query, where I can define cache keys. The data should be refreshed every 24 hours, so making a backend request for every interaction doesn’t seem ideal. A CDN is likely not an option since the data is sensitive and available only after user authentication.
Beta Was this translation helpful? Give feedback.
All reactions