Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming SSR involving Suspense & streaming ReadableStream: chunks arrive before route info, erroring #3117

Open
phryneas opened this issue Jan 7, 2025 · 0 comments
Labels
start Everything about TanStack Start

Comments

@phryneas
Copy link

phryneas commented Jan 7, 2025

Which project does this relate to?

Start

Describe the bug

This is a bug with Router/Start streaming SSR when

  • the route component suspends
  • the loader returns a ReadableStream
  • the stream has a fast first chunk

Under these circumstances, the generated HTML is out of order, containing these three <script tags in this order:

    <script class="tsr-once ">__TSR__.initMatch({ "index": 0, "__beforeLoadContext": "{}", "loaderData": "{\"$undefined\":0}", "extracted": {} })
        console.info(`Injected From Server:
__TSR__.initMatch({"index":0,"__beforeLoadContext":"{}","loaderData":"{\\"$undefined\\":0}","extracted":{}})`)
        if (typeof __TSR__ !== "undefined") __TSR__.cleanScripts()</script>
    <script
        class='tsr-once'>__TSR__.matches[1].extracted[1].value.controller.enqueue(new TextEncoder().encode("this is too soon!")); console.info(`Injected From Server:
__TSR__.matches[1].extracted[1].value.controller.enqueue(new TextEncoder().encode("this is too soon!"))`); if (typeof __TSR__ !== 'undefined') __TSR__.cleanScripts()</script>
    <div hidden id="S:0"><template id="P:1"></template>
        <script class="tsr-once ">__TSR__.initMatch({ "index": 1, "__beforeLoadContext": "{\"delayed\":{\"$undefined\":0},\"nested\":{\"$undefined\":0}}", "loaderData": "{\"delayed\":{\"status\":\"pending\"},\"nested\":{\"stream\":{}}}", "extracted": { "0": { "type": "promise", "path": ["delayed"] }, "1": { "type": "stream", "path": ["nested", "stream"] } } })
            console.info(`Injected From Server:
__TSR__.initMatch({"index":1,"__beforeLoadContext":"{\\"delayed\\":{\\"$undefined\\":0},\\"nested\\":{\\"$undefined\\":0}}","loaderData":"{\\"delayed\\":{\\"status\\":\\"pending\\"},\\"nested\\":{\\"stream\\":{}}}","extracted":{"0":{"type":"promise","path":["delayed"]},"1":{"type":"stream","path":["nested","stream"]}}})`)
            if (typeof __TSR__ !== "undefined") __TSR__.cleanScripts()</script>
        <div class="tsr-once"><!--$?--><template id="B:2"></template><!--/$--></div>
        <div class="tsr-once"><!--$?--><template id="B:3"></template><!--/$--></div>
    </div>

Note that the second script tag tries to access __TSR__.matches[1].extracted[1] - the problem here is that __TSR__.matches[1] will only be initialized as a result of the third script tag, so a Uncaught TypeError: __TSR__.matches[1] is undefined error is thrown.

Your Example Website or App

https://stackblitz.com/~/github.com/phryneas/reproduction-tanstack-router-chunk-stream-order-bug?file=app/routes/index.tsx

Steps to Reproduce the Bug or Issue

Open the console and see the bug.

Expected behavior

The chunk should arrive after the controller is initialized in the browser.

Screenshots or Videos

No response

Platform

  • OS: irrelevant
  • Browser: irrelevant
  • Version: 1.95.1

Additional context

No response

phryneas added a commit to apollographql/apollo-client-nextjs that referenced this issue Jan 7, 2025
@SeanCassiere SeanCassiere added the start Everything about TanStack Start label Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
start Everything about TanStack Start
Projects
Status: No status
Development

No branches or pull requests

2 participants