fix(ssr): invalid HTML comment tags (#1260)

This commit is contained in:
Marvin Hagemeister
2023-06-09 11:21:36 +02:00
committed by GitHub
parent 347e353357
commit 5724b73e71
23 changed files with 179 additions and 18 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
"twind/": "https://esm.sh/twind@0.16.19/",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3",
"$std/": "https://deno.land/std@0.190.0/"
+1 -1
View File
@@ -7,7 +7,7 @@
"$fresh/": "../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
+1 -1
View File
@@ -65,7 +65,7 @@ Fresh 1.0.2 to 1.1.3, and update Preact to the latest version:
+ "preact/": "https://esm.sh/preact@10.11.0/",
- "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.0",
+ "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3",
+ "preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"twind": "https://esm.sh/twind@0.16.17",
"twind/": "https://esm.sh/twind@0.16.17/"
+1 -1
View File
@@ -1,5 +1,5 @@
export const RECOMMENDED_PREACT_VERSION = "10.15.1";
export const RECOMMENDED_PREACT_RTS_VERSION = "6.0.3";
export const RECOMMENDED_PREACT_RTS_VERSION = "6.1.0";
export const RECOMMENDED_PREACT_SIGNALS_VERSION = "1.1.3";
export const RECOMMENDED_PREACT_SIGNALS_CORE_VERSION = "1.2.3";
export const RECOMMENDED_TWIND_VERSION = "0.16.19";
+35 -6
View File
@@ -1,5 +1,5 @@
import { renderToString } from "preact-render-to-string";
import { ComponentChildren, ComponentType, h, options } from "preact";
import { ComponentChildren, ComponentType, Fragment, h, options } from "preact";
import {
AppModule,
ErrorPage,
@@ -409,6 +409,17 @@ export function template(opts: TemplateOptions): string {
return "<!DOCTYPE html>" + renderToString(page);
}
const supportsUnstableComments = renderToString(h(Fragment, {
// @ts-ignore unstable features not supported in types
UNSTABLE_comment: "foo",
})) !== "";
if (!supportsUnstableComments) {
console.warn(
"⚠️ Found old version of 'preact-render-to-string'. Please upgrade it to >=6.1.0",
);
}
// Set up a preact option hook to track when vnode with custom functions are
// created.
const ISLANDS: Island[] = [];
@@ -431,11 +442,29 @@ options.vnode = (vnode) => {
ignoreNext = true;
const child = h(originalType, props);
ISLAND_PROPS.push(props);
return h(
`!--frsh-${island.id}:${ISLAND_PROPS.length - 1}--`,
null,
child,
);
// Newer versions of preact-render-to-string allow you to render comments
if (supportsUnstableComments) {
return h(
Fragment,
null,
h(Fragment, {
// @ts-ignore unstable property is not typed
UNSTABLE_comment: `frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
}),
child,
h(Fragment, {
// @ts-ignore unstable property is not typed
UNSTABLE_comment: `/frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
}),
);
} else {
return h(
`!--frsh-${island.id}:${ISLAND_PROPS.length - 1}--`,
null,
child,
);
}
};
}
}
+1 -1
View File
@@ -4,7 +4,7 @@
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
+1 -1
View File
@@ -4,7 +4,7 @@
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3"
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0"
},
"compilerOptions": {
"jsx": "react-jsx",
+1 -1
View File
@@ -4,6 +4,6 @@
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/preact-render-to-string@6.0.3?external=preact"
"preact-render-to-string": "https://esm.sh/preact-render-to-string@6.1.0?external=preact"
}
}
+1 -1
View File
@@ -4,7 +4,7 @@
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
+1 -1
View File
@@ -4,7 +4,7 @@
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3"
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0"
},
"compilerOptions": {
"jsx": "react-jsx",
+18
View File
@@ -0,0 +1,18 @@
{
"lock": false,
"tasks": {
"start": "deno run -A --watch=static/,routes/ dev.ts"
},
"imports": {
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.4",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3"
},
"compilerOptions": {
"jsx": "react-jsx",
"jsxImportSource": "preact"
}
}
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env -S deno run -A --watch=static/,routes/
import dev from "$fresh/dev.ts";
await dev(import.meta.url, "./main.ts");
+18
View File
@@ -0,0 +1,18 @@
// DO NOT EDIT. This file is generated by fresh.
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Test.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {
"./islands/Test.tsx": $$0,
},
baseUrl: import.meta.url,
};
export default manifest;
@@ -0,0 +1,13 @@
import { useEffect } from "preact/hooks";
export default function Test() {
useEffect(() => {
document.getElementById("foo")!.textContent = "it works";
}, []);
return (
<p id="foo">
it doesn't work
</p>
);
}
+13
View File
@@ -0,0 +1,13 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
import options from "./options.ts";
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
await start(routes, { ...options, experimentalDenoServe });
+16
View File
@@ -0,0 +1,16 @@
// Simulate Deno Deploy environment
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
import "./polyfill_deno_deploy.ts";
import { start } from "$fresh/server.ts";
import routes from "./fresh.gen.ts";
import options from "./options.ts";
const experimentalDenoServe = Deno.args.includes("--experimental-deno-serve");
await start(routes, { ...options, experimentalDenoServe });
+11
View File
@@ -0,0 +1,11 @@
import { FreshOptions } from "$fresh/server.ts";
export default {
async render(_ctx, render) {
await new Promise<void>((r) => r());
const body = render();
if (typeof body !== "string") {
throw new Error("body is missing");
}
},
} as FreshOptions;
@@ -0,0 +1,5 @@
// Simulate Deno Deploy environment
//@ts-ignore: Remove Deno.run for simulating deno deploy env
// deno-lint-ignore no-deprecated-deno-api
delete Deno.run;
@@ -0,0 +1,9 @@
import Test from "../islands/Test.tsx";
export default function Home() {
return (
<div>
<Test />
</div>
);
}
+1 -1
View File
@@ -4,7 +4,7 @@
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3"
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0"
},
"compilerOptions": {
"jsx": "react-jsx",
+1 -1
View File
@@ -7,7 +7,7 @@
"$fresh/": "../../",
"preact": "https://esm.sh/preact@10.11.0",
"preact/": "https://esm.sh/preact@10.11.0/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.4",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.0.3",
"@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.0.1"
},
+24
View File
@@ -1,5 +1,6 @@
import {
assert,
assertEquals,
assertStringIncludes,
delay,
Page,
@@ -245,3 +246,26 @@ Deno.test({
sanitizeOps: false,
sanitizeResources: false,
});
Deno.test({
name: "works with older preact-render-to-string v5",
async fn(_t) {
await withPageName(
"./tests/fixture_preact_rts_v5/main.ts",
async (page) => {
await page.goto("http://localhost:8000/", {
waitUntil: "networkidle2",
});
await page.waitForSelector("#foo");
await delay(100);
const text = await page.$eval("#foo", (el) => el.textContent);
assertEquals(text, "it works");
},
);
},
sanitizeOps: false,
sanitizeResources: false,
});
+1 -1
View File
@@ -9,7 +9,7 @@
"twind/": "https://esm.sh/twind@0.16.19/",
"preact": "https://esm.sh/preact@10.15.1",
"preact/": "https://esm.sh/preact@10.15.1/",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.0.3",
"preact-render-to-string": "https://esm.sh/*preact-render-to-string@6.1.0",
"@preact/signals": "https://esm.sh/*@preact/signals@1.1.3",
"@preact/signals-core": "https://esm.sh/@preact/signals-core@1.2.3",
"$std/": "https://deno.land/std@0.190.0/",