Skip to content

Streaming SSR: native external-HTML injection (StreamOptions.injection) - #169

Merged
trueadm merged 1 commit into
mainfrom
claude/ssr-stream-injection
Jul 18, 2026
Merged

trueadm merged 1 commit into
mainfrom
claude/ssr-stream-injection

Conversation

@trueadm

@trueadm trueadm commented Jul 18, 2026 •

Copy link
Copy Markdown
Collaborator

Why

TanStack team feedback on the Start integration: Start merges its data stream (<script> tags materializing as loaders settle) into the framework's HTML stream by decoding the bytes and re-parsing every chunk for safe insertion points (transformStreamWithRouter: backwards closing-tag scans, leftover buffers, a held </body> tail, four size caps, full decode→re-encode). Octane's streaming engine already emits tag-complete chunks and knows the document tail at generation time — so the merge is now first-class instead.

Along the way this surfaced a real defect in the external approach: for octane document renders the upstream transform enters tail-holding on the first chunk (the shell contains the whole document), after which every Suspense segment is buffered into a 64 KiB-capped tail until both streams end — out-of-order streaming is effectively disabled and large segments would trip the cap. Adopting this API restores real streaming.

What

StreamOptions.injection (StreamInjectionSource { take(), subscribe(notify), done }) on octane/server's renderToPipeableStream / renderToReadableStream:

  • Injected HTML is emitted verbatim, in push order, each drain as its own transport chunk strictly between renderer chunks — never before the shell. A single serialized write chain gives out-of-band notify-drains and wave-loop writes a total order under the existing backpressure gates.
  • Document renders hold the closing tail (</body> + interleaved hydration block markers + </html>, detected once by suffix) and write it last; injected chunks and streamed segments land inside <body>. The stream closes only when rendering is complete and done settles — late data scripts are never dropped. done rejection and abort route through the existing degraded terminal path, still emitting the tail for best-effort well-formedness.
  • Zero cost when absent: every hook no-ops without the option; the tail stays in the shell and the write path is unchanged (pinned by a test).

Octane extension tier (Fizz owns its data injection internally, so there is no React surface to mirror). Docs in docs/ssr.md; design + remaining phases (styles-in-head, auto-doctype, switching @octanejs/tanstack-start's vendored stream path onto this API, upstreaming to TanStack) in docs/ssr-stream-injection-plan.md.

Testing

  • packages/octane/tests/streaming-ssr-injection.test.ts — 8 contract cases (ordering/chunk-boundaries, done-gated close + idle drains, shell-first, web-stream parity, document tail-holding, no-injection byte-shape control, done rejection, abort during the done-wait). All red before the implementation, green after, in both the octane and octane-prod projects.
  • Full octane + octane-prod projects: 7814 passed. pnpm typecheck, pnpm format:check, changeset:check green.
  • Perf: benchmarks/streaming-ssr (30 iters, production builds) baseline vs candidate with injection absent — shell 0.22→0.23 ms (staggered) / 0.13→0.12 ms (all-fast), totals within run-to-run noise. No regression; no improvement claimed.

Residual risk / follow-ups

  • A source that never settles done holds the response open — signal is the bound (documented); TanStack applies its own serialization timeout upstream.
  • The bot (prerender) and buffered paths are out of scope by design; Start's buffered splice continues to work unchanged.
  • End-to-end proof through the website arrives with the follow-up that switches the vendored Start stream path onto this API (after Port TanStack Start in-repo as @octanejs/tanstack-start #168).

🤖 Generated with Claude Code


Note

Medium Risk
Touches core streaming SSR completion, write ordering, and document tail semantics in runStream; mistakes could affect response shape, hang streams, or regress the no-injection path, though tests and the cold-path design aim to keep absent-option behavior identical.

Overview
Adds StreamOptions.injection (StreamInjectionSource with take, subscribe, and done) on renderToPipeableStream / renderToReadableStream, exported from octane/server. Frameworks can push externally produced HTML (e.g. loader data <script> tags) into the response between renderer chunks instead of re-parsing the byte stream for safe insertion points.

When injection is set, runStream drains queued HTML via a serialized write chain (subscribe notifications interleave safely with Suspense wave writes), never emits injection before the shell, and for full documents splits and holds the </body>…</html> tail until render completion and done settle—so injected scripts and streamed segments land inside <body> before the closing tags. Failures from done rejection, take/subscribe throws, or abort use the existing fatal/degraded path and still flush the held tail when possible. Without injection, the write path and shell byte shape stay unchanged.

Docs cover the API in docs/ssr.md plus a design/plan note in docs/ssr-stream-injection-plan.md; contract tests live in streaming-ssr-injection.test.ts (fragment + document ordering, idle drains, failures).

Reviewed by Cursor Bugbot for commit b849e0e. Bugbot is set up for automated code reviews on this repo. Configure here.

…ed SSR

Frameworks like TanStack Start merge a data stream (<script> tags
materializing as loaders settle) into the render stream by decoding the
byte stream and re-parsing it for safe insertion points, holding the
document tail in bounded text buffers. Octane's streaming engine already
emits tag-complete chunks and knows the document tail at generation time,
so the merge is now first-class:

- StreamInjectionSource { take(), subscribe(notify), done } on
  StreamOptions (octane/server). Injected HTML is emitted verbatim, in
  push order, each drain as its own transport chunk strictly BETWEEN
  renderer chunks — never before the shell. A single serialized write
  chain gives out-of-band notify drains and wave-loop writes a total
  order; every hook no-ops when the option is absent (measured flat on
  the streaming-ssr bench).
- Document renders hold the closing tail (`</body>` + interleaved block
  markers + `</html>`, detected once by suffix) and write it last;
  injected chunks and streamed Suspense segments land inside <body>. The
  stream closes only when rendering is complete AND `done` settles, so
  late data scripts are never dropped. `done` rejection and abort route
  through the existing degraded terminal path, still emitting the tail.
- Docs: docs/ssr.md section; design + remaining phases in
  docs/ssr-stream-injection-plan.md (styles-in-head, auto-doctype, and
  moving @octanejs/tanstack-start's vendored stream path onto this API —
  the upstream transform currently buffers every post-shell chunk into a
  64KiB-capped tail for document renders, so adopting this also restores
  out-of-order streaming).

Contract tests: packages/octane/tests/streaming-ssr-injection.test.ts
(8 cases, red before the implementation), green in both the octane and
octane-prod projects; full octane suites 7814 passed; typecheck + format.

Co-Authored-By: Claude Fable 5 <[email protected]>
@vercel

vercel Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
octane-website Ready Ready Preview, Comment Jul 18, 2026 10:46pm
octane-website-mcp Ready Ready Preview, Comment Jul 18, 2026 10:46pm

Request Review

@vercel
vercel Bot temporarily deployed to Preview – octane-website-mcp July 18, 2026 22:46 Inactive
@vercel
vercel Bot temporarily deployed to Preview – octane-website July 18, 2026 22:46 Inactive

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit b849e0e. Configure here.

const tailChunk = heldDocumentTail;
heldDocumentTail = '';
const tailWrite = write(tailChunk);
if (tailWrite !== undefined) await tailWrite;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Held tail lost on abort race

Medium Severity

The success-path write of heldDocumentTail omits the terminal flag and clears the held string before awaiting. An abort that lands after done settles but before that write runs is rejected by the non-terminal sink check, and the catch path can no longer re-emit the tail — so the document closes without </body></html>. The degraded paths correctly pass terminal: true for this write.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit b849e0e. Configure here.

@trueadm
trueadm merged commit f511024 into main Jul 18, 2026
21 checks passed
@trueadm
trueadm deleted the claude/ssr-stream-injection branch July 18, 2026 22:55
trueadm added a commit that referenced this pull request Jul 18, 2026
…duction)

Conflict: regenerated the evals user-app corpus manifest against the merged lockfile.

Co-Authored-By: Claude Fable 5 <[email protected]>

This branch was previously deployed

2 inactive deployments
Preview – octane-website — b849e0e5 Deployed Jul 18, 2026 by vercel[bot]
Preview – octane-website-mcp — b849e0e5 Deployed Jul 18, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant