Skip to content

Commit ffa57be

Browse files
fix(vite): partial island loading (denoland#3226)
1 parent afdf5ea commit ffa57be

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useEffect, useState } from "preact/hooks";
2+
3+
export function ReadyIsland() {
4+
const [ready, set] = useState(false);
5+
6+
useEffect(() => {
7+
set(true);
8+
}, []);
9+
10+
return (
11+
<div class={ready ? "ready" : "not-ready"}>
12+
{ready ? "ready" : "waiting..."}
13+
</div>
14+
);
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Partial } from "@fresh/core/runtime";
2+
import { ReadyIsland } from "../../islands/tests/Ready.tsx";
3+
4+
export default function Page() {
5+
return (
6+
<div f-client-nav>
7+
<h1>Partial</h1>
8+
<Partial name="partial-test">
9+
<a href="/tests/partial_insert">click me</a>
10+
</Partial>
11+
<ReadyIsland />
12+
</div>
13+
);
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Partial } from "@fresh/core/runtime";
2+
import { CounterHooks } from "../../islands/tests/CounterHooks.tsx";
3+
4+
export default function Page() {
5+
return (
6+
<Partial name="partial-test">
7+
<CounterHooks />
8+
</Partial>
9+
);
10+
}

packages/plugin-vite/src/plugins/server_snapshot.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,16 @@ export function serverSnapshot(options: ResolvedFreshViteConfig): Plugin[] {
142142
const mod = manifest[id];
143143
const serverPath = path.join(root, mod.src ?? id);
144144

145+
let spec = pathToSpec(clientOutDir, mod.file);
146+
147+
if (spec.startsWith("./")) {
148+
spec = spec.slice(1);
149+
}
150+
145151
const name = namer.getUniqueName(specToName(id));
146152
islandMods.push({
147153
name,
148-
browser: pathToSpec(clientOutDir, mod.file),
154+
browser: spec,
149155
server: serverPath,
150156
});
151157
}

packages/plugin-vite/tests/build_test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,3 +266,33 @@ Deno.test({
266266
sanitizeOps: false,
267267
sanitizeResources: false,
268268
});
269+
270+
Deno.test({
271+
name: "vite build - partial island",
272+
fn: async () => {
273+
await using res = await buildVite(DEMO_DIR);
274+
275+
await withChildProcessServer(
276+
{
277+
cwd: res.tmp,
278+
args: ["serve", "-A", "--port", "0", "_fresh/server.js"],
279+
},
280+
async (address) => {
281+
await withBrowser(async (page) => {
282+
await page.goto(`${address}/tests/partial`, {
283+
waitUntil: "networkidle2",
284+
});
285+
286+
await page.locator(".ready").wait();
287+
await page.locator("a").click();
288+
await page.locator(".counter-hooks").wait();
289+
290+
await page.locator(".counter-hooks button").click();
291+
await waitForText(page, ".counter-hooks button", "count: 1");
292+
});
293+
},
294+
);
295+
},
296+
sanitizeOps: false,
297+
sanitizeResources: false,
298+
});

packages/plugin-vite/tests/dev_server_test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,25 @@ Deno.test({
216216
sanitizeResources: false,
217217
sanitizeOps: false,
218218
});
219+
220+
Deno.test({
221+
name: "vite dev - partial island",
222+
fn: async () => {
223+
await withDevServer(DEMO_DIR, async (address) => {
224+
await withBrowser(async (page) => {
225+
await page.goto(`${address}/tests/partial`, {
226+
waitUntil: "networkidle2",
227+
});
228+
229+
await page.locator(".ready").wait();
230+
await page.locator("a").click();
231+
await page.locator(".counter-hooks").wait();
232+
233+
await page.locator(".counter-hooks button").click();
234+
await waitForText(page, ".counter-hooks button", "count: 1");
235+
});
236+
});
237+
},
238+
sanitizeResources: false,
239+
sanitizeOps: false,
240+
});

0 commit comments

Comments
 (0)