fix: conditionally rendered element under fragment in island does not render (#1172)
This might be fixing #502, but I am not sure. All tests pass. I've added additional test cases for comments from #502 --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
+8
-8
@@ -6,20 +6,20 @@ function createRootFragment(
|
||||
replaceNode: Node | Node[],
|
||||
) {
|
||||
replaceNode = ([] as Node[]).concat(replaceNode);
|
||||
const s = replaceNode[replaceNode.length - 1].nextSibling;
|
||||
function insert(c: Node, r: Node) {
|
||||
parent.insertBefore(c, r || s);
|
||||
}
|
||||
// @ts-ignore this is fine
|
||||
return parent.__k = {
|
||||
nodeType: 1,
|
||||
parentNode: parent,
|
||||
firstChild: replaceNode[0],
|
||||
childNodes: replaceNode,
|
||||
insertBefore: insert,
|
||||
appendChild: insert,
|
||||
removeChild: function (c: Node) {
|
||||
parent.removeChild(c);
|
||||
insertBefore(node: Node, child: Node) {
|
||||
parent.insertBefore(node, child);
|
||||
},
|
||||
appendChild(child: Node) {
|
||||
parent.appendChild(child);
|
||||
},
|
||||
removeChild(child: Node) {
|
||||
parent.removeChild(child);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
+4
-1
@@ -14,4 +14,7 @@ export {
|
||||
} from "https://deno.land/std@0.178.0/streams/text_line_stream.ts";
|
||||
export { delay } from "https://deno.land/std@0.178.0/async/delay.ts";
|
||||
export { retry } from "https://deno.land/std@0.178.0/async/retry.ts";
|
||||
export { default as puppeteer } from "https://deno.land/x/puppeteer@16.2.0/mod.ts";
|
||||
export {
|
||||
default as puppeteer,
|
||||
Page,
|
||||
} from "https://deno.land/x/puppeteer@16.2.0/mod.ts";
|
||||
|
||||
+38
-30
@@ -18,22 +18,26 @@ 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/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/index.ts";
|
||||
import * as $20 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $21 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $22 from "./routes/middleware_root.ts";
|
||||
import * as $23 from "./routes/not_found.ts";
|
||||
import * as $24 from "./routes/params.tsx";
|
||||
import * as $25 from "./routes/props/[id].tsx";
|
||||
import * as $26 from "./routes/static.tsx";
|
||||
import * as $27 from "./routes/wildcard.tsx";
|
||||
import * as $15 from "./routes/islands/root_fragment.tsx";
|
||||
import * as $16 from "./routes/islands/root_fragment_conditional_first.tsx";
|
||||
import * as $17 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $18 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $19 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $20 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $21 from "./routes/layeredMdw/layer2/index.ts";
|
||||
import * as $22 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $23 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $24 from "./routes/middleware_root.ts";
|
||||
import * as $25 from "./routes/not_found.ts";
|
||||
import * as $26 from "./routes/params.tsx";
|
||||
import * as $27 from "./routes/props/[id].tsx";
|
||||
import * as $28 from "./routes/static.tsx";
|
||||
import * as $29 from "./routes/wildcard.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/Test.tsx";
|
||||
import * as $$2 from "./islands/kebab-case-counter-test.tsx";
|
||||
import * as $$1 from "./islands/RootFragment.tsx";
|
||||
import * as $$2 from "./islands/RootFragmentWithConditionalFirst.tsx";
|
||||
import * as $$3 from "./islands/Test.tsx";
|
||||
import * as $$4 from "./islands/kebab-case-counter-test.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
@@ -52,24 +56,28 @@ const manifest = {
|
||||
"./routes/intercept.tsx": $12,
|
||||
"./routes/intercept_args.tsx": $13,
|
||||
"./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/index.ts": $19,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $20,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $21,
|
||||
"./routes/middleware_root.ts": $22,
|
||||
"./routes/not_found.ts": $23,
|
||||
"./routes/params.tsx": $24,
|
||||
"./routes/props/[id].tsx": $25,
|
||||
"./routes/static.tsx": $26,
|
||||
"./routes/wildcard.tsx": $27,
|
||||
"./routes/islands/root_fragment.tsx": $15,
|
||||
"./routes/islands/root_fragment_conditional_first.tsx": $16,
|
||||
"./routes/layeredMdw/_middleware.ts": $17,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $18,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $19,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $20,
|
||||
"./routes/layeredMdw/layer2/index.ts": $21,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $22,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $23,
|
||||
"./routes/middleware_root.ts": $24,
|
||||
"./routes/not_found.ts": $25,
|
||||
"./routes/params.tsx": $26,
|
||||
"./routes/props/[id].tsx": $27,
|
||||
"./routes/static.tsx": $28,
|
||||
"./routes/wildcard.tsx": $29,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
"./islands/Test.tsx": $$1,
|
||||
"./islands/kebab-case-counter-test.tsx": $$2,
|
||||
"./islands/RootFragment.tsx": $$1,
|
||||
"./islands/RootFragmentWithConditionalFirst.tsx": $$2,
|
||||
"./islands/Test.tsx": $$3,
|
||||
"./islands/kebab-case-counter-test.tsx": $$4,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
config,
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import { useState } from "preact/hooks";
|
||||
|
||||
export default function RootFragment() {
|
||||
const [shown, setShown] = useState(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
Hello
|
||||
<div onClick={() => setShown(true)} id="root-fragment-click-me">
|
||||
World
|
||||
</div>
|
||||
{shown && <div>I'm rendered now</div>}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { useState } from "preact/hooks";
|
||||
|
||||
export default function RootFragmentWithConditionalFirst() {
|
||||
const [shown, setShown] = useState(false);
|
||||
|
||||
return (
|
||||
<>
|
||||
{shown && <div>I'm rendered on top</div>}
|
||||
Hello
|
||||
<div
|
||||
onClick={() => setShown(true)}
|
||||
id="root-fragment-conditional-first-click-me"
|
||||
>
|
||||
World
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import RootFragment from "../../islands/RootFragment.tsx";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div id="island-parent">
|
||||
<RootFragment />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import RootFragmentWithConditionalFirst from "../../islands/RootFragmentWithConditionalFirst.tsx";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div id="island-parent">
|
||||
<RootFragmentWithConditionalFirst />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+170
-104
@@ -2,6 +2,7 @@ import {
|
||||
assert,
|
||||
assertStringIncludes,
|
||||
delay,
|
||||
Page,
|
||||
puppeteer,
|
||||
TextLineStream,
|
||||
} from "./deps.ts";
|
||||
@@ -9,133 +10,198 @@ import {
|
||||
Deno.test({
|
||||
name: "island tests",
|
||||
async fn(t) {
|
||||
// Preparation
|
||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
}).spawn();
|
||||
await withPage(async (page) => {
|
||||
async function counterTest(counterId: string, originalValue: number) {
|
||||
const pElem = await page.waitForSelector(`#${counterId} > p`);
|
||||
|
||||
const decoder = new TextDecoderStream();
|
||||
const lines = serverProcess.stdout
|
||||
.pipeThrough(decoder)
|
||||
.pipeThrough(new TextLineStream());
|
||||
let value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue}`, `${counterId} first value`);
|
||||
|
||||
let started = false;
|
||||
for await (const line of lines) {
|
||||
if (line.includes("Listening on http://")) {
|
||||
started = true;
|
||||
break;
|
||||
const buttonPlus = await page.$(`#b-${counterId}`);
|
||||
await buttonPlus?.click();
|
||||
|
||||
await delay(100);
|
||||
|
||||
value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue + 1}`, `${counterId} click`);
|
||||
}
|
||||
}
|
||||
if (!started) {
|
||||
|
||||
await page.goto("http://localhost:8000/islands", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("Ensure 3 islands on 1 page are revived", async () => {
|
||||
await counterTest("counter1", 3);
|
||||
await counterTest("counter2", 10);
|
||||
await counterTest("kebab-case-file-counter", 5);
|
||||
});
|
||||
|
||||
await t.step("Ensure an island revive an img 'hash' path", async () => {
|
||||
// Ensure src path has __frsh_c=
|
||||
const pElem = await page.waitForSelector(`#img-in-island`);
|
||||
const srcString = (await pElem?.getProperty("src"))?.toString()!;
|
||||
assertStringIncludes(srcString, "image.png?__frsh_c=");
|
||||
|
||||
// Ensure src path is the same as server rendered
|
||||
const resp = await fetch(new Request("http://localhost:8000/islands"));
|
||||
const body = await resp.text();
|
||||
|
||||
const imgFilePath = body.match(/img id="img-in-island" src="(.*?)"/)
|
||||
?.[1]!;
|
||||
assertStringIncludes(srcString, imgFilePath);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
async function withPage(fn: (page: Page) => Promise<void>) {
|
||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
}).spawn();
|
||||
|
||||
const textDecoderStream = new TextDecoderStream();
|
||||
const textLineStream = new TextLineStream();
|
||||
|
||||
const lines = serverProcess.stdout
|
||||
.pipeThrough(textDecoderStream)
|
||||
.pipeThrough(textLineStream);
|
||||
|
||||
try {
|
||||
if (!await didServerStart(lines)) {
|
||||
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();
|
||||
await delay(100);
|
||||
value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue + 1}`, `${counterId} click`);
|
||||
try {
|
||||
const page = await browser.newPage();
|
||||
await fn(page);
|
||||
} finally {
|
||||
await browser.close();
|
||||
}
|
||||
|
||||
await page.goto("http://localhost:8000/islands", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("Ensure 3 islands on 1 page are revived", async () => {
|
||||
await counterTest("counter1", 3);
|
||||
await counterTest("counter2", 10);
|
||||
await counterTest("kebab-case-file-counter", 5);
|
||||
});
|
||||
|
||||
await t.step("Ensure an island revive an img 'hash' path", async () => {
|
||||
// Ensure src path has __frsh_c=
|
||||
const pElem = await page.waitForSelector(`#img-in-island`);
|
||||
const srcString = (await pElem?.getProperty("src"))?.toString()!;
|
||||
assertStringIncludes(srcString, "image.png?__frsh_c=");
|
||||
|
||||
// Ensure src path is the same as server rendered
|
||||
const resp = await fetch(new Request("http://localhost:8000/islands"));
|
||||
const body = await resp.text();
|
||||
const imgFilePath = body.match(/img id="img-in-island" src="(.*?)"/)
|
||||
?.[1]!;
|
||||
assertStringIncludes(srcString, imgFilePath);
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
|
||||
} finally {
|
||||
await lines.cancel();
|
||||
|
||||
serverProcess.kill("SIGTERM");
|
||||
|
||||
// Wait until the process exits
|
||||
await serverProcess.status;
|
||||
}
|
||||
}
|
||||
|
||||
async function didServerStart(
|
||||
stdoutLines: ReadableStream<string>,
|
||||
): Promise<boolean> {
|
||||
for await (const line of stdoutLines) {
|
||||
if (line.includes("Listening on http://")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Deno.test({
|
||||
name: "island tests with </script>",
|
||||
|
||||
async fn(t) {
|
||||
await withPage(async (page) => {
|
||||
page.on("dialog", () => {
|
||||
assert(false, "There is XSS");
|
||||
});
|
||||
|
||||
await page.goto("http://localhost:8000/evil", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("prevent XSS on Island", async () => {
|
||||
const bodyElem = await page.waitForSelector(`body`);
|
||||
const value = await bodyElem?.evaluate((el) => el.getInnerHTML());
|
||||
|
||||
assertStringIncludes(
|
||||
value,
|
||||
`{"message":"\\u003c/script\\u003e\\u003cscript\\u003ealert('test')\\u003c/script\\u003e"}`,
|
||||
`XSS is not escaped`,
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "island tests with </script>",
|
||||
async fn(t) {
|
||||
// Preparation
|
||||
const serverProcess = new Deno.Command(Deno.execPath(), {
|
||||
args: ["run", "-A", "./tests/fixture/main.ts"],
|
||||
stdout: "piped",
|
||||
stderr: "inherit",
|
||||
}).spawn();
|
||||
name: "island with fragment as root",
|
||||
|
||||
const decoder = new TextDecoderStream();
|
||||
const lines = serverProcess.stdout
|
||||
.pipeThrough(decoder)
|
||||
.pipeThrough(new TextLineStream());
|
||||
async fn(_t) {
|
||||
await withPage(async (page) => {
|
||||
await page.goto("http://localhost:8000/islands/root_fragment", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
let started = false;
|
||||
for await (const line of lines) {
|
||||
if (line.includes("Listening on http://")) {
|
||||
started = true;
|
||||
break;
|
||||
const clickableSelector = "#root-fragment-click-me";
|
||||
|
||||
await page.waitForSelector(clickableSelector);
|
||||
|
||||
const contentBeforeClick = await getIslandParentTextContent();
|
||||
assert(contentBeforeClick === "HelloWorld");
|
||||
|
||||
await page.click(clickableSelector);
|
||||
await delay(100);
|
||||
|
||||
const contentAfterClick = await getIslandParentTextContent();
|
||||
assert(contentAfterClick === "HelloWorldI'm rendered now");
|
||||
|
||||
async function getIslandParentTextContent() {
|
||||
return await page.$eval(
|
||||
"#island-parent",
|
||||
(el: Element) => el.textContent,
|
||||
);
|
||||
}
|
||||
}
|
||||
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();
|
||||
page.on("dialog", () => {
|
||||
assert(false, "There is XSS");
|
||||
});
|
||||
|
||||
await page.goto("http://localhost:8000/evil", {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("prevent XSS on Island", async () => {
|
||||
const bodyElem = await page.waitForSelector(`body`);
|
||||
const value = await bodyElem?.evaluate((el) => el.getInnerHTML());
|
||||
|
||||
assertStringIncludes(
|
||||
value,
|
||||
`{"message":"\\u003c/script\\u003e\\u003cscript\\u003ealert('test')\\u003c/script\\u003e"}`,
|
||||
`XSS is not escaped`,
|
||||
);
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
|
||||
await lines.cancel();
|
||||
serverProcess.kill("SIGTERM");
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "island with fragment as root and conditional child first",
|
||||
|
||||
async fn(_t) {
|
||||
await withPage(async (page) => {
|
||||
await page.goto(
|
||||
"http://localhost:8000/islands/root_fragment_conditional_first",
|
||||
{
|
||||
waitUntil: "networkidle2",
|
||||
},
|
||||
);
|
||||
|
||||
const clickableSelector = "#root-fragment-conditional-first-click-me";
|
||||
await page.waitForSelector(clickableSelector);
|
||||
|
||||
const contentBeforeClick = await getIslandParentTextContent(page);
|
||||
assert(contentBeforeClick === "HelloWorld");
|
||||
|
||||
await page.click(clickableSelector);
|
||||
await delay(100);
|
||||
|
||||
const contentAfterClick = await getIslandParentTextContent(page);
|
||||
assert(contentAfterClick === "I'm rendered on topHelloWorld");
|
||||
});
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
async function getIslandParentTextContent(page: Page) {
|
||||
return await page.$eval("#island-parent", (el: Element) => el.textContent);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user