You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've encountered an issue with useSWR's refreshWhenHidden prop. Due to a known behavior across browser vendors, document.visibilityState does not update to hidden when the user changes windows or applications (e.g., using alt+tab or system gestures).
In particular, the problem lies in the isVisible function located in swr/src/_internal/utils/web-preset.ts:
Since document.visibilityState stays visible during window or app switches, useSWR continues to behave as if the window is in the foreground, which interferes with refreshWhenHidden behavior.
Expected Behavior
useSWR should recognize that the window has become inactive when a user switches to another application or browser tab and should stop to revalidate data refreshWhenHidden accordingly, even if document.visibilityState remains visible.
Repro Steps / Code Example
Set refreshWhenHidden to false and refreshInterval to 500 in a component using useSWR.
Switch to a different window or application using alt+tab or a similar gesture.
Note that document.visibilityState does not update to hidden, causing a revalidation every 5 seconds.
You can also test this behavior in a minimal CodeSandbox setup to observe the issue.
This issue appears to be rooted in document.visibilityState handling across browsers, but any insights on improving useSWR's behavior to account for this would be appreciated.
Bug report
Description / Observed Behavior
I've encountered an issue with useSWR's
refreshWhenHidden
prop. Due to a known behavior across browser vendors,document.visibilityState
does not update tohidden
when the user changes windows or applications (e.g., usingalt+tab
or system gestures).In particular, the problem lies in the
isVisible
function located inswr/src/_internal/utils/web-preset.ts
:Since
document.visibilityState
staysvisible
during window or app switches, useSWR continues to behave as if the window is in the foreground, which interferes withrefreshWhenHidden
behavior.Expected Behavior
useSWR should recognize that the window has become inactive when a user switches to another application or browser tab and should stop to revalidate data
refreshWhenHidden
accordingly, even ifdocument.visibilityState
remainsvisible
.Repro Steps / Code Example
refreshWhenHidden
tofalse
andrefreshInterval
to500
in a component usinguseSWR
.alt+tab
or a similar gesture.document.visibilityState
does not update tohidden
, causing a revalidation every 5 seconds.You can also test this behavior in a minimal CodeSandbox setup to observe the issue.
Additional Context
2.2.5
Chrome 131.0.6778.70 (arm64)
12.3.4
This issue appears to be rooted in
document.visibilityState
handling across browsers, but any insights on improving useSWR's behavior to account for this would be appreciated.References
https://stackoverflow.com/questions/28993157/visibilitychange-event-is-not-triggered-when-switching-program-window-with-altt
The text was updated successfully, but these errors were encountered: