[query-broadcast-client-experimental] broadcastQueryClient: an `added` event overwrites a receiving query that has no data (e.g. in error) with the sender's empty state · Issue #12213 · TanStack/query · GitHub
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
{{ message }}
Repository navigation
[query-broadcast-client-experimental] broadcastQueryClient: an added event overwrites a receiving query that has no data (e.g. in error) with the sender's empty state #12213
When another tab creates a query, the receiving tab applies the sender's state on added whenever its own query has no data. A query that has no data but did settle (a failed query: status: 'error', errorUpdatedAt > 0) is replaced with the sender's brand-new state: status: 'pending', fetchStatus: 'idle', error: null, dataUpdatedAt: 0, errorUpdatedAt: 0.
The receiving query now looks like it is loading, but no fetch runs. Observers render a pending/loading UI indefinitely (until a refetch interval, focus or invalidation happens to trigger), and the error is lost.
Tab A and tab B both call broadcastQueryClient({ queryClient }).
In tab A, useQuery({ queryKey: ['x'], queryFn: () => Promise.reject(new Error('down')), retry: false }). It settles in status: 'error', with no data.
Tab B creates the same query: either mount the same useQuery, or queryClient.getQueryCache().build(queryClient, { queryKey: ['x'] }) (for example, as a side effect of a prefetch that has not started yet).
In tab A, the query is now status: 'pending', fetchStatus: 'idle', error: null, errorUpdatedAt: 0. The observer shows loading, and no request is in flight.
Without the plugin, the same can be shown in a single client:
constquery=queryClient.getQueryCache().find({queryKey: ['x']})!// in error, no dataquery.setState({status: 'pending',fetchStatus: 'idle',data: undefined,dataUpdatedAt: 0,error: null,errorUpdatedAt: 0})// observers now report pending with no fetch running
Expected behavior
An added event carries nothing the receiver lacks when the sender's query has never settled. The receiver should keep its own state, in particular an error.
Suggested fix
Skip an incoming added state that never received data or an error:
Activity