Repository navigation
fix(unhead): split streaming template at SSR outlet marker - #750
Conversation
`prepareStreamingTemplate` now detects the canonical Vite SSR outlet (`<!--app-html-->` / `<!--ssr-outlet-->`) and splits the body interior at the marker so the app stream lands inside the outlet's wrapper element (e.g. `<div id="app">`) instead of before it. Without this, any template using the canonical Vite SSR pattern hits a hydration container mismatch. Example aligned: `examples/vite-ssr-vue-streaming` switches to `<div id="app"><!--app-html--></div>`; `App.vue` no longer owns the hydration wrapper. Playwright's hydration-warnings test no longer masks `Hydration`/`mismatch` strings so real regressions surface.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
📝 WalkthroughWalkthroughThe changes refactor SSR streaming to use marked outlet locations ( Changes
Sequence DiagramsequenceDiagram
participant Client as Client (Browser)
participant Server as Vite SSR Server
participant Head as HeadStream Component
participant HTML as HTML Template
Server->>HTML: Render template with <!--app-html--> outlet marker
Server->>Head: Render HeadStream component (during shell phase)
Head-->>Server: Return <script data-allow-mismatch="children"></script>
Server->>Server: Inject head tags into shell
Server->>Server: Split template at <!--app-html--> marker
Server-->>Client: Send shell (before outlet) + placeholder script
Note over Client: Mount immediately (no router.isReady() guard)
Server->>Head: Stream head updates (during streaming phase)
Head-->>Server: Return <script data-allow-mismatch="children" innerHTML="...">
Server-->>Client: Stream content at outlet location
Client->>Client: Hydrate with mismatch suppression on marked scripts
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Bundle Size Analysis
|
There was a problem hiding this comment.
🧹 Nitpick comments (3)
packages/unhead/src/stream/server.ts (2)
301-323: Docs: mention the outlet-marker split.The public JSDoc for
prepareStreamingTemplatestill only describes splitting at body tag boundaries. Since templates containing<!--app-html-->/<!--ssr-outlet-->now behave materially differently (stream content is relocated into the outlet), it's worth calling this out so users understand when to add the marker.📝 Suggested doc tweak
/** * `@experimental` * * Prepares a template for streaming SSR by splitting it at body tag boundaries * and injecting head content into both parts. * * This is the recommended way to handle streaming templates as it: * - Uses consistent template parsing (same as transformHtmlTemplateRaw) * - Properly injects head content, html/body attrs, and bootstrap script * - Injects body tags (scripts at end of body) into the closing part + * - If the template contains an outlet marker (`<!--app-html-->` or + * `<!--ssr-outlet-->`) inside `<body>`, the body interior is split at that + * marker so streamed app output lands inside the outlet wrapper (e.g. + * `<div id="app"><!--app-html--></div>`), preventing hydration container + * mismatches. *🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/unhead/src/stream/server.ts` around lines 301 - 323, Update the JSDoc for prepareStreamingTemplate to explicitly mention that templates are split not only at body tag boundaries but also at the outlet marker(s) (e.g., <!--app-html--> / <!--ssr-outlet-->), and describe the different behavior: when an outlet marker is present the streamed app content is relocated into that outlet (instead of the default body insertion), and that html/body attributes, head injection and bootstrap script handling still apply; reference the function name prepareStreamingTemplate and the outlet markers <!--app-html--> and <!--ssr-outlet--> so readers know when to add the marker and what to expect.
356-373: Outlet split logic looks correct.Placement is right:
beforeOutlet(including any wrapper opening like<div id="app">) lands inshell, the stream is written in the gap, andafterOutlet + ssr.bodyTags + endPartputs closing tags (e.g.</div>) before the body scripts and</body></html>. This keeps the hydration root intact and keeps late-injected body scripts in their canonical position.Two minor notes:
OUTLET_REis a naive substring regex and would also match an<!--app-html-->comment that happens to appear inside an inline<script>/<style>text node. Extremely unlikely in a realindex.html, but worth being aware of if templates get more elaborate.- Only the first occurrence is used (non-global regex). Silently ignoring a second marker seems fine, but could be worth a dev-mode warning later.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/unhead/src/stream/server.ts` around lines 356 - 373, Detect and handle the two edge cases mentioned: (1) avoid matching an OUTLET_RE inside inline <script> or <style> by refining the detection in the outlet-splitting block (use a context-aware check instead of naive substring match — e.g., scan for the <!--app-html--> comment only in HTML text nodes or ensure it is not inside a <script> or <style> tag before using outletMatch), and (2) warn in dev-mode if more than one outlet marker exists (check for additional matches via matchAll or repeated tests) and log a clear dev warning referencing OUTLET_RE and the outlet handling logic (the variables outletMatch, beforeOutlet, bodyInterior, shell) so maintainers are alerted when multiple markers are silently ignored.examples/vite-ssr-vue-streaming/tests/streaming.spec.ts (1)
249-268: Broadvitefilter may mask real hydration warnings.The comment explicitly says "No filter for hydration/mismatch", which is the right intent, but
!e.includes('vite')is a substring check — a genuine hydration error whose message happens to include a vite-served URL (e.g. a module path) would be dropped. Consider narrowing to known-noisy prefixes (e.g. HMR-related messages) or matching on source rather than text.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@examples/vite-ssr-vue-streaming/tests/streaming.spec.ts` around lines 249 - 268, The current filter in the test 'no unexpected console errors or hydration warnings' (variables: issues, critical) uses a broad substring check (!e.includes('vite')) that can mask real hydration warnings; refine the filter to only ignore known noisy Vite/HMR messages by matching specific HMR patterns or sources instead of any occurrence of "vite" — for example, inspect the Playwright console message (from page.on) and match msg.text() or msg.location() against tighter patterns like HMR endpoints or module-prefixes (/@hmr, /@vite/, 'vite/hmr', or a specific origin) or use a regex that targets only those prefixes, then update the critical filter to use that narrower check in place of !e.includes('vite').
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@examples/vite-ssr-vue-streaming/tests/streaming.spec.ts`:
- Around line 249-268: The current filter in the test 'no unexpected console
errors or hydration warnings' (variables: issues, critical) uses a broad
substring check (!e.includes('vite')) that can mask real hydration warnings;
refine the filter to only ignore known noisy Vite/HMR messages by matching
specific HMR patterns or sources instead of any occurrence of "vite" — for
example, inspect the Playwright console message (from page.on) and match
msg.text() or msg.location() against tighter patterns like HMR endpoints or
module-prefixes (/@hmr, /@vite/, 'vite/hmr', or a specific origin) or use a
regex that targets only those prefixes, then update the critical filter to use
that narrower check in place of !e.includes('vite').
In `@packages/unhead/src/stream/server.ts`:
- Around line 301-323: Update the JSDoc for prepareStreamingTemplate to
explicitly mention that templates are split not only at body tag boundaries but
also at the outlet marker(s) (e.g., <!--app-html--> / <!--ssr-outlet-->), and
describe the different behavior: when an outlet marker is present the streamed
app content is relocated into that outlet (instead of the default body
insertion), and that html/body attributes, head injection and bootstrap script
handling still apply; reference the function name prepareStreamingTemplate and
the outlet markers <!--app-html--> and <!--ssr-outlet--> so readers know when to
add the marker and what to expect.
- Around line 356-373: Detect and handle the two edge cases mentioned: (1) avoid
matching an OUTLET_RE inside inline <script> or <style> by refining the
detection in the outlet-splitting block (use a context-aware check instead of
naive substring match — e.g., scan for the <!--app-html--> comment only in HTML
text nodes or ensure it is not inside a <script> or <style> tag before using
outletMatch), and (2) warn in dev-mode if more than one outlet marker exists
(check for additional matches via matchAll or repeated tests) and log a clear
dev warning referencing OUTLET_RE and the outlet handling logic (the variables
outletMatch, beforeOutlet, bodyInterior, shell) so maintainers are alerted when
multiple markers are silently ignored.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: dbb2e1de-54eb-4716-ac7b-2fe344974778
📒 Files selected for processing (9)
examples/vite-ssr-vue-streaming/index.htmlexamples/vite-ssr-vue-streaming/src/App.vueexamples/vite-ssr-vue-streaming/src/entry-client.tsexamples/vite-ssr-vue-streaming/tests/streaming.spec.tspackages/unhead/src/stream/server.tspackages/vue/src/stream/client.tspackages/vue/src/stream/server.tspackages/vue/src/stream/vite.tspackages/vue/test/streaming.test.ts
💤 Files with no reviewable changes (1)
- examples/vite-ssr-vue-streaming/src/entry-client.ts
Add `outlet?: RegExp | string | false` to `wrapStream` / `prepareStreamingTemplate` options. Defaults to the existing `<!--app-html-->` / `<!--ssr-outlet-->` regex; custom markers or full opt-out (`outlet: false`) are now supported for users whose templates use a different placeholder.
🔗 Linked issue
Follow-up to #748
❓ Type of change
📚 Description
Under a template using the canonical Vite SSR pattern (
<div id="app"><!--app-html--></div>), the streaming app output landed before the hydration container instead of inside it, producing a container-level hydration mismatch.prepareStreamingTemplatenow detects<!--app-html-->/<!--ssr-outlet-->and splits the body interior at the marker so the app stream lands inside the outlet's wrapper element.examples/vite-ssr-vue-streamingswitches to<div id="app"><!--app-html--></div>;App.vueno longer owns the hydration wrapper.Hydration/mismatchstrings so real regressions surface.✅ Test plan
pnpm -F unhead testpnpm -C examples/vite-ssr-vue-streaming test— existing hydration tests run against the new outlet-based template with the stricter console-warning filterSummary by CodeRabbit
Bug Fixes
Tests