Improve accuracy in determining visibility state by dcondrey · Pull Request #4120 · vercel/swr · GitHub
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
Copy Markdown

@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
Copy Markdown

codesandbox-ci Bot commented Apr 17, 2025

Comment thread src/_internal/utils/web-preset.ts Outdated
@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
Copy Markdown
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

@dcondrey dcondrey requested a review from promer94 March 31, 2026 10:35
Copy link
Copy Markdown
Collaborator

@promer94 promer94 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this pr would solve #4130

i was unable to reproduce the original bug.

It did solve the previous issue nicely❤️

But i am still hesitate to add another option here.

Comment thread src/_internal/types.ts
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the default value here should be true as you have commented.

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