Window Focus Refetching
See original GitHub issueFrom FR #247
Currently, Apollo Client’s pollInterval
refetches every n milliseconds, regardless of whether the user is actively using the page or not, which isn’t optimized. Rising libraries such as SWR or React Query support focus refetching, meaning that the client will not poll if the user has switched to a different tab or window, and it will immediately fetch again if the user comes back to the tab. This saves a lot of wasted requests, while maintaining a great UX.
It would be nice to see such feature implemented in Apollo Client as well.
This is how these 2 libraries implement the focus:
~https://github.com/vercel/swr/search?q=isDocumentVisible&unscoped_q=isDocumentVisible~ ~https://github.com/tannerlinsley/react-query/search?q=isDocumentVisible&unscoped_q=isDocumentVisible~
- https://github.com/vercel/swr/search?q=visibilityState
- https://github.com/TanStack/query/blob/f6eeab079d88df811fd827767db1457083e85b01/packages/query-core/src/focusManager.ts#L22
See FR #247 for more details as it contains a lot of good commentary from the community.
Issue Analytics
- State:
- Created a year ago
- Reactions:9
- Comments:5 (2 by maintainers)
Top GitHub Comments
Hi @jpvajda,
I build a proof of concept implementation: https://github.com/xcv58/apollo-client/pull/1/files#diff-ebf1072d16b9775e8cae2d74140e5da612378bf107621ab70c3769b3c4d31ee5
I think this implementation is not optimal since it will add listeners for each query with the
refetchOnFocus: true
option. We can add the global listeners onQueryManager
class once and use itsthis.queries.forEach
to broadcast when the focus state change and let individualQueryInfo
handle the events.Could you please take a look and share your feedback?
@jpvajda the links are out of date. Below links are their implementations:
https://github.com/vercel/swr/search?q=visibilityState
https://github.com/TanStack/query/blob/f6eeab079d88df811fd827767db1457083e85b01/packages/query-core/src/focusManager.ts#L22