Skip to content
Source
Sign inSign up
TanStack/query

[vue-query] broadcast client + nuxt DataCloneError #8356

Closedhaakonmt(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

When using the broadcast client with Nuxt (v3.14.1592), some actions, such as invalidations, will not be broadcasted correctly, seemingly due to the payload sent with postMessage not being serializable.

I have provided a screenshot showing the Firefox debugger with a breakpoint at postMessage. It shows that the data.queryKey field of the payload is a Proxy, which is probably likely to be the culprit?

Your minimal, reproducible example

https://stackblitz.com/edit/github-scuhgu?file=app.vue

Steps to reproduce

  1. Run the app in StackBlitz
  2. Open the console
  3. Click "Add value"
  4. Observe that there are errors in the console

Expected behavior

As a user, I expect the postMessage call to succeed, but it does not.

How often does this bug happen?

Every time

Screenshots or Videos

Error message in Chrome Screenshot from Firefox showing how the queryKey is a Proxy

Platform

  • OS: MacOS Sonoma 14.6.1
  • Browser: Chrome/Firefox
  • Version: 131.0.6778.86/133.0b9 respectively

Tanstack Query adapter

vue-query

TanStack Query version

5.61.4

TypeScript version

5.7.2

Additional context

No response

  1. DamianOsipiuk(GitHub profile, opens in a new tab) added labelspackage: vue-query

  2. You might consider modifying vueQueryState.value to toRaw(vueQueryState.value) during hydration. This approach could potentially resolve your issue:

    if (import.meta.client) {
      broadcastQueryClient({ queryClient });
      // hydrate(queryClient, vueQueryState.value);
      hydrate(queryClient, toRaw(vueQueryState.value));
    }
  3. @Mini-ghost Thank you, this does the trick it seems! 😄 Do you think this technique has any potential downsides? I'm just wondering since this is not part of the example in the docs.

  4. Mini-ghost(GitHub profile, opens in a new tab)commented

    @haakonmt I believe this should not have any potential downsides. The vueQueryState.value here is a Vue reactive object, so we observe that the queryKey at this point is a Proxy object, which causes postMessage to malfunction. The toRaw method allows us to retrieve the original object behind the reactive proxy, which is a plain object. This approach resolves the issue of postMessage being unable to handle Proxy objects.

    The following approach can also address this issue and may offer slightly better performance:

    if (import.meta.server) {
      nuxt.hooks.hook('app:rendered', () => {
        nuxt.payload.query = dehydrate(queryClient);
      });
    }
    
    if (import.meta.client) {
      broadcastQueryClient({ queryClient });
      hydrate(queryClient, nuxt.payload.query);
    }

Sign in to join this conversation.