refetch
Description: Defines a "soft" expiration interval (in milliseconds). When a component renders, Archibald checks this interval to decide if a background refresh is needed.
Default Value: 300000 (5 minutes).
-
How It Works:
- Reactive, not Proactive:
refetchdoes not use an active timer (setInterval). It only performs a check when the component using the hook actually renders or re-renders. - Soft Limit: It acts as a "soft" expiration. When the
refetchtime passes, the cache entry is marked asstale. - Background Update: If a component renders and the data is
stale, Archibald triggers a background fetch. The component continues to show the existing cached data (keepingisDone: true) until the new data arrives, preventing UI flickering. - Respects
ttl:refetchrespects thettl(hard limit). Ifttlis reached beforerefetch, the data is consideredexpired. Ifrefetchis reached first, it is onlystale.
- Reactive, not Proactive:
-
Key Difference:
refetchvs.poll:poll: An active timer. It forces a fetch at fixed intervals regardless of component rendering (as long as it's mounted). Use this for real-time data.refetch: A passive check. It only checks "is the data old?" when the UI actually needs to display it (i.e., on a re-render). Use this for data that doesn't change constantly but should stay relatively fresh.
-
How To: Use
refetchto ensure that users who stay on a page for a long time eventually get updated data without seeing a "hard" loading state. Note that since it is reactive, the update is only triggered if the component (or its parent) re-renders for any reason.// Data is valid for 15 mins, but will attempt// a background refresh after 5 mins on next render.useFetch('profile',fetchProfile,{ttl: 15 * 60 * 1000,refetch: 5 * 60 * 1000}); -
Best Practice:
refetch vs ttlAlways keep
refetchlower than or equal tottl. Ifrefetchis higher thanttl, the data will hit the hard expiration (ttl) and trigger a full fetch before the "soft"refetchinterval is even reached.- Use
refetch: -1to disable this behavior entirely, meaning data will only be refreshed if it completely expires (ttl) or if a manualrefetch()is called. - If you use
ttl: -1(endless caching), you can still userefetchto allow the "permanent" data to be updated in the background when components re-render.
- Use