Skip to content

Commit 53af2cb

Browse files
committed
fix(hydration): avoid template parse for pre-wrapped octane-frag arms during adoption
The compiler emits HTML multi-root @if/@switch arms pre-wrapped in <octane-frag> with frag=0. During hydration, claimRoots called templateRootCount which fell through to resolveLazyTemplate for these templates (frag was 0), forcing a template parse on the first adoption of every such arm — the exact work the lazy-template path was designed to avoid. Encode the root count as a negative frag value for pre-wrapped templates so templateRootCount can read the count directly (absolute value) without parsing. Adjust parseTemplate and lazyFragmentRootMatches to use frag > 0 so negative values correctly skip the raw-fragment wrapping and root-descriptor paths.
1 parent 1e969db commit 53af2cb

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

‎packages/octane/src/compiler/compile.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26382,7 +26382,10 @@ function planJsx(
2638226382
const flag = nsFlag(tplNs);
2638326383
// A raw multi-root template passes its root count, which hydration reads
2638426384
// without parsing the template to find where the roots it adopted end.
26385-
const fragArg = !single && (flag !== 0 || resolvedFrag) ? htmlIdx : 0;
26385+
// Pre-wrapped HTML multi-root templates (flag=0, !resolvedFrag) store the
26386+
// count as a negative number so templateRootCount can read it without
26387+
// parsing and parseTemplate knows the wrapper is already present.
26388+
const fragArg = single ? 0 : flag !== 0 || resolvedFrag ? htmlIdx : -htmlIdx;
2638626389
let template = rootTemplate;
2638726390
if (!single && flag === 0 && !resolvedFrag) {
2638826391
template = templateElement(

‎packages/octane/src/runtime.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18388,7 +18388,9 @@ interface LazyTemplateRecord {
1838818388
ns: 0 | 1 | 2 | 3;
1838918389
/**
1839018390
* Raw multi-root markup: the number of roots, as the compiler counts them.
18391-
* 0 = one root, or HTML roots pre-wrapped in `<octane-frag>`.
18391+
* 0 = one root. Positive = raw fragment (SVG/MathML/opaque resolved-HTML),
18392+
* parseTemplate adds the wrapper. Negative = HTML roots pre-wrapped in
18393+
* `<octane-frag>`, count is the absolute value, wrapper already in `html`.
1839218394
*/
1839318395
frag: number;
1839418396
parsed: Array<Node | null>;
@@ -18434,7 +18436,7 @@ function parseTemplate(html: string, ns: 0 | 1 | 2, frag: number): Node {
1843418436
// Fixed HTML multi-root templates arrive pre-wrapped by the compiler. Opaque
1843518437
// multi-root templates carry raw markup because their eventual namespace is
1843618438
// unknown, so add the equivalent wrapper only after HTML wins at clone time.
18437-
(STAGED_DOM?.view(t) ?? t).innerHTML = frag ? `<octane-frag>${html}</octane-frag>` : html;
18439+
(STAGED_DOM?.view(t) ?? t).innerHTML = frag > 0 ? `<octane-frag>${html}</octane-frag>` : html;
1843818440
const root = getFirstChild(t.content) as Element;
1843918441
// Multi-root HTML templates arrive wrapped in a synthetic <octane-frag>. The
1844018442
// wrapper never exists in the server DOM (the roots render bare), so stamp it
@@ -18510,14 +18512,14 @@ function isLazyFragment(lazy: LazyTemplateRecord): boolean {
1851018512

1851118513
/**
1851218514
* How many roots a multi-root template has. The compiler passes the count as
18513-
* a raw fragment's `frag`; a parsed or pre-wrapped template counts its
18514-
* children.
18515+
* a raw fragment's `frag` (positive) or a pre-wrapped HTML fragment's `frag`
18516+
* (negative, absolute value = count); a parsed template counts its children.
1851518517
*/
1851618518
function templateRootCount(template: Node | LazyTemplateRecord): number {
1851718519
let parsed: Node;
1851818520
if ((template as Node).nodeType === undefined) {
1851918521
const lazy = template as LazyTemplateRecord;
18520-
if (lazy.frag !== 0) return lazy.frag;
18522+
if (lazy.frag !== 0) return lazy.frag > 0 ? lazy.frag : -lazy.frag;
1852118523
parsed = resolveLazyTemplate(lazy);
1852218524
} else parsed = template as Node;
1852318525
let count = 0;
@@ -18554,7 +18556,7 @@ function lazyRootMatches(server: Node, lazy: LazyTemplateRecord): boolean {
1855418556
*/
1855518557
function lazyFragmentRootMatches(server: Node, lazy: LazyTemplateRecord): boolean {
1855618558
const root =
18557-
lazy.frag !== 0
18559+
lazy.frag > 0
1855818560
? lazyRootDescriptor(lazy)
1855918561
: templateRootDescriptor(lazy.html, 13 /* '<octane-frag>'.length */);
1856018562
if (root === 8) return true;

0 commit comments

Comments
 (0)