Skip to content

Commit 62c5a98

Browse files
authored
fix: Update SuspenseList to handle hydration context and add unit tests for resolveSSRNode and createResource functions (#2531)
* fix: Update `SuspenseList` to handle hydration context * chore: add changeset for `SuspenseList` hydration context fix * test: add unit tests for `resolveSSRNode` and `createResource` functions * chore: add changeset for `resolveSSRNode` and `createResource` tests * chore: add TODO for `revealOrder` and `tail` support in `SuspenseList`
1 parent f59ee48 commit 62c5a98

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

.changeset/late-melons-raise.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Update `SuspenseList` to handle hydration context

.changeset/sad-heads-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
Add unit tests for `resolveSSRNode` and `createResource` functions

packages/solid/src/server/rendering.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import {
2-
Owner,
2+
Accessor,
3+
castError,
4+
catchError,
5+
cleanNode,
36
createContext,
47
createMemo,
5-
useContext,
8+
createOwner,
9+
Owner,
610
runWithOwner,
7-
catchError,
8-
Accessor,
911
Setter,
1012
Signal,
11-
castError,
12-
cleanNode,
13-
createOwner
13+
useContext
1414
} from "./reactive.js";
1515
import type { JSX } from "../jsx.js";
1616

@@ -623,7 +623,14 @@ export function SuspenseList(props: {
623623
revealOrder: "forwards" | "backwards" | "together";
624624
tail?: "collapsed" | "hidden";
625625
}) {
626-
// TODO: support tail options
626+
// TODO: support revealOrder and tail options
627+
if (sharedConfig.context && !sharedConfig.context.noHydrate) {
628+
const c = sharedConfig.context;
629+
setHydrateContext(nextHydrateContext());
630+
const result = props.children;
631+
setHydrateContext(c);
632+
return result;
633+
}
627634
return props.children;
628635
}
629636

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { describe, expect, test } from "vitest";
2+
import { createResource } from "../src/index.js";
3+
import { resolveSSRNode } from "dom-expressions/src/server.js";
4+
5+
describe("resolveSSRNode", () => {
6+
test("should resolve a string node", () => {
7+
expect(resolveSSRNode("Hello World")).toBe("Hello World");
8+
});
9+
10+
test("should resolve a null or boolean node", () => {
11+
expect(resolveSSRNode(null)).toBe("");
12+
expect(resolveSSRNode(false)).toBe("");
13+
});
14+
15+
test("should resolve an array of nodes", () => {
16+
const nodes = ["<div>", "<span>", "</span>", "</div>"];
17+
expect(resolveSSRNode(nodes)).toBe("<div><!--!$--><span><!--!$--></span><!--!$--></div>");
18+
});
19+
20+
test("should resolve an object with 't' property", () => {
21+
const node = { t: "<div>Text</div>" };
22+
expect(resolveSSRNode(node)).toBe("<div>Text</div>");
23+
});
24+
25+
test("should resolve a function node", () => {
26+
const fn = () => "dynamic content";
27+
expect(resolveSSRNode(fn)).toBe("dynamic content");
28+
});
29+
});
30+
31+
describe("createResource", () => {
32+
test("should return initial value immediately if provided", () => {
33+
const [data] = createResource(() => Promise.resolve("test"), { initialValue: "loading" });
34+
expect(data()).toBe("loading");
35+
});
36+
37+
test("should handle a promise and update the value", async () => {
38+
const [data, { refetch }] = createResource(
39+
() => new Promise(resolve => setTimeout(() => resolve("Success!"), 10))
40+
);
41+
42+
// Initially, data should be undefined, and loading should be true
43+
expect(data()).toBeUndefined();
44+
expect(data.loading).toBe(true);
45+
46+
await new Promise(r => setTimeout(r, 20)); // Wait for the promise to resolve
47+
48+
// After resolution, data should have the new value, and loading should be false
49+
expect(data()).toBe("Success!");
50+
expect(data.loading).toBe(false);
51+
});
52+
});

0 commit comments

Comments
 (0)