Repository navigation
Streaming SSR: native external-HTML injection (StreamOptions.injection) - #169
Conversation
…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]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ 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; |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit b849e0e. Configure here.
…duction) Conflict: regenerated the evals user-app corpus manifest against the merged lockfile. Co-Authored-By: Claude Fable 5 <[email protected]>


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 }) onoctane/server'srenderToPipeableStream/renderToReadableStream:</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 anddonesettles — late data scripts are never dropped.donerejection and abort route through the existing degraded terminal path, still emitting the tail for best-effort well-formedness.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) indocs/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,donerejection, abort during the done-wait). All red before the implementation, green after, in both theoctaneandoctane-prodprojects.octane+octane-prodprojects: 7814 passed.pnpm typecheck,pnpm format:check,changeset:checkgreen.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
doneholds the response open —signalis the bound (documented); TanStack applies its own serialization timeout upstream.prerender) and buffered paths are out of scope by design; Start's buffered splice continues to work unchanged.🤖 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(StreamInjectionSourcewithtake,subscribe, anddone) onrenderToPipeableStream/renderToReadableStream, exported fromoctane/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
injectionis set,runStreamdrains 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 anddonesettle—so injected scripts and streamed segments land inside<body>before the closing tags. Failures fromdonerejection,take/subscribethrows, or abort use the existing fatal/degraded path and still flush the held tail when possible. Withoutinjection, the write path and shell byte shape stay unchanged.Docs cover the API in
docs/ssr.mdplus a design/plan note indocs/ssr-stream-injection-plan.md; contract tests live instreaming-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.