Skip to content

Commit 8f4a0eb

Browse files
committed
fix: preserve head statement source order across SSR partition boundary
The SSR head partition independently classified each hoisted head statement as front (capture-free) or deferred (captures a setup local). When a component mixed static and setup-local head resources like [free, capture, free], the second free statement was reordered ahead of the capturing one, breaking discovery order versus the client path which emits all resources in list order after setup. Fix: once the first capturing statement is encountered, all subsequent statements are also deferred, preserving their relative source order. This matches the client's sequential registration and keeps CSS cascade order consistent between SSR and client mounts.
1 parent f0b5392 commit 8f4a0eb

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9783,15 +9783,17 @@ function ssrCompileBodyWithMapTemps(
97839783
for (const s of inlinedSubs) collectStatementBindings(s, bodyBindings);
97849784
if (bodyBindings.size > 0) {
97859785
frontHead = [];
9786+
let seenCapture = false;
97869787
for (const stmt of headStatements) {
9787-
let captures = false;
9788-
for (const free of collectFreeIdentifiers(stmt, [])) {
9789-
if (bodyBindings.has(free)) {
9790-
captures = true;
9791-
break;
9788+
if (!seenCapture) {
9789+
for (const free of collectFreeIdentifiers(stmt, [])) {
9790+
if (bodyBindings.has(free)) {
9791+
seenCapture = true;
9792+
break;
9793+
}
97929794
}
97939795
}
9794-
(captures ? deferredHead : frontHead).push(stmt);
9796+
(seenCapture ? deferredHead : frontHead).push(stmt);
97959797
}
97969798
}
97979799
}

‎packages/octane/tests/_fixtures/float-group-order.tsrx‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ export function LocalHref(props) @{
8484
</>
8585
}
8686

87+
// A static sheet, then a setup-local-computed sheet, then another static sheet.
88+
// SSR must preserve source order across the partition boundary — the static
89+
// trailing sheet must NOT be reordered ahead of the setup-local one.
90+
export function MixedStaticAndLocal(props) @{
91+
const slug = '/' + props.name + '-local.css';
92+
<>
93+
<link rel="stylesheet" href="/first-static.css" precedence="one" />
94+
<link rel="stylesheet" href={slug} precedence="two" />
95+
<link rel="stylesheet" href="/last-static.css" precedence="three" />
96+
<div class="mixed">{'M'}</div>
97+
</>
98+
}
99+
87100
function Exploding(props) @{
88101
const boom = () => {
89102
throw new Error('mount failure');

‎packages/octane/tests/float-resources.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
NestedTryOrder,
4545
LocalHref,
4646
FailingArm,
47+
MixedStaticAndLocal,
4748
} from './_fixtures/float-group-order.tsrx';
4849

4950
function sheetHrefs(): string[] {
@@ -613,6 +614,21 @@ describe('Float resources — precedence group discovery order', () => {
613614
expect(sheetHrefs()).toEqual(['/x-dep.css']);
614615
});
615616

617+
it('SSR preserves source order when static and setup-local head hoists are mixed', async () => {
618+
const App = () => Server.createElement(srvOrder.MixedStaticAndLocal, { name: 'x' }) as any;
619+
const r = await Server.renderToString(App as any);
620+
const first = r.html.indexOf('/first-static.css');
621+
const local = r.html.indexOf('/x-local.css');
622+
const last = r.html.indexOf('/last-static.css');
623+
expect(first).toBeGreaterThan(-1);
624+
expect(local).toBeGreaterThan(first);
625+
expect(last).toBeGreaterThan(local);
626+
627+
// Client control: same discovery order.
628+
await act(() => mountInto(MixedStaticAndLocal, { name: 'x' }));
629+
expect(sheetHrefs()).toEqual(['/first-static.css', '/x-local.css', '/last-static.css']);
630+
});
631+
616632
it('an arm that fails while rendering a child keeps its already-registered sheet', async () => {
617633
// Resources are page-global and never removed; registration precedes
618634
// child evaluation on the server too, so client and server agree that a

0 commit comments

Comments
 (0)