How to revalidate but RESPECTING dedupingInterval #532
-
There are a few open issues and PRs related: #271 #482 #417 #519 The issue is as follows. Given the following:
Now, when using one or multiple things above (e.g. {
dedupingInterval: 15 * 1000,
revalidateOnMount: false,
revalidateOnReconnect: false,
revalidateOnFocus: false
} Visibility (instead of Mount)Then, for those components/hooks that are used in Dialogs, where you want to refetch on "visibility" (which feels like mount), you can handle this yourself: useEffect(() => {
if (!visible) {
return
}
revalidate()
}, [visible]) The issue with the above is that if somewhere in the tree you already fetched this data, A few alternatives that would respect // Fetcher has the useSWRHook, so this would act like a mount. Set `revalidateOnMount` to true.
visible && <Fetcher onDataChanged={setData} />
// When not visible, "break" swr, doesn't work well with Suspense, requires "prop drilling" with custom hooks
useSWR(visible ? 'key' : null)
// Or explicitly break, making visible a dependency
useSWR(() => {
if (!visible) {
throw new Error('Not visible')
}
return 'key'
}) Userland focus (instead of global window Focus)For "screen", you probably want to handle focus yourself: useFocusEffect(revalidate, [revalidate]) Again, if multiple hooks request the same key, this will force requests multiple times, disrespecting dedupingInterval. Userland online (instead of browser online)Some services/libraries expose net-connectivity, and you only want to revalidate when those change (think: flaky service, websockets, duplexes, failover's and non-traditional network connections), as well as be able to use your own method of net-connectivity instead of Again, we can handle this ourselves: const { isInternetReachable } = useNetInfo()
useEffect(() => {
if (!isInternetReachable) {
return
}
revalidate()
}, [isInternetReachable] Again, the same "workarounds" can be used to use the deduping issues. ProblemUsing the workaround feels both very against the grain, and also don't compose at all. I basically need a HOC that exposes a way to create a Possible solutions#417 gives a glimpse of what could be made, which I suggest is implemented for all the "revalidate" checks:
If #417 had an implementation (move all these "detectors" to configuration), the question for me would be resolved. The setFocusHandler(handleFocus => {
// Listen to visibillitychange and focus
if (typeof window !== 'undefined' && window.addEventListener) {
window.addEventListener('visibilitychange', handleFocus, false)
window.addEventListener('focus', handleFocus, false)
}
return () => {
// Be sure to unsubscribe if a new handler is set
window.removeEventListener('visibilitychange', handleFocus)
window.removeEventListener('focus', handleFocus)
}
}) But realistically, before that is implemented, adding Am I missing something? I can create minimal working examples that show this issue, but I think it's clear. Feel free to ask me tho. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
FWIW, I moved to |
Beta Was this translation helpful? Give feedback.
FWIW, I moved to
react-query
.