Skip to content
Source
Sign inSign up
TanStack/query

useSuspenseQuery fails with "Missing queryFn" for a query that broadcastQueryClient re-created from another tab #12129

ClosedVladislavSedletskiy(GitHub profile, opens in a new tab) opened this issue 0 comments

When a query is removed and then re-added in another tab, broadcastQueryClient builds it in the current tab from { queryKey, queryHash } only, without queryFn. If the other tab's fetch does not succeed, no updated message with data follows, so the query stays pending and has no queryFn.

An already mounted useSuspenseQuery for that key suspends on its next render, and QueryObserver.fetchOptimistic calls query.fetch() without options. Query.fetch falls back to the queryFn of its observers. The suspending observer, though, is still attached to the removed query: it moves to the new one in setOptions, which runs in an effect and never runs while the component is suspended. The fetch rejects with Missing queryFn: '["user"]', and the error reaches the error boundary.

A first mount is not affected, because the QueryObserver constructor passes its options to the query. The non-suspense path is not affected either, because it fetches with query.fetch(this.options).

Real-world case: the user logs out in tab A, and we call removeQueries for the current-user query. The login page in tab A requests the user again and gets 401. The user then switches to tab B, and tab B crashes with Missing queryFn instead of handling the 401.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/k6qymr

Steps to reproduce

  1. Click 1. Log out in tab A. This calls removeQueries(['user']), then prefetchQuery for ['user'], and that fetch rejects with 401.

  2. Click 2. Re-render tab B.

  3. Tab B shows Error: Missing queryFn: '["user"]'.

Expected behavior

Tab B fetches the user with the observer's queryFn, the same way the non-suspense path does, and shows Error: 401 Unauthorized.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS 15.0.1 (24A348)
  • Browser: Chrome 155 (HeadlessChrome/155.0.0.0 via Playwright 1.63; the sandbox and the real two-tab case)
  • Node.js: v25.9.0 (the query-core-only reproduction)
  • The bug is in query-core and the broadcast client, not in the browser: the Node reproduction fails the same way.

Tanstack Query adapter

react-query

TanStack Query version

  • @tanstack/react-query / @tanstack/query-core: 5.104.4 (also reproduces on 5.100.9) - @tanstack/query-broadcast-client-experimental: 5.104.4 (broadcast-channel 7.4.0) - react / react-dom: 19.2.0

TypeScript version

No response

Additional context

In QueryObserver.fetchOptimistic, pass the defaulted options to the fetch:

query.fetch() query.fetch(defaultedOptions)

With this change the reproduction resolves. I checked it against a patched 5.104.4 build.

Another option is for broadcastQueryClient not to build a query from an added message that carries no data.

Our workaround is queryClient.setQueryDefaults(queryKey, { queryFn }) for the affected key.

  1. TkDodo closed this

Sign in to join this conversation.