Skip to content

Improve accuracy in determining visibility state#4120

Open
dcondrey wants to merge 6 commits intovercel:mainfrom
dcondrey:dcondrey-patch-2
Open

Improve accuracy in determining visibility state#4120
dcondrey wants to merge 6 commits intovercel:mainfrom
dcondrey:dcondrey-patch-2

Conversation

@dcondrey
Copy link

@dcondrey dcondrey commented Apr 17, 2025

Separated isVisible() and hasFocus() and poll conditions independently.

Related Issues

Fixes #3039, #4130

@dcondrey dcondrey requested review from huozhi and shuding as code owners April 17, 2025 13:04
@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 17, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@dcondrey dcondrey force-pushed the dcondrey-patch-2 branch 5 times, most recently from b2aadc1 to 4a2c832 Compare December 24, 2025 07:55
dcondrey added 5 commits March 3, 2026 10:57
Enhanced visibility check.  Resolves vercel#3039
no longer using isUndefined
- Change isActive() to accept hasFocus() as alternative to isVisible(),
  fixing revalidateOnFocus in Chrome where visibilityState can be stale
- Default refreshWhenUnfocused to falsy (consistent with refreshWhenHidden
  and refreshWhenOffline), so polling pauses on alt-tab via hasFocus()
- Restore isActive() in FOCUS_EVENT handler (was incorrectly changed to
  isOnline() only)
- Rewrite unit tests to verify actual behavior with mocked state
- Update mockVisibilityHidden() to also mock hasFocus() for accuracy

Fixes vercel#3039, vercel#4130
@dcondrey dcondrey requested a review from shuding March 4, 2026 01:47
Add explicit type annotation to the `use` constant so TypeScript sees a
single callable signature instead of trying to unify incompatible
overloaded signatures from React.use and the inline fallback.
@dcondrey
Copy link
Author

dcondrey commented Mar 5, 2026

Note: Added a fix for the failing test-typing CI checks. The use polyfill in src/index/use-swr.ts was defined as a union of React.use and an inline fallback whose signatures TypeScript 5.9 couldn't unify, causing TS2349: This expression is not callable errors. Added an explicit type annotation to give it a single callable signature. This is unrelated to the visibility/focus changes.

promer94
promer94 previously approved these changes Mar 6, 2026
@promer94 promer94 dismissed their stale review March 10, 2026 15:40

I need to test it with real browser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refreshWhenHidden not working properly

3 participants