fix(query-broadcast-client-experimental): keep error state of settled queries on added broadcasts #12704
main fromrefs/pull/12704/head·opened View on GitHubChanges#
When another tab creates a query, the receiving tab applied the sender's initial state on added whenever its own query had no data. A query that already settled in error has no data either, so it was replaced with the sender's brand-new state: status: 'pending', error: null, errorUpdatedAt: 0. The error was lost and observers rendered a pending UI indefinitely without any fetch running, exactly as described in the issue.
added messages carry the sender's initial state at creation time, so adopting them is only ever meaningful for a query that has not settled yet. The receiver now also requires errorUpdatedAt === 0 before adopting, so a query that already failed keeps its error state.
Fixes #12213
Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with the package's vitest suite (the full
pnpm run test:prmatrix does not apply to this single-package change and was not run). - I have followed the AI contribution policy and 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 (patch on
@tanstack/query-broadcast-client-experimental).
Verification#
- New regression test: an error-settled query with no data keeps its error state when an
addedmessage with a fresh pending state arrives. - The regression test fails on current
main(1 failed, 13 passed) and passes with the fix (14 passed,vitest runfor@tanstack/query-broadcast-client-experimental). - The two pre-existing
added-handling tests still pass: queries with data are never overwritten, and unsettled queries with no data still adopt the sender's state. - Size limit comparison: pending/unavailable locally; the change is one extra numeric comparison in the broadcast message handler of the experimental broadcast package.
Summary by CodeRabbit#
- Bug Fixes
- Fixed an issue where an incoming query update could replace an existing error state when the query had no data. The error status and message are now preserved when the local query has already settled with an error, preventing the displayed state from incorrectly changing to pending.
- 1
draarivpatel-ui(GitHub profile, opens in a new tab) linked this to fix #12213
tanstack-source added labelsbug
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
No actionable comments were generated in the recent review.
Walkthrough
-
When an existing query has no data but has a recorded error, the broadcast client now keeps its local state after receiving an
addedmessage. A regression test verifies that the error status and value remain unchanged.Changes: Added broadcast state handling
Layer / File(s) Summary Guard added-state adoption
packages/query-broadcast-client-experimental/src/index.ts,packages/query-broadcast-client-experimental/src/__tests__/index.test.ts,.changeset/broadcast-added-error-state.mdThe receiver adopts an incoming addedstate only when its query has no data anderrorUpdatedAtis zero. The test checks that a query with an existing error keeps its status and error value. The changeset declares a patch release.Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Possibly related PRs
- TanStack/query#12563: Addresses the same issue by changing how the broadcast client handles incoming
addedstates.
Suggested reviewers:
lazerg,sukvvon - TanStack/query#12563: Addresses the same issue by changing how the broadcast client handles incoming
Merge Risk:
Minimal · up to56d94-
This patch keeps a query's recorded error from being overwritten by an incoming added message that would leave it pending with no fetch running. It is a small, well-tested change in an experimental package, so merge-readiness risk is minimal.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the package and the fix: error state of settled queries is kept when an added broadcast arrives. This matches the main change in index.ts and the added test. The title is 97 characters… Description check The description follows the template. It explains the bug and the fix, and it links issue #12213. It lists the checklist items and the release impact with a changeset. It also adds a verification sect…Linked Issues check Issue #12213requires that anaddedmessage not overwrite a receiving query that has no data but has settled in error. At head,packages/query-broadcast-client-experimental/src/index.tschanges th…Out of Scope Changes check The reviewed changes are limited to the addedbranch guard insrc/index.ts(one added condition with a comment), a regression test insrc/__tests__/index.test.ts, and a patch changeset for `@tan…Full details: Title check
-
Explanation
The title names the package and the fix: error state of settled queries is kept when an added broadcast arrives. This matches the main change in index.ts and the added test. The title is 97 characters, which is long, but it stays specific and on topic.
Full details: Description check
-
Explanation
The description follows the template. It explains the bug and the fix, and it links issue
#12213. It lists the checklist items and the release impact with a changeset. It also adds a verification section with test results. The Changes section states the motivation clearly. The description is mostly complete, so the check passes.
Full details: Linked Issues check
-
Explanation
Issue
#12213requires that anaddedmessage not overwrite a receiving query that has no data but has settled in error. At head,packages/query-broadcast-client-experimental/src/index.tschanges theaddedbranch to callquery.setState(state)only whenquery.state.data === undefined && query.state.errorUpdatedAt === 0. A receiver with a recorded error (errorUpdatedAt > 0) keeps its error state. The issue's suggested fix (apply only when the incoming state hasdataUpdatedAt > 0orerrorUpdatedAt > 0) is a different guard. The PR uses the receiver-side guard that the issue describes as the stricter freshness-style option. The sender'saddedstate is always the initial state at build time, so the receiver-side guard covers the reported case. The existing behavior for receivers with data (#11391) is unchanged. The summary and the AI-generated summary report a regression test for the error-settled case and two existingaddedtests. I did not run or read the test file, so the test evidence rests on the PR description and summary. Remainingaddedbehavior for receivers that are still pending withfetchStatusset is unchanged and is not part of#12213.
Full details: Out of Scope Changes check
-
Explanation
The reviewed changes are limited to the
addedbranch guard insrc/index.ts(one added condition with a comment), a regression test insrc/__tests__/index.test.ts, and a patch changeset for@tanstack/query-broadcast-client-experimental. All three directly support the fix for#12213. No public API or unrelated behavior changed according to the diff and summary. I did not inspect the test file or changeset contents directly.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/query
- Branch: main
- File: .coderabbit.yaml
- Commit: 18b1ade
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
92a2e14d-a2b6-4522-9c41-b253ce893603
Commits-
Reviewing files that changed from the base of the PR and between 18b1aded329058c074cda6b1c1a08aba73fdc914 and 56d94f4d955ede7450b496329c830805d8451cb3.
Files selected for processing: 3-
.changeset/broadcast-added-error-state.mdpackages/query-broadcast-client-experimental/src/__tests__/index.test.tspackages/query-broadcast-client-experimental/src/index.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
-
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 56d94f4d955ede7450b496329c830805d8451cb3
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 26 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR