fix: reviving multiple islands on one page (#197)
Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
co-authored by
Luca Casonato
parent
458fe2ca3c
commit
afafb7c938
+12
-6
@@ -34,17 +34,17 @@ export function revive(islands: Record<string, ComponentType>) {
|
||||
function walk(node: Node | null) {
|
||||
const tag = node!.nodeType === 8 &&
|
||||
((node as Comment).data.match(/^\s*frsh-(.*)\s*$/) || [])[1];
|
||||
let endNode: Node | null = null;
|
||||
if (tag) {
|
||||
const startNode = node!;
|
||||
const children = [];
|
||||
const parent = node!.parentNode;
|
||||
// collect all children of the island
|
||||
while ((node = node!.nextSibling) && node.nodeType !== 8) {
|
||||
children.push(node);
|
||||
}
|
||||
startNode.parentNode!.removeChild(startNode);
|
||||
if (node) {
|
||||
node.parentNode?.removeChild(node);
|
||||
}
|
||||
startNode.parentNode!.removeChild(startNode); // remove start tag node
|
||||
|
||||
const [id, n] = tag.split(":");
|
||||
render(
|
||||
h(islands[id], ISLAND_PROPS[Number(n)]),
|
||||
@@ -54,11 +54,17 @@ export function revive(islands: Record<string, ComponentType>) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
) as any as HTMLElement,
|
||||
);
|
||||
endNode = node;
|
||||
}
|
||||
|
||||
const sib = node!.nextSibling;
|
||||
if (sib) walk(sib as Comment);
|
||||
const fc = node!.firstChild;
|
||||
if (fc) walk(fc as Comment);
|
||||
if (endNode) {
|
||||
endNode.parentNode?.removeChild(endNode); // remove end tag node
|
||||
}
|
||||
|
||||
if (sib) walk(sib);
|
||||
if (fc) walk(fc);
|
||||
}
|
||||
walk(document.body);
|
||||
}
|
||||
|
||||
+1
-1
@@ -125,7 +125,7 @@ Deno.test({
|
||||
throw new Error("Server didn't start up");
|
||||
}
|
||||
|
||||
await delay(500);
|
||||
await delay(100);
|
||||
|
||||
// Access the root page
|
||||
const res = await fetch("http://localhost:8000");
|
||||
|
||||
+28
-24
@@ -16,18 +16,20 @@ import * as $10 from "./routes/failure.ts";
|
||||
import * as $11 from "./routes/index.tsx";
|
||||
import * as $12 from "./routes/intercept.tsx";
|
||||
import * as $13 from "./routes/intercept_args.tsx";
|
||||
import * as $14 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $15 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $16 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $17 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $18 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $19 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $20 from "./routes/middleware_root.ts";
|
||||
import * as $21 from "./routes/params.tsx";
|
||||
import * as $22 from "./routes/props/[id].tsx";
|
||||
import * as $23 from "./routes/static.tsx";
|
||||
import * as $24 from "./routes/wildcard.tsx";
|
||||
import * as $$0 from "./islands/Test.tsx";
|
||||
import * as $14 from "./routes/islands/index.tsx";
|
||||
import * as $15 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $16 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $17 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $18 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $19 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $20 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $21 from "./routes/middleware_root.ts";
|
||||
import * as $22 from "./routes/params.tsx";
|
||||
import * as $23 from "./routes/props/[id].tsx";
|
||||
import * as $24 from "./routes/static.tsx";
|
||||
import * as $25 from "./routes/wildcard.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/Test.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
@@ -45,20 +47,22 @@ const manifest = {
|
||||
"./routes/index.tsx": $11,
|
||||
"./routes/intercept.tsx": $12,
|
||||
"./routes/intercept_args.tsx": $13,
|
||||
"./routes/layeredMdw/_middleware.ts": $14,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $15,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $16,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $17,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $18,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $19,
|
||||
"./routes/middleware_root.ts": $20,
|
||||
"./routes/params.tsx": $21,
|
||||
"./routes/props/[id].tsx": $22,
|
||||
"./routes/static.tsx": $23,
|
||||
"./routes/wildcard.tsx": $24,
|
||||
"./routes/islands/index.tsx": $14,
|
||||
"./routes/layeredMdw/_middleware.ts": $15,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $16,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $17,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $18,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $19,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $20,
|
||||
"./routes/middleware_root.ts": $21,
|
||||
"./routes/params.tsx": $22,
|
||||
"./routes/props/[id].tsx": $23,
|
||||
"./routes/static.tsx": $24,
|
||||
"./routes/wildcard.tsx": $25,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Test.tsx": $$0,
|
||||
"./islands/Counter.tsx": $$0,
|
||||
"./islands/Test.tsx": $$1,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/** @jsx h */
|
||||
import { h, IS_BROWSER, useState } from "$fresh/runtime.ts";
|
||||
|
||||
interface CounterProps {
|
||||
start: number;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function Counter(props: CounterProps) {
|
||||
const [count, setCount] = useState(props.start);
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => setCount(count + 1)}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { h } from "$fresh/runtime.ts";
|
||||
import Counter from "../../islands/Counter.tsx";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<Counter id="counter1" start={3} />
|
||||
<Counter id="counter2" start={10} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { assert, delay, puppeteer, TextLineStream } from "./deps.ts";
|
||||
|
||||
Deno.test({
|
||||
name: "island tests",
|
||||
async fn(t) {
|
||||
// Preparation
|
||||
const serverProcess = Deno.run({
|
||||
cmd: ["deno", "run", "-A", "--no-check", "./tests/fixture/main.ts"],
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
});
|
||||
|
||||
const decoder = new TextDecoderStream();
|
||||
const lines = serverProcess.stdout.readable
|
||||
.pipeThrough(decoder)
|
||||
.pipeThrough(new TextLineStream());
|
||||
|
||||
let started = false;
|
||||
for await (const line of lines) {
|
||||
console.log(line);
|
||||
if (line.includes("Server listening on http://")) {
|
||||
started = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!started) {
|
||||
throw new Error("Server didn't start up");
|
||||
}
|
||||
|
||||
await delay(100);
|
||||
|
||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
||||
const page = await browser.newPage();
|
||||
|
||||
async function counterTest(counterId: string, originalValue: number) {
|
||||
const pElem = await page.waitForSelector(`#${counterId} > p`);
|
||||
let value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue}`, `${counterId} first value`);
|
||||
|
||||
const buttonPlus = await page.$(`#b-${counterId}`);
|
||||
await buttonPlus?.click();
|
||||
value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue + 1}`, `${counterId} click`);
|
||||
}
|
||||
|
||||
await page.goto("http://localhost:8000/islands", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("Ensure 2 islands on 1 page are revived", async () => {
|
||||
await counterTest("counter1", 3);
|
||||
await counterTest("counter2", 10);
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
|
||||
await lines.cancel();
|
||||
serverProcess.kill("SIGTERM");
|
||||
serverProcess.close();
|
||||
},
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
@@ -265,7 +265,6 @@ Deno.test("static file - by 'hashed' path", async () => {
|
||||
// verify that the asset hook is applied to the srcset
|
||||
const imgInIslandSrcSet = body.match(/srcset="(.*?)"/)?.[1];
|
||||
assert(imgInIslandSrcSet);
|
||||
console.log(imgInIslandSrcSet);
|
||||
assert(
|
||||
imgInIslandSrcSet.includes(globalThis.__FRSH_BUILD_ID),
|
||||
"img-in-island-srcset",
|
||||
|
||||
Reference in New Issue
Block a user