Skip to content
Source
Sign inSign up
TanStack/query

Some part of the query state is lost during channel broadcast #3742

Closedmarkivancho(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

App uses broadcastQueryClient utility, components that use the same query lose their query state after opening a new window that triggers the same query.

Adding some context to how I use react-query in my daily job. We are using the default broadcastQueryClient config to sync data if user decides to open app in new tab. In some cases, we utilize isFetchedAfterMount to ensure that user sees the latest data without UI flick. The loading screen is rendered until isFetchedAfterMount is switched to true. On one of the screens we have a link to another part of the app that uses same query. If user click "Open in new tab" on screen that uses isFetchedAfterMount, it will swtich back to false after loading of that query is finished on other tab.

Your minimal, reproducible example

CRA bootstrapped project;

Steps to reproduce

Watch the attached video in the repo's readme.

Expected behavior

I would expect that isFetchedAfterMount stays the same, regardless of queryClient syncs between tabs.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS, Chrome Version 102.0.5005.115 (Official Build) (x86_64)

react-query version

4.0.0-beta.23 and 3.39.1

TypeScript version

No response

Additional context

Some console logging of queryObserver of my production app. First one is state of query after loading is finished in first tab. Second is state of query in first tab after loading is finished in second tab

notice that dataUpdateCount of initialState and state in each screenshot, that's where the part of isFetchedAfterMount logic is hidden

first second

  1. TkDodo added labelspackage: broadcast-client

  2. we only sync the QueryState between tabs; this is the same interface that is used to hydrate from the server or to persist in external storages:

    https://github.com/TanStack/query/blob/357ec041a6fcc4a550f3df02c12ecc7bcdefbc05/packages/query-core/src/query.ts#L38-L50

    Every other prop returned from useQuery, including isFetchedAfterMount, is derived per observer:

    https://github.com/TanStack/query/blob/357ec041a6fcc4a550f3df02c12ecc7bcdefbc05/packages/query-core/src/queryObserver.ts#L556-L558

    So because it is not "state" but derived state, there isn't anything we can do I think.

  3. TkDodo closed this

Sign in to join this conversation.