Skip to content
Source
Sign inSign up
TanStack/query

fix(broadcast-client): don't overwrite an existing query's data on an incoming 'added' message #11392

Mergedlazerg(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/11392/headmerged by TkDodo View on GitHub

🎯 Changes

Since 5.102.0, the added broadcast message carries the query's state, and the receiving tab applies it with query.setState(state) whenever the query already exists locally. A tab mounting a query for the first time always broadcasts added, even if it hasn't fetched yet, so any other tab that already has resolved data for that key gets reset back to pending with no data.

This only skips setState when the local query already has data, so a query that has never resolved locally can still adopt state from an added message (matches how a brand-new tab picks up a key another tab already resolved via initialData), while an already-resolved query in one tab won't get clobbered just because another tab mounts the same key.

Fixes #11391

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.

Summary by CodeRabbit

  • Bug Fixes

    • Preserved existing query data when another browser tab broadcasts an added update for the same query.
    • Allowed queries without data to adopt the broadcasted successful state.
    • Continued creating unknown queries from incoming broadcast updates.
  • Tests

    • Added coverage for cross-tab query synchronization, including populated and data-less queries.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: Path: .coderabbit.yaml

      Review profile: CHILL

      Plan: Team

      Run ID: 39f92968-21f7-468f-a281-699e5b272555

      📥 Commits

      Reviewing files that changed from the base of the PR and between d47e3e9df848402bb68f14bba7a5dfb46e6b821f and 0a73db4c255eb44e1889696f8d5a9ddf57934229.

      📒 Files selected for processing (1)
      • packages/query-broadcast-client-experimental/src/__tests__/index.test.ts
      💤 Files with no reviewable changes (1)
      • packages/query-broadcast-client-experimental/src/tests/index.test.ts

      Included review availability: Your plan provides up to 10 included reviews per hour; 8 remain after this review.


      📝 Walkthrough

      Walkthrough

      The broadcast client now preserves existing query data when it receives an added event. It applies the broadcast state only to existing queries without data and builds unknown queries from the broadcast state. Tests cover both existing-query cases, and a patch changeset documents the fix.

      Changes

      Broadcast added-state handling

      Layer / File(s)Summary
      Conditional handling for added broadcasts
      packages/query-broadcast-client-experimental/src/index.ts
      Existing queries receive the broadcast state only when their current data is undefined.
      Added-state regression coverage and release metadata
      packages/query-broadcast-client-experimental/src/__tests__/index.test.ts, .changeset/fix-broadcast-added-overwrites-existing-data.md
      Tests cover resolved and data-less queries. The changeset declares a patch release.

      Estimated code review effort: 2 (Simple) | ~10 minutes

      Merge Risk: ⚪ Minimal · up to 0a73d

      The broadcast client preserves resolved local query data when receiving an added message while allowing data-less queries to adopt incoming state. No merge-blocking risk remains.

      🚥 Pre-merge checks | ✅ 4 | ❌ 1

      ❌ Failed checks (1 warning)

      Check nameStatusExplanationResolution
      Docstring Coverage⚠️ WarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files.Write docstrings for the functions missing them to satisfy the coverage threshold.
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the primary fix: preventing incoming 'added' messages from overwriting existing query data.
      Description check✅ PassedThe description covers the change, motivation, linked issue, testing, contributor checklist, and changeset requirement. It is complete enough for review.
      Linked Issues check✅ PassedThe implementation and tests satisfy issue #11391. Existing resolved queries preserve their data, queries without local data adopt incoming state, and the shared handling path applies to browser and s…
      Out of Scope Changes check✅ PassedThe changeset, implementation change, and focused tests are directly related to fixing the incoming 'added' message behavior. No unrelated changes are present.
      • Fix all pre-merge checks with AI
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    2. TkDodo approved these changes

    3. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 0a73db4c255eb44e1889696f8d5a9ddf57934229

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded3m 31sView ↗
      nx run-many --target=build --exclude=examples/*...✅ Succeeded51sView ↗

      ☁️ Nx Cloud last updated this comment at 2026-09-21 08:49:50 UTC

    4. More templates

      @tanstack/angular-query-experimental

      npm i https://pkg.pr.new/@tanstack/angular-query-experimental@11392

      @tanstack/eslint-plugin-query

      npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@11392

      @tanstack/lit-query

      npm i https://pkg.pr.new/@tanstack/lit-query@11392

      @tanstack/preact-query

      npm i https://pkg.pr.new/@tanstack/preact-query@11392

      @tanstack/preact-query-devtools

      npm i https://pkg.pr.new/@tanstack/preact-query-devtools@11392

      @tanstack/preact-query-persist-client

      npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@11392

      @tanstack/query-async-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@11392

      @tanstack/query-broadcast-client-experimental

      npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@11392

      @tanstack/query-core

      npm i https://pkg.pr.new/@tanstack/query-core@11392

      @tanstack/query-devtools

      npm i https://pkg.pr.new/@tanstack/query-devtools@11392

      @tanstack/query-persist-client-core

      npm i https://pkg.pr.new/@tanstack/query-persist-client-core@11392

      @tanstack/query-sync-storage-persister

      npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@11392

      @tanstack/react-query

      npm i https://pkg.pr.new/@tanstack/react-query@11392

      @tanstack/react-query-devtools

      npm i https://pkg.pr.new/@tanstack/react-query-devtools@11392

      @tanstack/react-query-next-experimental

      npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@11392

      @tanstack/react-query-persist-client

      npm i https://pkg.pr.new/@tanstack/react-query-persist-client@11392

      @tanstack/solid-query

      npm i https://pkg.pr.new/@tanstack/solid-query@11392

      @tanstack/solid-query-devtools

      npm i https://pkg.pr.new/@tanstack/solid-query-devtools@11392

      @tanstack/solid-query-persist-client

      npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@11392

      @tanstack/svelte-query

      npm i https://pkg.pr.new/@tanstack/svelte-query@11392

      @tanstack/svelte-query-devtools

      npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@11392

      @tanstack/svelte-query-persist-client

      npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@11392

      @tanstack/vue-query

      npm i https://pkg.pr.new/@tanstack/vue-query@11392

      @tanstack/vue-query-devtools

      npm i https://pkg.pr.new/@tanstack/vue-query-devtools@11392

      commit: 0a73db4

    5. TkDodo merged this3221525

    6. TkDodo closed this

Sign in to join this conversation.