Skip to content
Source
Sign inSign up
TanStack/query

fix(broadcast-client): handle unhandled postMessage rejections #10771

Mergedn-satoshi061(GitHub profile, opens in a new tab)merged 11 commits intomain fromrefs/pull/10771/headmerged by TkDodo View on GitHub

Summary

Fixes #10543

broadcastQueryClient calls channel.postMessage() three times (on updated, removed, added) without .catch()-ing the returned promise. When query data contains a non-cloneable value (ReadableStream, Response, File, functions, Vue/MobX reactive proxies, etc.), the rejection becomes an unhandled DataCloneError with a stack pointing into node_modules — effectively unactionable in Sentry/Datadog.

Changes

  • Wrap all channel.postMessage() calls in a safePost() helper that catches rejections
  • Add optional onBroadcastError callback to BroadcastQueryClientOptions so consumers can route errors to their own error tracker with the offending queryHash
  • Fall back to console.warn in development when onBroadcastError is not provided
  • Silent in production (no unhandled rejection, no console noise)

Usage

broadcastQueryClient({
  queryClient,
  broadcastChannel: 'my-app',
  onBroadcastError: (error, message) => {
    Sentry.captureException(error, {
      extra: { queryHash: message.queryHash, type: message.type },
    })
  },
})

Test plan

  • onBroadcastError is called with the error and message when postMessage rejects
  • console.warn is emitted in development when no onBroadcastError is provided
  • No warning in production when postMessage fails
  • Existing tests still pass
  • ESLint passes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Prevented unhandled rejections when cross-tab broadcasts fail due to non-serializable data.
    • Failed synchronization now skips only the affected query while other broadcasts continue.
  • New Features

    • Added an optional broadcast-error callback with event details for custom error reporting.
    • Development builds provide console warnings when no error handler is configured.
    • Reduced broadcast payload size for removed items.
  • Documentation

    • Added guidance for handling broadcast errors.
  • Tests

    • Expanded coverage for failures, callbacks, rejected handlers, and warnings.
    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: defaults

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: f6bcdf99-3e41-4fd2-a9de-8c771bf6854e

      📥 Commits

      Reviewing files that changed from the base of the PR and between b0e7d7dca1d217197b8a784de415dfe5d6155414 and 3e3623e2affd1352602bc7c1d9b0afc37078d91b.

      📒 Files selected for processing (1)
      • docs/framework/react/plugins/broadcastQueryClient.md
      🚧 Files skipped from review as they are similar to previous changes (1)
      • docs/framework/react/plugins/broadcastQueryClient.md

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


      📝 Walkthrough

      Walkthrough

      broadcastQueryClient now routes rejected BroadcastChannel.postMessage calls through safePost. It supports onBroadcastError, catches callback failures, and warns in development. Tests, documentation, and release metadata cover the behavior.

      Changes

      Broadcast error handling and recovery

      Layer / File(s)Summary
      Options interface and callback setup
      packages/query-broadcast-client-experimental/src/index.ts
      Adds BroadcastErrorEvent and the optional onBroadcastError callback.
      Safe post helper with error handling
      packages/query-broadcast-client-experimental/src/index.ts
      Handles send failures, callback errors, promise rejections, and development-only warnings.
      Broadcast integration
      packages/query-broadcast-client-experimental/src/index.ts
      Routes updated, removed, and added messages through safePost. Updated and added messages include state; removed messages do not.
      Tests
      packages/query-broadcast-client-experimental/src/__tests__/index.test.ts
      Tests callback metadata, callback failure handling, and development versus production warning behavior.
      Documentation and release metadata
      docs/framework/react/plugins/broadcastQueryClient.md, .changeset/fix-broadcast-client-unhandled-rejection.md
      Documents the API and failure behavior, and adds patch release metadata.

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Merge Risk: ⚪ Minimal · up to 3e362

      The change contains broadcast message rejection handling and optional error reporting without introducing a supported merge-blocking risk; no actionable merge-blocking risk remains after normal checks and review.

      Sequence Diagram(s)

      sequenceDiagram
        participant QueryCache
        participant BroadcastQueryClient
        participant BroadcastChannel
        participant ErrorHandler
        QueryCache->>BroadcastQueryClient: query update, add, or removal
        BroadcastQueryClient->>BroadcastChannel: safePost(message)
        BroadcastChannel--xBroadcastQueryClient: postMessage rejection
        BroadcastQueryClient->>ErrorHandler: onBroadcastError(error, event)
        ErrorHandler-->>BroadcastQueryClient: completes, throws, or rejects

      Possibly related PRs

      Suggested labels: documentation

      🚥 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%.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 handling unhandled postMessage rejections, which is the primary change.
      Description check✅ PassedThe description clearly explains the motivation, implementation, usage, and test plan, although it does not use every template heading.
      Linked Issues check✅ PassedThe changes address issue #10543 by catching postMessage failures, reporting query context, preventing unhandled rejections, and testing environment-specific behavior.
      Out of Scope Changes check✅ PassedThe implementation, tests, documentation, and changeset all support the linked issue and stated broadcast error-handling objectives.
      ✨ 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. Actionable comments posted: 1

      🧹 Nitpick comments (1)
      packages/query-broadcast-client-experimental/src/__tests__/index.test.ts (1)

      60-78: ⚡ Quick win

      Add a regression test for onBroadcastError throwing.

      Please add a case where onBroadcastError throws, and assert no unhandled rejection escapes. That protects the error-handling contract end-to-end.

      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`
      around lines 60 - 78, Add a test that simulates postMessage failing and the
      supplied onBroadcastError itself throwing: mockPostMessage to reject with the
      DOMException as before, pass an onBroadcastError mock that throws (e.g. throw
      new Error('boom')), register a temporary process.on('unhandledRejection')
      handler that fails the test if invoked, call broadcastQueryClient({ queryClient,
      broadcastChannel: 'test_channel', onBroadcastError }), trigger the broadcast via
      queryClient.setQueryData(['test'], { value: 1 }), await a microtask tick (e.g.
      setTimeout 0 or next tick), then remove the unhandledRejection handler and
      assert that onBroadcastError was called with the cloneError and that no
      unhandled rejection handler fired.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Inline comments:
      In `@packages/query-broadcast-client-experimental/src/index.ts`:
      - Around line 41-43: The postMessage rejection handler currently calls
      onBroadcastError(error, message) directly which can itself throw and convert the
      rejection handler into an unhandled rejection; update the catch path that
      contains the onBroadcastError invocation (in
      packages/query-broadcast-client-experimental/src/index.ts — the block that
      checks if (onBroadcastError) { onBroadcastError(error, message) } else if
      (process.env.NODE_ENV !== 'production') { ... }) to invoke onBroadcastError
      inside a local try/catch: call onBroadcastError(error, message) in the try, and
      if it throws swallow or log that secondary error (fallback to the existing
      console.warn behavior) so the original postMessage rejection handling remains
      safe and cannot be escalated by a throwing callback.
      
      ---
      
      Nitpick comments:
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`:
      - Around line 60-78: Add a test that simulates postMessage failing and the
      supplied onBroadcastError itself throwing: mockPostMessage to reject with the
      DOMException as before, pass an onBroadcastError mock that throws (e.g. throw
      new Error('boom')), register a temporary process.on('unhandledRejection')
      handler that fails the test if invoked, call broadcastQueryClient({ queryClient,
      broadcastChannel: 'test_channel', onBroadcastError }), trigger the broadcast via
      queryClient.setQueryData(['test'], { value: 1 }), await a microtask tick (e.g.
      setTimeout 0 or next tick), then remove the unhandledRejection handler and
      assert that onBroadcastError was called with the cloneError and that no
      unhandled rejection handler fired.
      🪄 Autofix (Beta)

      Fix all unresolved CodeRabbit comments on this PR:

      • Push a commit to this branch (recommended)
      • Create a new PR with the fixes

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 52ee48f6-f465-4bac-8951-d52f60b62ab6

      📥 Commits

      Reviewing files that changed from the base of the PR and between ba6e7beebd50143408f01fcf5d9aee2ec1486f60 and 0893f0d95892512df2a6fb57444b7ca4951530ee.

      📒 Files selected for processing (3)
      • .changeset/fix-broadcast-client-unhandled-rejection.md
      • packages/query-broadcast-client-experimental/src/__tests__/index.test.ts
      • packages/query-broadcast-client-experimental/src/index.ts
    3. Actionable comments posted: 1

      🧹 Nitpick comments (1)
      packages/query-broadcast-client-experimental/src/__tests__/index.test.ts (1)

      60-91: ⚡ Quick win

      Add a regression test for rejected async handlers.

      This test covers sync throw, but a Promise-rejecting onBroadcastError path is different and worth locking down with a dedicated case.

      Suggested additional test
      +    it('should not cause an unhandled rejection when onBroadcastError returns a rejected promise', async () => {
      +      const cloneError = new DOMException('DataCloneError', 'DataCloneError')
      +      mockPostMessage.mockRejectedValueOnce(cloneError)
      +
      +      const unhandledRejections: Array<unknown> = []
      +      const onUnhandledRejection = (reason: unknown) => {
      +        unhandledRejections.push(reason)
      +      }
      +      process.on('unhandledRejection', onUnhandledRejection)
      +
      +      const onBroadcastError = vi.fn().mockRejectedValue(new Error('boom-async'))
      +
      +      try {
      +        broadcastQueryClient({
      +          queryClient,
      +          broadcastChannel: 'test_channel',
      +          onBroadcastError,
      +        })
      +
      +        queryClient.setQueryData(['test-async'], { value: 1 })
      +        await new Promise((r) => setTimeout(r, 0))
      +
      +        expect(onBroadcastError).toHaveBeenCalledWith(
      +          cloneError,
      +          expect.objectContaining({ type: 'added' }),
      +        )
      +        expect(unhandledRejections).toHaveLength(0)
      +      } finally {
      +        process.off('unhandledRejection', onUnhandledRejection)
      +      }
      +    })
      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`
      around lines 60 - 91, Add a regression test that mirrors the existing
      "onBroadcastError throws" case but instead uses an async/rejecting handler:
      create an onBroadcastError stub that returns a rejected Promise (e.g.
      vi.fn().mockRejectedValue(new Error('boom'))), call broadcastQueryClient({
      queryClient, broadcastChannel: 'test_channel', onBroadcastError }), trigger a
      broadcast via queryClient.setQueryData(['test'], { value: 1 }), capture process
      'unhandledRejection' events into an array, wait a tick (setTimeout 0 or await
      Promise.resolve()) then assert the mock was called with the cloneError and
      expected message and that no unhandledRejections were recorded; reference
      onBroadcastError, broadcastQueryClient, and queryClient.setQueryData when
      locating where to add the test.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Inline comments:
      In `@packages/query-broadcast-client-experimental/src/index.ts`:
      - Around line 42-51: safePost currently calls onBroadcastError(...) inside a
      synchronous try/catch which misses async rejections; update safePost to capture
      the return value of onBroadcastError (e.g., const result =
      onBroadcastError(error, message)) and handle both sync and async errors by
      calling Promise.resolve(result).catch(err => { if (process.env.NODE_ENV !==
      'production') console.warn(`[broadcastQueryClient] failed to broadcast
      "${message.type}" for queryHash "${message.queryHash}":`, err); }); so async
      callbacks are properly caught and logged without causing unhandledRejection.
      
      ---
      
      Nitpick comments:
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`:
      - Around line 60-91: Add a regression test that mirrors the existing
      "onBroadcastError throws" case but instead uses an async/rejecting handler:
      create an onBroadcastError stub that returns a rejected Promise (e.g.
      vi.fn().mockRejectedValue(new Error('boom'))), call broadcastQueryClient({
      queryClient, broadcastChannel: 'test_channel', onBroadcastError }), trigger a
      broadcast via queryClient.setQueryData(['test'], { value: 1 }), capture process
      'unhandledRejection' events into an array, wait a tick (setTimeout 0 or await
      Promise.resolve()) then assert the mock was called with the cloneError and
      expected message and that no unhandledRejections were recorded; reference
      onBroadcastError, broadcastQueryClient, and queryClient.setQueryData when
      locating where to add the test.
      🪄 Autofix (Beta)

      Fix all unresolved CodeRabbit comments on this PR:

      • Push a commit to this branch (recommended)
      • Create a new PR with the fixes

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 2357c389-bb7f-4815-9fcc-0db5fcf17887

      📥 Commits

      Reviewing files that changed from the base of the PR and between 0893f0d95892512df2a6fb57444b7ca4951530ee and 71ceeeaf22dff7e439acf8884b2b79ff53aa49fd.

      📒 Files selected for processing (2)
      • packages/query-broadcast-client-experimental/src/__tests__/index.test.ts
      • packages/query-broadcast-client-experimental/src/index.ts
    4. 🧹 Nitpick comments (1)
      packages/query-broadcast-client-experimental/src/__tests__/index.test.ts (1)

      101-118: ⚡ Quick win

      Ensure the unhandledRejection listener is always cleaned up.

      If this test throws before Line 117, the listener can leak into later tests. Wrap the body in try/finally and move process.off(...) to finally.

      Proposed fix
             process.on('unhandledRejection', onUnhandledRejection)
      
             const onBroadcastError = vi
               .fn()
               .mockRejectedValueOnce(new Error('async boom'))
      
      -      broadcastQueryClient({
      -        queryClient,
      -        broadcastChannel: 'test_channel',
      -        onBroadcastError,
      -      })
      -
      -      queryClient.setQueryData(['test'], { value: 1 })
      -
      -      await new Promise((r) => setTimeout(r, 10))
      -
      -      process.off('unhandledRejection', onUnhandledRejection)
      -
      -      expect(onBroadcastError).toHaveBeenCalledWith(
      -        cloneError,
      -        expect.objectContaining({ type: 'added' }),
      -      )
      -      expect(unhandledRejections).toHaveLength(0)
      +      try {
      +        broadcastQueryClient({
      +          queryClient,
      +          broadcastChannel: 'test_channel',
      +          onBroadcastError,
      +        })
      +
      +        queryClient.setQueryData(['test'], { value: 1 })
      +
      +        await new Promise((r) => setTimeout(r, 10))
      +
      +        expect(onBroadcastError).toHaveBeenCalledWith(
      +          cloneError,
      +          expect.objectContaining({ type: 'added' }),
      +        )
      +        expect(unhandledRejections).toHaveLength(0)
      +      } finally {
      +        process.off('unhandledRejection', onUnhandledRejection)
      +      }
      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`
      around lines 101 - 118, The test registers an unhandledRejection listener
      (onUnhandledRejection) but only removes it after awaited work, which can leak if
      the test throws; wrap the test body that calls broadcastQueryClient({
      queryClient, broadcastChannel: 'test_channel', onBroadcastError }) and the
      subsequent queryClient.setQueryData/await in a try/finally and move
      process.off('unhandledRejection', onUnhandledRejection) into the finally block
      so the listener is always cleaned up even on failures; keep the same
      onBroadcastError mock and onUnhandledRejection setup/teardown structure.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Nitpick comments:
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`:
      - Around line 101-118: The test registers an unhandledRejection listener
      (onUnhandledRejection) but only removes it after awaited work, which can leak if
      the test throws; wrap the test body that calls broadcastQueryClient({
      queryClient, broadcastChannel: 'test_channel', onBroadcastError }) and the
      subsequent queryClient.setQueryData/await in a try/finally and move
      process.off('unhandledRejection', onUnhandledRejection) into the finally block
      so the listener is always cleaned up even on failures; keep the same
      onBroadcastError mock and onUnhandledRejection setup/teardown structure.

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: f614ddbd-4a69-46da-979b-42cb682551ca

      📥 Commits

      Reviewing files that changed from the base of the PR and between 71ceeeaf22dff7e439acf8884b2b79ff53aa49fd and 062cba086abc0432fd19f988553c5bde09fa22b7.

      📒 Files selected for processing (2)
      • packages/query-broadcast-client-experimental/src/__tests__/index.test.ts
      • packages/query-broadcast-client-experimental/src/index.ts
    5. Fixed in the latest commit: wrapped both unhandledRejection listener tests in try/finally so process.off() is always called even if an assertion throws.

    6. Actionable comments posted: 1

      Caution

      Some comments are outside the diff and can’t be posted inline due to platform limitations.

      ⚠️ Outside diff range comments (1)
      packages/query-broadcast-client-experimental/src/__tests__/index.test.ts (1)

      163-204: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

      Restore console.warn with try/finally to avoid spy leakage.

      If an assertion fails before mockRestore(), the spy can leak into later tests and cause cascading failures.

      Proposed fix
       it('should warn in dev when postMessage fails and onBroadcastError is not provided', async () => {
         process.env['NODE_ENV'] = 'development'
         const cloneError = new DOMException('DataCloneError', 'DataCloneError')
         mockPostMessage.mockRejectedValueOnce(cloneError)
      
         const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
      
      -  broadcastQueryClient({
      -    queryClient,
      -    broadcastChannel: 'test_channel',
      -  })
      -
      -  queryClient.setQueryData(['test'], { value: 1 })
      -
      -  await new Promise((r) => setTimeout(r, 0))
      -  expect(warnSpy).toHaveBeenCalledWith(
      -    expect.stringContaining('cross-tab sync for this query was skipped'),
      -    cloneError,
      -  )
      -
      -  warnSpy.mockRestore()
      +  try {
      +    broadcastQueryClient({
      +      queryClient,
      +      broadcastChannel: 'test_channel',
      +    })
      +
      +    queryClient.setQueryData(['test'], { value: 1 })
      +
      +    await new Promise((r) => setTimeout(r, 0))
      +    expect(warnSpy).toHaveBeenCalledWith(
      +      expect.stringContaining('cross-tab sync for this query was skipped'),
      +      cloneError,
      +    )
      +  } finally {
      +    warnSpy.mockRestore()
      +  }
       })
      
       it('should not warn in production when postMessage fails', async () => {
         process.env['NODE_ENV'] = 'production'
         const cloneError = new DOMException('DataCloneError', 'DataCloneError')
         mockPostMessage.mockRejectedValueOnce(cloneError)
      
         const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
      
      -  broadcastQueryClient({
      -    queryClient,
      -    broadcastChannel: 'test_channel',
      -  })
      -
      -  queryClient.setQueryData(['test'], { value: 1 })
      -
      -  await new Promise((r) => setTimeout(r, 0))
      -  expect(warnSpy).not.toHaveBeenCalled()
      -
      -  warnSpy.mockRestore()
      +  try {
      +    broadcastQueryClient({
      +      queryClient,
      +      broadcastChannel: 'test_channel',
      +    })
      +
      +    queryClient.setQueryData(['test'], { value: 1 })
      +
      +    await new Promise((r) => setTimeout(r, 0))
      +    expect(warnSpy).not.toHaveBeenCalled()
      +  } finally {
      +    warnSpy.mockRestore()
      +  }
       })
      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`
      around lines 163 - 204, Wrap the console.warn spy setup and assertions in a
      try/finally so the spy is always restored even if an assertion fails;
      specifically, in the two tests ("should warn in dev when postMessage fails and
      onBroadcastError is not provided" and "should not warn in production when
      postMessage fails") move the creation of warnSpy (vi.spyOn(console,
      'warn').mockImplementation(() => {})) before the action, perform the
      broadcastQueryClient(...) and queryClient.setQueryData(...) and the expect(...)
      inside a try block, and call warnSpy.mockRestore() in the finally block to
      ensure the spy is cleaned up; keep references to mockPostMessage,
      broadcastQueryClient, queryClient, and warnSpy to locate the changes.
      🧹 Nitpick comments (2)
      docs/framework/react/plugins/broadcastQueryClient.md (1)

      35-37: ⚡ Quick win

      Update the API call snippet to reflect the new option.

      The snippet still shows only queryClient and broadcastChannel; consider including onBroadcastError (or ...options) so the top-level API example matches the expanded options surface.

      Suggested doc tweak
      -broadcastQueryClient({ queryClient, broadcastChannel })
      +broadcastQueryClient({ queryClient, broadcastChannel, onBroadcastError })
      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@docs/framework/react/plugins/broadcastQueryClient.md` around lines 35 - 37,
      Update the top-level API snippet for broadcastQueryClient to show the new
      options surface by including the onBroadcastError option (or a spread like
      ...options) in the call signature; change the example invocation of
      broadcastQueryClient({ queryClient, broadcastChannel }) to include
      onBroadcastError (or ...options) so it matches the expanded options in the
      implementation and docs.
      packages/query-broadcast-client-experimental/src/__tests__/index.test.ts (1)

      139-161: ⚡ Quick win

      Add rejection-path coverage for updated and removed events.

      The new failure-path assertions currently validate only the 'added' event shape. Since safePost is used across outbound message types, add one rejection test each for 'updated' and 'removed' to lock the event contract and prevent branch-specific regressions.

      🤖 Prompt for AI Agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`
      around lines 139 - 161, Add rejection-path tests for the other message types:
      duplicate the existing test that mocks mockPostMessage to reject (using the same
      cloneError and mockRejectedValueOnce) and assert onBroadcastError is called with
      that error and an event object whose type is 'updated' (triggered by updating an
      existing query via queryClient.setQueryData on the same key) and another test
      where type is 'removed' (triggered by removing the query via
      queryClient.removeQueries or setting data to undefined). Use the same helpers
      used in the current test: mockPostMessage, broadcastQueryClient({... ,
      onBroadcastError }), and expect.objectContaining<BroadcastErrorEvent> to
      validate queryHash and queryKey shape so the failure path of safePost is covered
      for 'updated' and 'removed'.
      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Inline comments:
      In `@packages/query-broadcast-client-experimental/src/index.ts`:
      - Line 19: Outbound "'added'" broadcast messages omit the source query state;
      update the message shape to include a state field and ensure creators populate
      it so receivers (the inbound 'added' handler that reads state) get the correct
      data. Specifically, extend the 'added' payload type to include state (alongside
      queryHash and queryKey) and modify the places that construct/send the 'added'
      message (the two sites flagged around the existing message constructors) to pass
      the source query's state (e.g., use the query's dehydrated/state value when
      building the message) so the emitter and receiver contracts match.
      
      ---
      
      Outside diff comments:
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`:
      - Around line 163-204: Wrap the console.warn spy setup and assertions in a
      try/finally so the spy is always restored even if an assertion fails;
      specifically, in the two tests ("should warn in dev when postMessage fails and
      onBroadcastError is not provided" and "should not warn in production when
      postMessage fails") move the creation of warnSpy (vi.spyOn(console,
      'warn').mockImplementation(() => {})) before the action, perform the
      broadcastQueryClient(...) and queryClient.setQueryData(...) and the expect(...)
      inside a try block, and call warnSpy.mockRestore() in the finally block to
      ensure the spy is cleaned up; keep references to mockPostMessage,
      broadcastQueryClient, queryClient, and warnSpy to locate the changes.
      
      ---
      
      Nitpick comments:
      In `@docs/framework/react/plugins/broadcastQueryClient.md`:
      - Around line 35-37: Update the top-level API snippet for broadcastQueryClient
      to show the new options surface by including the onBroadcastError option (or a
      spread like ...options) in the call signature; change the example invocation of
      broadcastQueryClient({ queryClient, broadcastChannel }) to include
      onBroadcastError (or ...options) so it matches the expanded options in the
      implementation and docs.
      
      In `@packages/query-broadcast-client-experimental/src/__tests__/index.test.ts`:
      - Around line 139-161: Add rejection-path tests for the other message types:
      duplicate the existing test that mocks mockPostMessage to reject (using the same
      cloneError and mockRejectedValueOnce) and assert onBroadcastError is called with
      that error and an event object whose type is 'updated' (triggered by updating an
      existing query via queryClient.setQueryData on the same key) and another test
      where type is 'removed' (triggered by removing the query via
      queryClient.removeQueries or setting data to undefined). Use the same helpers
      used in the current test: mockPostMessage, broadcastQueryClient({... ,
      onBroadcastError }), and expect.objectContaining<BroadcastErrorEvent> to
      validate queryHash and queryKey shape so the failure path of safePost is covered
      for 'updated' and 'removed'.
      🪄 Autofix (Beta)

      Fix all unresolved CodeRabbit comments on this PR:

      • Push a commit to this branch (recommended)
      • Create a new PR with the fixes

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: ac035baa-9615-4fe3-b1ba-af6c95bf3b11

      📥 Commits

      Reviewing files that changed from the base of the PR and between 5b5339006a11ea8be53f801d9ee33a04e2a72b95 and ff1754938bad92eefd765a6247d68236dcf01d66.

      📒 Files selected for processing (3)
      • docs/framework/react/plugins/broadcastQueryClient.md
      • packages/query-broadcast-client-experimental/src/__tests__/index.test.ts
      • packages/query-broadcast-client-experimental/src/index.ts
    7. Update summary

      After reviewing other PRs addressing the same issue (#10569, #10770, #10697) and the CodeRabbit feedback, I've made additional improvements beyond the initial unhandled-rejection fix. Here's a full summary of what's in the branch now.


      Changes from the original PR

      Types

      • Exported BroadcastErrorEvent interface (with queryKey: QueryKey) so consumers can import and reference the type directly:
        import type { BroadcastErrorEvent } from '@tanstack/query-broadcast-client-experimental'
      • Replaced the flat BroadcastMessage interface with a discriminated union — state is now only present on updated and added (not removed), which matches the actual wire protocol
      • onBroadcastError now receives BroadcastErrorEvent (no internal state field) rather than the raw message object
      • onBroadcastError return type is void | Promise<void> to explicitly support async callbacks

      Error handling in safePost

      CaseBehaviour
      postMessage rejectsonBroadcastError called with error + event; or dev console.warn if no callback
      onBroadcastError throws synchronouslyCaught by try/catch; dev console.warn with callback error
      onBroadcastError returns a rejecting PromiseCaught by result?.catch(); dev console.warn with callback error
      Production, no callbackSilent — no unhandled rejection, no console noise

      Warning messages

      Two distinct messages to make debugging easier:

      • Broadcast failure (no callback): [broadcastQueryClient] Failed to broadcast "X" event for query Y. The query value could not be structured-cloned; cross-tab sync for this query was skipped.
      • Callback itself failed: [broadcastQueryClient] onBroadcastError threw while handling "X" for query Y.

      Bug fix: state missing from 'added' broadcasts

      The receiver's onmessage handler reads state for added events (query.setState(state) / queryCache.build(..., state)), but the sender was omitting it — causing receiving tabs to apply undefined state. Fixed by including state in the outbound added message and updating the discriminated union accordingly.

      Other

      • Replaced queryClient.getQueryCache().subscribe() with queryCache.subscribe() (variable already in scope)
      • Added JSDoc to all BroadcastQueryClientOptions fields
      • Added "Handling broadcast errors" section to the docs with a Sentry example

      CodeRabbit review responses

      CommentResolution
      onBroadcastError throwing causes a new unhandled rejectionWrapped callback in try/catch; added test asserting zero unhandledRejection events when callback throws
      Async onBroadcastError rejecting causes unhandled rejectionExtended type to void | Promise<void>; added result?.catch() guard; added test for async rejection path
      'added' broadcasts omit state, breaking receiver contractAdded state to the added discriminated union variant and to the safePost call site; confirmed with existing tests

      Test coverage (9 tests)

      • subscribe / unsubscribe (existing)
      • onBroadcastError called when postMessage rejects
      • No unhandled rejection when callback throws synchronously
      • Dev console.warn emitted when callback throws (asserts message contains "onBroadcastError threw while handling")
      • No unhandled rejection when async callback rejects
      • Dev console.warn emitted when async callback rejects
      • Dev console.warn emitted when no callback provided (asserts "cross-tab sync for this query was skipped")
      • No console.warn in production
    8. ✅ Actions performed

      Reviews resumed.

    9. Addressed in the latest commit (52d17622d).

      All four tests that call vi.spyOn(console, 'warn') now wrap their assertions in try/finally so mockRestore() is guaranteed to run even if an assertion fails — preventing the spy from leaking into subsequent tests.

      const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
      try {
        // ...test body and assertions...
      } finally {
        warnSpy.mockRestore()
      }

      Applied to:

      • should warn in dev when onBroadcastError itself throws
      • should warn in dev when async onBroadcastError rejects
      • should warn in dev when postMessage fails and onBroadcastError is not provided
      • should not warn in production when postMessage fails
    10. TkDodo approved these changes

    11. Note

      GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

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

      View your CI Pipeline Execution ↗ for commit 3e3623e2affd1352602bc7c1d9b0afc37078d91b

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

      ☁️ Nx Cloud last updated this comment at 2026-08-20 08:23:16 UTC

    13. pkg-pr-new[bot](GitHub profile, opens in a new tab)commented
      More templates

      @tanstack/angular-query-experimental

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

      @tanstack/eslint-plugin-query

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

      @tanstack/lit-query

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

      @tanstack/preact-query

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

      @tanstack/preact-query-devtools

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

      @tanstack/preact-query-persist-client

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

      @tanstack/query-async-storage-persister

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

      @tanstack/query-broadcast-client-experimental

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

      @tanstack/query-core

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

      @tanstack/query-devtools

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

      @tanstack/query-persist-client-core

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

      @tanstack/query-sync-storage-persister

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

      @tanstack/react-query

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

      @tanstack/react-query-devtools

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

      @tanstack/react-query-next-experimental

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

      @tanstack/react-query-persist-client

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

      @tanstack/solid-query

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

      @tanstack/solid-query-devtools

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

      @tanstack/solid-query-persist-client

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

      @tanstack/svelte-query

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

      @tanstack/svelte-query-devtools

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

      @tanstack/svelte-query-persist-client

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

      @tanstack/vue-query

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

      @tanstack/vue-query-devtools

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

      commit: 5f8172f

    14. TkDodo merged this3c79861

    15. TkDodo closed this

Sign in to join this conversation.