Skip to content

Commit 4895d53

Browse files
committed
fix(web): only an in-render <HydrationScript /> declares the #3971 shell ledger
A host-placed bootstrap (generateHydrationScript: hand-built documents, islands hosts like Astro) never gets a marker, so it must not make hydrate() park: the ledger is now declared only by the render that also writes the marker. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_018LY7F9czoRqdMEvk2QwPr1
1 parent 7fc039a commit 4895d53

3 files changed

Lines changed: 27 additions & 7 deletions

File tree

‎.changeset/hydrate-mid-parse-shell-3971.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ fix(web): `hydrate()` started mid-parse waits for the end of the shell (#3971)
66

77
An `async` client entry can run while the browser is still parsing the server shell. `hydrate()` then gathered only the `_hk` nodes parsed so far, created fresh client nodes for the rest, and left the server copies parsed afterwards in the document, dead (no listeners, no bindings).
88

9-
Every render now ends its shell with a small marker script, after its records. When `hydrate()` starts before that marker has run and the parser is still working, it parks until the marker runs (or `DOMContentLoaded`, if none comes), then hydrates against the complete shell. Streamed fragments arrive after the shell and are claimed as before, so streaming still hydrates before the stream ends. No API change; `noScripts` renders and hand-built `_$HY` objects are unaffected.
9+
A render that emits `<HydrationScript />` now ends its shell with a small marker script, after its records, and its bootstrap declares the ledger the marker writes to. When `hydrate()` starts before that marker has run and the parser is still working, it parks until the marker runs (or `DOMContentLoaded`, if none comes), then hydrates against the complete shell. Streamed fragments arrive after the shell and are claimed as before, so streaming still hydrates before the stream ends. No API change. A host-placed bootstrap (`generateHydrationScript()`: hand-built documents, islands hosts such as Astro that await each island's children themselves) declares no ledger and never parks; renders without `<HydrationScript />` and `noScripts` renders are byte-identical.

‎packages/web/src/server.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,9 +3447,11 @@ export function HydrationScript(props: { nonce?: string; eventNames?: string[] }
34473447
// components
34483448
export function HydrationScript(props) {
34493449
const nonce = scriptNonce(sharedConfig.context && sharedConfig.context.nonce);
3450-
// The render owns its bootstrap: its shell ends with the marker (#3971).
3450+
// The render owns its bootstrap, so its shell ends with the marker and the
3451+
// bootstrap declares the ledger hydrate() parks on (#3971).
34513452
if (sharedConfig.context) sharedConfig.context.bootstrap = true;
3452-
return ssr(generateHydrationScript({ nonce, ...props }));
3453+
const { eventNames, nonce: n } = { nonce, ...props };
3454+
return ssr(hydrationBootstrap(eventNames, n, true));
34533455
}
34543456
export function ssrGroup<T extends () => any[]>(fn: T, n: number): T;
34553457

@@ -5814,12 +5816,21 @@ export function generateHydrationScript(options?: {
58145816
// its fallback, or keyed outside any boundary — would otherwise raise
58155817
// `unhandledrejection` from the inline script alone, and a handler attached
58165818
// later only revokes the report. A consumer still meets the rejection.
5817-
export function generateHydrationScript({ eventNames = ["click", "input"], nonce } = {}) {
5819+
export function generateHydrationScript({ eventNames, nonce } = {}) {
5820+
return hydrationBootstrap(eventNames, nonce, false);
5821+
}
5822+
5823+
// `ledger` (#3971): only an in-render <HydrationScript /> declares `_$HY.s`,
5824+
// because only that render writes the shell-end marker it records. A
5825+
// host-placed bootstrap (generateHydrationScript: hand-built documents,
5826+
// islands hosts like Astro, which await each island's children themselves)
5827+
// declares none, so hydrate() never parks under it.
5828+
function hydrationBootstrap(eventNames = ["click", "input"], nonce, ledger) {
58185829
return `<script${
58195830
nonce ? ` nonce="${escape(String(nonce), true)}"` : ""
58205831
}>window._$HY||(e=>{let t=e=>e&&e.hasAttribute&&(e.hasAttribute("_hk")?e:t(e.host&&e.host.nodeType?e.host:e.parentNode));["${eventNames.join(
58215832
'","'
5822-
)}"].forEach((o=>document.addEventListener(o,(o=>{if(!e.events)return;let s=t(o.composedPath&&o.composedPath()[0]||o.target);s&&!e.completed.has(s)&&e.events.push([s,o])}))))})(_$HY={events:[],completed:new WeakSet,r:new Proxy({},{set:(r,k,v)=>(v instanceof Promise&&v.catch(()=>{}),r[k]=v,!0)}),s:{},fe(){}});</script><!--xs-->`;
5833+
)}"].forEach((o=>document.addEventListener(o,(o=>{if(!e.events)return;let s=t(o.composedPath&&o.composedPath()[0]||o.target);s&&!e.completed.has(s)&&e.events.push([s,o])}))))})(_$HY={events:[],completed:new WeakSet,r:new Proxy({},{set:(r,k,v)=>(v instanceof Promise&&v.catch(()=>{}),r[k]=v,!0)}),${ledger ? "s:{}," : ""}fe(){}});</script><!--xs-->`;
58235834
}
58245835

58255836
function queue(fn) {

‎packages/web/test/server/shell-end-marker-3971.spec.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,17 @@ const Doc = (props: { children: any }) => (
3636
);
3737

3838
describe("shell-end marker (#3971)", () => {
39-
test("the bootstrap declares the shell ledger", () => {
40-
expect(generateHydrationScript()).toContain("s:{},fe(){}");
39+
test("only an in-render <HydrationScript /> declares the shell ledger", () => {
40+
// Host-placed bootstraps (hand-built documents, islands hosts like Astro)
41+
// get no ledger, so hydrate() never parks under them.
42+
expect(generateHydrationScript()).not.toContain("s:{}");
43+
expect(
44+
renderToString(() => (
45+
<Doc>
46+
<p>x</p>
47+
</Doc>
48+
))
49+
).toContain("s:{},fe(){}");
4150
});
4251

4352
test("renderToString ends with the marker, after the records", () => {

0 commit comments

Comments
 (0)