Skip to content
Source
Sign inSign up
TanStack/router

using setupRouterSsrQueryIntegration let ssr never finish with renderRouterToString #5155

Send to my agent
Closedsimllll(GitHub profile, opens in a new tab) opened this issue 3 comments

Which project does this relate to?

Router

Describe the bug

When using setupRouterSsrQueryIntegration with renderRouterToString the server never responses. It hangs forver.

It seems some promises never resolve, I debugged it a bit, and found that https://github.com/TanStack/router/blob/1405da71edf9d7cc6d983770fccd0126ddd18e2a/packages/router-core/src/ssr/ssr-server.ts#L118 is never resolved. To verify my thesis, i added a "manuel p.resolve()" in onSerialize() and the server starts working again!

when I add a console log for the content of dehydratedRouter:

Image

I get:

crossSerializeStream start {
  manifest: undefined,
  matches: [
    { i: '__root__', u: 1758056689675, s: 'success', ssr: true },
    { i: '/', u: 1758056689699, s: 'success', l: [Object], ssr: true }
  ],
  lastMatchId: '/',
  dehydratedData: {
    queryStream: ReadableStream { locked: false, state: 'readable', supportsBYOB: false }
  }
}

Your Example Website or App

https://vocal-moxie-902b3d.netlify.app/

Steps to Reproduce the Bug or Issue

  1. Fork the example "basic-ssr-file-based" (https://tanstack.com/router/v1/docs/framework/react/examples/basic-ssr-file-based)
  2. apply these changes: https://github.com/simllll/tanstack-router-test/commit/bdbee23b89ef9fbda47557b05073dfc40fd57fba

Call "http://localhost:3000" via curl or browser, and you will see you never get a response.

Expected behavior

The server should work as before, I don't even use "useQuery" in the example at all, I just added the plugin (renderRouterToString) and it stops working.

Screenshots or Videos

No response

Platform

  • Router Version: 1.131.44
  • OS: Linux
  • Browser: not relevant, but Chrome and "curl"
  • Browser Version: curl 8.5.0, chrome Version 136.0.7103.113 (Offizieller Build) (64-Bit)
  • Bundler: vite with express middleware
  • Bundler Version: vite 6.3.6+ / latest tested version: 7.1.5

Additional context

For me it seems the issue lies in seroval's crossSerializeStream(), I couldn't find a documentation for "onDone", but saw other examples with it.. I'm not sure why, but in my case the onDone is never called.

Other notes:

  • maybe we could add a "timeout" promise with a race, to not run into endless promises in this case also for the future?
  1. With renderRouterToStream() it seems to work fine:

    Image

    Is it supposed to work only with the streaming method?

  2. Even though the server responses with the renderRouterToStream(), adding a useQueryClient() in the application results in (on server side):

    Error in renderToPipeableStream: Error: No QueryClient set, use QueryClientProvider to set one
        at useQueryClient (./node_modules/@tanstack/react-query/src/QueryClientProvider.tsx:32:52)

    shouldn't the setupRouterSsrQueryIntegration register a QueryClient? At least it looks like this if I check the code?!

    similar strange issue is happening in another app with TanStackRouterDevtools when it calls useRouter() hook, it doesn't get any value returned.

    Somehow the context is lost or not correclty assigned in some specific cases?! need to debug this further, as soon as I have solved this issue here ;-)

    Edit This is only happening in one of my apps, not in the test repo. I have no duplicte node_modules, and currently no clue where this issue is coming from. Any input appreciated to debug it. The error with the full stack trace which I get in the http response:

    No QueryClient set, use QueryClientProvider to set one" data-stck="Switched to client rendering because the server rendering errored:
    
    Error: No QueryClient set, use QueryClientProvider to set one
        at useQueryClient (./node_modules/@tanstack/react-query/src/QueryClientProvider.tsx:32:52)
        at useAuth (./client/src/api/use-api-auth.ts:41:23)
        at Header (./client/src/components/features/website/Header.tsx:38:28)
        at Object.react_stack_bottom_frame (./node_modules/react-dom/cjs/react-dom-server.node.development.js:9176:18)
        at renderWithHooks (./node_modules/react-dom/cjs/react-dom-server.node.development.js:4797:19)
        at renderElement (./node_modules/react-dom/cjs/react-dom-server.node.development.js:5232:23)
        at retryNode (./node_modules/react-dom/cjs/react-dom-server.node.development.js:5991:31)
        at renderNodeDestructive (./node_modules/react-dom/cjs/react-dom-server.node.development.js:5941:11)
        at renderNode (./node_modules/react-dom/cjs/react-dom-server.node.development.js:6398:18)
        at renderChildrenArray (./node_modules/react-dom/cjs/react-dom-server.node.development.js:6274:9)" data-cstck="
        at Header (./client/src/components/features/website/Header.tsx:25:85)
        at div (<anonymous>)
        at IndexPage (<anonymous>)
        at Lazy (<anonymous>)
        at MatchInnerImpl (./node_modules/@tanstack/react-router/dist/esm/Match.js:134:3)
        at SafeFragment (./node_modules/@tanstack/react-router/dist/esm/SafeFragment.js:7:110)
        at CatchBoundaryImpl (./node_modules/@tanstack/react-router/dist/esm/CatchBoundary.js:30:5)
        at CatchBoundary (./node_modules/@tanstack/react-router/dist/esm/CatchBoundary.js:10:32)
        at SafeFragment (./node_modules/@tanstack/react-router/dist/esm/SafeFragment.js:7:110)
        at SafeFragment (./node_modules/@tanstack/react-router/dist/esm/SafeFragment.js:7:110)
        at MatchImpl (./node_modules/@tanstack/react-router/dist/esm/Match.js:35:3)
        at Suspense (<anonymous>)
        at OutletImpl (./node_modules/@tanstack/react-router/dist/esm/Match.js:222:53)
        at div (<anonymous>)
        at body (<anonymous>)
        at html (<anonymous>)
        at RootComponent (<anonymous>)
        at MatchInnerImpl (./node_modules/@tanstack/react-router/dist/esm/Match.js:134:3)
        at SafeFragment (./node_modules/@tanstack/react-router/dist/esm/SafeFragment.js:7:110)
        at CatchBoundaryImpl (./node_modules/@tanstack/react-router/dist/esm/CatchBoundary.js:30:5)
        at CatchBoundary (./node_modules/@tanstack/react-router/dist/esm/CatchBoundary.js:10:32)
        at SafeFragment (./node_modules/@tanstack/react-router/dist/esm/SafeFragment.js:7:110)
        at SafeFragment (./node_modules/@tanstack/react-router/dist/esm/SafeFragment.js:7:110)
        at MatchImpl (./node_modules/@tanstack/react-router/dist/esm/Match.js:35:3)
        at CatchBoundaryImpl (./node_modules/@tanstack/react-router/dist/esm/CatchBoundary.js:30:5)
        at CatchBoundary (./node_modules/@tanstack/react-router/dist/esm/CatchBoundary.js:10:32)
        at MatchesInner (./node_modules/@tanstack/react-router/dist/esm/Matches.js:40:53)
        at SafeFragment (./node_modules/@tanstack/react-router/dist/esm/SafeFragment.js:7:110)
        at Matches (./node_modules/@tanstack/react-router/dist/esm/Matches.js:30:53)
        at QueryClientProvider (file://./node_modules/@tanstack/react-query/build/modern/QueryClientProvider.js:20:3)
        at opts.router.options.Wrap (file://./node_modules/@tanstack/react-router-ssr-query/dist/esm/index.js:11:33)
        at RouterContextProvider (./node_modules/@tanstack/react-router/dist/esm/RouterProvider.js:12:3)
        at RouterProvider (./node_modules/@tanstack/react-router/dist/esm/RouterProvider.js:33:27)
        at RouterServer (./node_modules/@tanstack/react-router/dist/esm/ssr/RouterServer.js:9:114)">

    Edit 2: SOLVED queryClientProvider weird behaviour. For everyone who is also running into this, check your vite.config and ensure that there is no ssr block defined. I had this one in it, remove it and problem was gone :)

    - ssr: {
    -    // Enable SSR support
    -    noExternal: ["@tanstack/react-router", "@tanstack/react-query"],
    -  },
  3. @simllll I think you still need to wrap the app in a QueryClientProvider as Done in this package made for react https://github.com/TanStack/router/blob/main/packages/react-router-ssr-query/src/index.tsx

Sign in to join this conversation.