T
TanStack2y ago
wise-white

When preloading and then navigating to a link, should the loader function always run twice?

I've enabled 'intent' preloading, and I have some conditional behavior in my loader functions based on the 'preload' boolean. I had assumed that the loader function will run twice in this situation, once with the preload=true when someone hovers over the link, and then preload=false when you actually click and navigate to that link. However, I'm seeing that sometimes the loader doesn't run the second time, particularly if you click on a link while the lazy component is loading. Is this expected?
3 Replies
rival-black
rival-black2y ago
SWR is enabled by default and preloaded data is cached for like 30 seconds by default You'll need to customize either preloadStaleTime or preloadGcTime If you bring the stale time of the preload down, it will load instantly, but trigger your loader in the background if it's stale If you lower the gc time enough, the preload won't get cached at all
wise-white
wise-whiteOP2y ago
@Tanner Linsley Thank you, I'll play around with those settings. In your opinion, is it appropriate to do application logic in the loaders (for example, loading the fetched data into a zustand store)? Or should the loaders be totally focused on data fetching, and things like managing global state should happen elsewhere in the application/components?
rival-black
rival-black2y ago
The latter

Did you find this page helpful?