Repository navigation
fix(focusManager): stop listening for focus events - #4805
Conversation
The `focus` event had many caveats (as discussed in TanStack#4797), so we now listen only for `visibilitychange` event.
|
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. Latest deployment of this branch, based on commit 52f4cda:
|
| If you see a refetch that you are not expecting, it is likely because you just focused the window and TanStack Query is doing a [`refetchOnWindowFocus`](../guides/window-focus-refetching). During development, this will probably be triggered more frequently, especially because focusing between the Browser DevTools and your app will also cause a fetch, so be aware of that. | ||
|
|
There was a problem hiding this comment.
wow this is great! I didn't know that it would also fix this issue ❤️
| [//]: # 'Example5' | ||
| [//]: # 'Example4' |
There was a problem hiding this comment.
maybe we should switch to named examples @DamianOsipiuk 😅. I see the vue-query docs only overwrite/use the 'Example' slot, but this could become tedious with numbers
There was a problem hiding this comment.
As you know naming things is hard, so i just went with numbers 🙈
But yeah, probably we should name them properly.
TkDodo
left a comment
There was a problem hiding this comment.
accidentally pressed request changes - i wanted to approve 😅
|
@TkDodo I believe we should add a note about this to the migration guide. |
|
aah yes, forgot about that 👍 . |
|
Hello guys. We have also encountered ux experience issues caused by focus recently and would like to know if this pull request can be merged into version V3. The risk is too high for us to perform a major version upgrade |
it's a breaking change so no. also, it was merged to v5. seems like you haven't upgraded in over a year ... |
|
Yes. we are keeping using v3. And we fix this by git-patch. Maybe it's time
to upgrade. Thx~
Dominik Dorfmeister ***@***.***> 于2023年7月15日周六 16:19写道:
… if this pull request can be merged into version V3
it's a breaking change so no. also, it was merged to v5. seems like you
haven't upgraded in over a year ...
—
Reply to this email directly, view it on GitHub
<#4805 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACIBOGGUNCILAYE2DVUJ4YDXQJHAPANCNFSM6AAAAAATZUTNZQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
|
@gabrielbrattgard it does whatever the browser visibilitychange does so yes, that's an purpose. You can totally opt back into the |
|
This is great @ilyasmez! |
@RiccardoRomagnoli, do you really want the page to refetch when the user interacts with it? This might result in content changing under their cursors (e.g. the item they wanted to click is not there anymore, or the position of the item changes). I believe that interacting with the page is already too late. |
This behaviour can be desirable in certain scenarios. E.g. A notification badge counter that can update when a user refocuses on to the browser window. My workaround fix from the comments on this thread plus experimenting with other ways: If anybody can explain the weird reason why the queries only refetch if manually triggered I'd love to know and improve what I'm doing here. |
── 변경 이유 (WHY) ────────────────────────────────── useWindowFocusManager가 창 focus/blur로 React Query의 포커스 판단을 덮어쓰고 있었다(2026-01-18 첫 커밋부터, 이유 기록 없음). TanStack Query v5는 focus 이벤트가 iframe·alert/confirm·DevTools·파일 선택 창에서 잘못 발생하는 문제로 창 포커스 재조회를 visibilitychange로만 하도록 바꿨다 (TanStack/query#4805). 게다가 focusManager.setFocused(boolean)을 한 번 부르면 그 뒤로는 탭이 보이는지를 보지 않아(query-core focusManager.ts isFocused()), 떠날 때 blur만 오고 돌아올 때 focus가 안 오는 브라우저에선 돌아와도 재조회되지 않을 수 있었다. 2026-10-14 사용자 결정으로 제거한다. ── 변경 내용 (WHAT) ───────────────────────────────── - useWindowFocusManager.ts 삭제, QueryProvider에서 호출 제거, eslint.config.js 허용 목록에서 항목 제거 - useAppVersionCheck.ts JSDoc: 옛 훅을 선례로 든 서술 정정(새 배포 감지는 그대로 focus·visibilitychange를 모두 듣는다) - NEW-VERSION-RELOAD.md·CACHE-STRATEGY.md §11 갱신, CHANGELOG - 계획 docs/plans/2026-10-14-cache-strategy-followups.md 커밋(PR 4개 중 첫째) ── 영향 범위 ──────────────────────────────────────── 데스크톱에서 창 두 개를 나란히 띄우고 오갈 때(둘 다 보이는 상태)는 더 이상 재조회하지 않는다(사용자 승인). 파일 선택 창을 닫을 때의 재조회는 사라진다. 검증(e2e 모킹, staleTime 지난 뒤): focus만 → 목록 재조회 0건, 탭이 다시 보임 → 1건. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
── 변경 이유 (WHY) ────────────────────────────────── useWindowFocusManager가 창 focus/blur로 React Query의 포커스 판단을 덮어쓰고 있었다(2026-01-18 첫 커밋부터, 이유 기록 없음). TanStack Query v5는 focus 이벤트가 iframe·alert/confirm·DevTools·파일 선택 창에서 잘못 발생하는 문제로 창 포커스 재조회를 visibilitychange로만 하도록 바꿨다 (TanStack/query#4805). 게다가 focusManager.setFocused(boolean)을 한 번 부르면 그 뒤로는 탭이 보이는지를 보지 않아(query-core focusManager.ts isFocused()), 떠날 때 blur만 오고 돌아올 때 focus가 안 오는 브라우저에선 돌아와도 재조회되지 않을 수 있었다. 2026-10-14 사용자 결정으로 제거한다. ── 변경 내용 (WHAT) ───────────────────────────────── - useWindowFocusManager.ts 삭제, QueryProvider에서 호출 제거, eslint.config.js 허용 목록에서 항목 제거 - useAppVersionCheck.ts JSDoc: 옛 훅을 선례로 든 서술 정정(새 배포 감지는 그대로 focus·visibilitychange를 모두 듣는다) - NEW-VERSION-RELOAD.md·CACHE-STRATEGY.md §11 갱신, CHANGELOG - 계획 docs/plans/2026-10-14-cache-strategy-followups.md 커밋(PR 4개 중 첫째) ── 영향 범위 ──────────────────────────────────────── 데스크톱에서 창 두 개를 나란히 띄우고 오갈 때(둘 다 보이는 상태)는 더 이상 재조회하지 않는다(사용자 승인). 파일 선택 창을 닫을 때의 재조회는 사라진다. 검증(e2e 모킹, staleTime 지난 뒤): focus만 → 목록 재조회 0건, 탭이 다시 보임 → 1건. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Why?
As discussed in #4797,
focusevent has many caveats, as it's triggered in the following scenarios:alert,confirm).I think the original idea behind
refetchOnWindowFocuswas to refetch the queries when the user "comes back" to the page, and that's achievable usingvisibilitychange.Before
You can see that the query is being refetched in many unexpected scenarios, while the user didn't leave the page.
rq-focus-fix-before.1.mp4
After
The query is refetched only when the user stop using the app, and comes back.
rq-focus-fix-after.1.mp4
What does this PR do
focusevent listener infocusManager.