Skip to content
Source
Sign inSign up
TanStack/router

Start RSC: `preload()` / `preconnect()` / `preinit()` called in a server component never reach the SSR document #8727

Closedhavalex(GitHub profile, opens in a new tab) opened this issue 0 comments

Which project does this relate to?

Start with RSC (@tanstack/react-start-rsc 0.1.63, @vitejs/plugin-rsc 0.5.37, React 19.3.0)

Describe the bug

A server component that calls ReactDOM.preload(href, { as: "image" }), preconnect() or preinit() emits a Flight hint row (<id>:H<code><json>). React applies a hint where the payload is decoded: on the server, react-server-dom's client calls react-dom's dispatcher, which hands it to the Fizz request running in the current async context, and Fizz writes it into the document's <head>. That is how Next's App Router gets server-component preloads into the HTML.

renderServerComponent and createCompositeComponent decode the stream inside the loader (await ssrHandler.decode(stream), globalThis.__RSC_SSR__ in serialization.server.js), before the HTML render starts. No Fizz request exists in that async context, so every hint is dropped silently. The SSR HTML does not carry them; the browser applies them only when it decodes the payload itself (at hydration, after the resources were needed, or on a client navigation).

Effect: a page's LCP image preload, font preloads and CDN preconnects are missing from the HTML that matters most for them.

Steps to reproduce

  1. A route loader returns renderServerComponent(<Hero />), where Hero calls preload("/hero.avif", { as: "image", fetchPriority: "high" }).
  2. Load the page with JavaScript disabled, or view source: no <link rel="preload" href="/hero.avif">.
  3. With JavaScript on, the link appears only after hydration; on a client navigation it is added before the page renders.

Expected behavior

Hints from the payload reach the SSR document head, as they do when the payload is decoded inside the HTML render.

Possible fixes

  • Decode inside the render (the proxy's getTree() already supports deferred decoding), or
  • capture the hint rows during the early decode and replay them from the render, through react-dom's public preload / preconnect / prefetchDNS / preinit / preloadModule / preinitModule. This is our workaround: it scans the replayable stream's bytes for H rows with React's Flight framing (length-prefixed rows skipped by length) and dispatches each hint once, inside the document render's async context, the received rows at once and later ones as they arrive.

Happy to share the scanner if the second route is preferred.

Sign in to join this conversation.