Skip to content

Commit 60f8624

Browse files
committed
fix #1596 ssr fragment text merge, fix #1599 ssr onCleanup
1 parent af20f00 commit 60f8624

File tree

6 files changed

+65
-47
lines changed

6 files changed

+65
-47
lines changed

.changeset/strange-eggs-itch.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"babel-preset-solid": patch
3+
"solid-js": patch
4+
---
5+
6+
fix #1596 ssr fragment text merge, fix #1599 ssr onCleanup

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@
3333
"@types/jest": "^28.1.6",
3434
"@types/node": "^18.11.19",
3535
"babel-jest": "^28.1.3",
36-
"babel-plugin-jsx-dom-expressions": "^0.35.18",
36+
"babel-plugin-jsx-dom-expressions": "^0.35.19",
3737
"coveralls": "^3.1.1",
3838
"csstype": "^3.1.0",
39-
"dom-expressions": "0.35.18",
39+
"dom-expressions": "0.35.19",
4040
"fast-glob": "^3.2.11",
41-
"hyper-dom-expressions": "0.35.18",
41+
"hyper-dom-expressions": "0.35.19",
4242
"jest": "^28.1.3",
4343
"jest-environment-jsdom": "^28.1.3",
4444
"jest-ts-webcompat-resolver": "^1.0.0",
45-
"lit-dom-expressions": "0.35.18",
45+
"lit-dom-expressions": "0.35.19",
4646
"ncp": "^2.0.0",
4747
"npm-run-all": "^4.1.5",
4848
"rimraf": "^3.0.2",

packages/babel-preset-solid/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"test": "node test.js"
1515
},
1616
"dependencies": {
17-
"babel-plugin-jsx-dom-expressions": "^0.35.18"
17+
"babel-plugin-jsx-dom-expressions": "^0.35.19"
1818
},
1919
"peerDependencies": {
2020
"@babel/core": "^7.0.0"

packages/solid/src/server/reactive.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ export type Setter<T> = undefined extends T
1313
export type Signal<T> = [get: Accessor<T>, set: Setter<T>];
1414

1515
const ERROR = Symbol("error");
16-
export const BRANCH = Symbol("branch");
1716
export function castError(err: any) {
1817
if (err instanceof Error || typeof err === "string") return err;
1918
return new Error("Unknown error");
@@ -26,12 +25,23 @@ function handleError(err: any) {
2625
for (const f of fns) f(err);
2726
}
2827

29-
const UNOWNED: Owner = { context: null, owner: null };
28+
const UNOWNED: Owner = { context: null, owner: null, owned: null, cleanups: null };
3029
export let Owner: Owner | null = null;
3130

3231
interface Owner {
3332
owner: Owner | null;
3433
context: any | null;
34+
owned: Owner[] | null;
35+
cleanups: (() => void)[] | null;
36+
}
37+
38+
export function createOwner(): Owner {
39+
const o = { owner: Owner, context: null, owned: null, cleanups: null };
40+
if (Owner) {
41+
if (!Owner.owned) Owner.owned = [o];
42+
else Owner.owned.push(o);
43+
}
44+
return o;
3545
}
3646

3747
export function createRoot<T>(fn: (dispose: () => void) => T, detachedOwner?: typeof Owner): T {
@@ -41,12 +51,14 @@ export function createRoot<T>(fn: (dispose: () => void) => T, detachedOwner?: ty
4151
? UNOWNED
4252
: {
4353
context: null,
44-
owner: detachedOwner === undefined ? owner : detachedOwner
54+
owner: detachedOwner === undefined ? owner : detachedOwner,
55+
owned: null,
56+
cleanups: null
4557
};
4658
Owner = root;
4759
let result: T;
4860
try {
49-
result = fn(() => {});
61+
result = fn(fn.length === 0 ? () => {} : () => cleanNode(root));
5062
} catch (err) {
5163
handleError(err);
5264
} finally {
@@ -68,7 +80,7 @@ export function createSignal<T>(
6880
}
6981

7082
export function createComputed<T>(fn: (v?: T) => T, value?: T): void {
71-
Owner = { owner: Owner, context: null };
83+
Owner = createOwner();
7284
try {
7385
fn(value);
7486
} catch (err) {
@@ -89,7 +101,7 @@ export function createReaction(fn: () => void) {
89101
}
90102

91103
export function createMemo<T>(fn: (v?: T) => T, value?: T): () => T {
92-
Owner = { owner: Owner, context: null };
104+
Owner = createOwner();
93105
let v: T;
94106
try {
95107
v = fn(value);
@@ -136,18 +148,21 @@ export function on<T, U>(
136148
export function onMount(fn: () => void) {}
137149

138150
export function onCleanup(fn: () => void) {
139-
let node;
140-
if (Owner && (node = lookup(Owner, BRANCH))) {
141-
if (!node.cleanups) node.cleanups = [fn];
142-
else node.cleanups.push(fn);
151+
if (Owner) {
152+
if (!Owner.cleanups) Owner.cleanups = [fn];
153+
else Owner.cleanups.push(fn);
143154
}
144155
return fn;
145156
}
146157

147-
export function cleanNode(node: { cleanups?: Function[] | null }) {
158+
export function cleanNode(node: Owner) {
159+
if (node.owned) {
160+
for (let i = 0; i < node.owned.length; i++) cleanNode(node.owned[i]);
161+
node.owned = null;
162+
}
148163
if (node.cleanups) {
149164
for (let i = 0; i < node.cleanups.length; i++) node.cleanups[i]();
150-
node.cleanups = undefined;
165+
node.cleanups = null;
151166
}
152167
}
153168

packages/solid/src/server/rendering.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
castError,
1212
onCleanup,
1313
cleanNode,
14-
BRANCH
14+
createOwner
1515
} from "./reactive.js";
1616
import type { JSX } from "../jsx.js";
1717

@@ -270,9 +270,8 @@ export function ErrorBoundary(props: {
270270
!sync && ctx.replace("e" + id, displayFallback);
271271
sync = true;
272272
});
273-
onCleanup(() => cleanNode(clean));
274273
createMemo(() => {
275-
Owner!.context = { [BRANCH]: (clean = {}) };
274+
clean = Owner;
276275
return (res = props.children);
277276
});
278277
if (error) return displayFallback();
@@ -556,11 +555,7 @@ export function Suspense(props: { fallback?: string; children: string }) {
556555
let clean: any;
557556
const ctx = sharedConfig.context!;
558557
const id = ctx.id + ctx.count;
559-
const o = Owner;
560-
if (o) {
561-
if (o.context) o.context[BRANCH] = clean = {};
562-
else o.context = { [BRANCH]: (clean = {}) };
563-
}
558+
const o = createOwner();
564559
const value: SuspenseContextType =
565560
ctx.suspense[id] ||
566561
(ctx.suspense[id] = {
@@ -574,11 +569,11 @@ export function Suspense(props: { fallback?: string; children: string }) {
574569
});
575570
function runSuspense() {
576571
setHydrateContext({ ...ctx, count: 0 });
572+
o && cleanNode(o);
577573
return runWithOwner(o!, () => {
578574
return createComponent(SuspenseContext.Provider, {
579575
value,
580576
get children() {
581-
clean && cleanNode(clean);
582577
return props.children;
583578
}
584579
});
@@ -601,7 +596,9 @@ export function Suspense(props: { fallback?: string; children: string }) {
601596
done = ctx.async ? ctx.registerFragment(id) : undefined;
602597
if (ctx.async) {
603598
setHydrateContext({ ...ctx, count: 0, id: ctx.id + "0-f", noHydrate: true });
604-
const res = { t: `<template id="pl-${id}"></template>${resolveSSRNode(props.fallback)}<!pl-${id}>` };
599+
const res = {
600+
t: `<template id="pl-${id}"></template>${resolveSSRNode(props.fallback)}<!pl-${id}>`
601+
};
605602
setHydrateContext(ctx);
606603
return res;
607604
}

pnpm-lock.yaml

Lines changed: 20 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)