Skip to content

Commit 45710c7

Browse files
committed
fix(octane): keep CompiledContext children when a clear is discarded
resetScopeChildren replaced a scope's child records without journaling them. A rolled-back render restored the child DOM and left those records empty, so the next render mounted a second copy.
1 parent 7c4b792 commit 45710c7

4 files changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"octane": patch
3+
---
4+
5+
Keep a `CompiledContext`'s children when a render that clears them is discarded. The following render updates those children instead of mounting a second copy beside the restored ones.

‎packages/octane/src/runtime.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16976,6 +16976,16 @@ function resetScopeChildren(scope: Scope, detachDom = true): void {
1697616976

1697716977
// Drop the collections rather than emptying them: the lazily-allocated ones go back to null,
1697816978
// and `slots` gets a fresh array (compiled bodies index it directly, so it stays non-null).
16979+
// A discarded root render restores the child DOM detached above. Journal the records
16980+
// replaced below with it, so that DOM still belongs to the scope that rendered it.
16981+
journalRootProperty(scope, 'children', scope.children);
16982+
journalRootProperty(scope, 'cleanups', scope.cleanups);
16983+
journalRootProperty(scope, '_slots', scope._slots);
16984+
journalRootProperty(scope, 'effectSlots', scope.effectSlots);
16985+
journalRootProperty(scope, 'hooks', scope.hooks);
16986+
journalRootProperty(scope, 'slots', scope.slots);
16987+
journalRootProperty(scope, 'compilerMemo', scope.compilerMemo);
16988+
journalRootProperty(scope, 'refFields', scope.refFields);
1697916989
scope.children = null;
1698016990
scope.cleanups = null;
1698116991
scope._slots = null;

‎packages/octane/tests/_fixtures/compiled-boundaries.tsrx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CompiledOutlet,
44
createCompiledContext,
55
createContext,
6+
use,
67
useContext,
78
useLayoutEffect,
89
useState,
@@ -98,6 +99,25 @@ export function GatedShell(props: { show: boolean; log: (message: string) => voi
9899
</main>
99100
}
100101

102+
function Hold(props: { gate: PromiseLike<string> | null }) @{
103+
const value = props.gate !== null ? use(props.gate) : 'held';
104+
<span>{value as string}</span>
105+
}
106+
107+
export function SuspendedGateShell(props: {
108+
show: boolean;
109+
gate: PromiseLike<string> | null;
110+
log: (message: string) => void;
111+
}) @{
112+
<main>
113+
<GateProvider show={props.show}>
114+
<Screen label="screen" log={props.log} />
115+
</GateProvider>
116+
<Hold gate={props.gate} />
117+
<footer>tail</footer>
118+
</main>
119+
}
120+
101121
export function PendingShell(props: { component: typeof Other; label: string }) @{
102122
<CompiledContext context={Tone} value="ready">
103123
@try {

‎packages/octane/tests/compiled-boundaries.test.ts‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,43 @@ describe('checked compiled boundaries', () => {
119119
expect(() => renderToString(server.CheckedProvider, props)).toThrow(/CompiledContext/);
120120
});
121121

122+
it('keeps compiled provider children when a render that clears them is discarded', () => {
123+
const events: string[] = [];
124+
const log = (value: string) => events.push(value);
125+
const view = mount(client.SuspendedGateShell, { show: true, gate: null, log });
126+
try {
127+
const button = view.container.querySelector('button')!;
128+
const article = view.container.querySelector('article')!;
129+
flushSync(() => button.click());
130+
expect(button.textContent).toBe('screen:gated:1');
131+
132+
// A later sibling suspends the root, so this clear never commits.
133+
const gate = new Promise<string>(() => {});
134+
flushSync(() => view.root.render(client.SuspendedGateShell, { show: false, gate, log }));
135+
expect(view.container.querySelector('article')).toBe(article);
136+
expect(view.container.querySelectorAll('button')).toHaveLength(1);
137+
expect(button.textContent).toBe('screen:gated:1');
138+
expect(view.container.querySelector('footer')!.textContent).toBe('tail');
139+
expect(events).toEqual(['mount']);
140+
141+
flushSync(() => view.root.render(client.SuspendedGateShell, { show: true, gate: null, log }));
142+
expect(view.container.querySelectorAll('article')).toHaveLength(1);
143+
expect(view.container.querySelector('article')).toBe(article);
144+
expect(view.container.querySelector('button')).toBe(button);
145+
expect(button.textContent).toBe('screen:gated:1');
146+
expect(events).toEqual(['mount']);
147+
148+
flushSync(() =>
149+
view.root.render(client.SuspendedGateShell, { show: false, gate: null, log }),
150+
);
151+
expect(view.container.querySelector('article')).toBeNull();
152+
expect(view.container.querySelector('footer')!.textContent).toBe('tail');
153+
expect(events).toEqual(['mount', 'cleanup']);
154+
} finally {
155+
view.unmount();
156+
}
157+
});
158+
122159
it('removes compiled provider children when they become null', () => {
123160
const events: string[] = [];
124161
const log = (value: string) => events.push(value);

0 commit comments

Comments
 (0)