fix: only render keyed comments when needed (#1859)

This commit is contained in:
Marvin Hagemeister
2023-10-04 22:07:06 +02:00
committed by GitHub
parent 0e58c6b475
commit 3b73449a93
5 changed files with 77 additions and 37 deletions
+14 -1
View File
@@ -286,6 +286,8 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
island && island &&
!patched.has(vnode) !patched.has(vnode)
) { ) {
current.islandDepth++;
// Check if an island is rendered inside another island, not just // Check if an island is rendered inside another island, not just
// passed as a child.In that case we treat it like a normal // passed as a child.In that case we treat it like a normal
// Component. Example: // Component. Example:
@@ -359,6 +361,7 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
// deno-lint-ignore no-explicit-any // deno-lint-ignore no-explicit-any
} else if (vnode.type === (Partial as any)) { } else if (vnode.type === (Partial as any)) {
current.partialCount++; current.partialCount++;
current.partialDepth++;
if (hasIslandOwner(current, vnode)) { if (hasIslandOwner(current, vnode)) {
throw new Error( throw new Error(
`<Partial> components cannot be used inside islands.`, `<Partial> components cannot be used inside islands.`,
@@ -373,7 +376,9 @@ options.__b = (vnode: VNode<Record<string, unknown>>) => {
vnode.props.children, vnode.props.children,
`frsh-partial:${vnode.props.name}:${mode}:${vnode.key ?? ""}`, `frsh-partial:${vnode.props.name}:${mode}:${vnode.key ?? ""}`,
); );
} else if (vnode.key) { } else if (
vnode.key && (current.islandDepth > 0 || current.partialDepth > 0)
) {
const child = h(vnode.type, vnode.props); const child = h(vnode.type, vnode.props);
vnode.type = Fragment; vnode.type = Fragment;
vnode.props = { vnode.props = {
@@ -396,6 +401,14 @@ options.__r = (vnode) => {
options.diffed = (vnode: VNode<Record<string, unknown>>) => { options.diffed = (vnode: VNode<Record<string, unknown>>) => {
if (typeof vnode.type === "function") { if (typeof vnode.type === "function") {
if (vnode.type !== Fragment) { if (vnode.type !== Fragment) {
if (current) {
if (islandByComponent.has(vnode.type)) {
current.islandDepth--;
} else if (vnode.type === Partial as ComponentType) {
current.partialDepth--;
}
}
ownerStack.pop(); ownerStack.pop();
} else if (vnode.props.__freshHead) { } else if (vnode.props.__freshHead) {
if (current) { if (current) {
+2
View File
@@ -40,6 +40,8 @@ export class RenderState {
error: Error | null = null; error: Error | null = null;
isPartial: boolean; isPartial: boolean;
partialCount = 0; partialCount = 0;
partialDepth = 0;
islandDepth = 0;
url: URL; url: URL;
constructor( constructor(
+38 -36
View File
@@ -57,24 +57,25 @@ import * as $51 from "./routes/keys_components/swap.tsx";
import * as $52 from "./routes/keys_dom/index.tsx"; import * as $52 from "./routes/keys_dom/index.tsx";
import * as $53 from "./routes/keys_dom/injected.tsx"; import * as $53 from "./routes/keys_dom/injected.tsx";
import * as $54 from "./routes/keys_dom/swap.tsx"; import * as $54 from "./routes/keys_dom/swap.tsx";
import * as $55 from "./routes/loading/index.tsx"; import * as $55 from "./routes/keys_outside/index.tsx";
import * as $56 from "./routes/loading/injected.tsx"; import * as $56 from "./routes/loading/index.tsx";
import * as $57 from "./routes/loading/update.tsx"; import * as $57 from "./routes/loading/injected.tsx";
import * as $58 from "./routes/missing_partial/index.tsx"; import * as $58 from "./routes/loading/update.tsx";
import * as $59 from "./routes/missing_partial/injected.tsx"; import * as $59 from "./routes/missing_partial/index.tsx";
import * as $60 from "./routes/missing_partial/update.tsx"; import * as $60 from "./routes/missing_partial/injected.tsx";
import * as $61 from "./routes/mode/append.tsx"; import * as $61 from "./routes/missing_partial/update.tsx";
import * as $62 from "./routes/mode/index.tsx"; import * as $62 from "./routes/mode/append.tsx";
import * as $63 from "./routes/mode/injected.tsx"; import * as $63 from "./routes/mode/index.tsx";
import * as $64 from "./routes/mode/prepend.tsx"; import * as $64 from "./routes/mode/injected.tsx";
import * as $65 from "./routes/mode/replace.tsx"; import * as $65 from "./routes/mode/prepend.tsx";
import * as $66 from "./routes/no_islands/index.tsx"; import * as $66 from "./routes/mode/replace.tsx";
import * as $67 from "./routes/no_islands/injected.tsx"; import * as $67 from "./routes/no_islands/index.tsx";
import * as $68 from "./routes/no_islands/update.tsx"; import * as $68 from "./routes/no_islands/injected.tsx";
import * as $69 from "./routes/no_partial_response/index.tsx"; import * as $69 from "./routes/no_islands/update.tsx";
import * as $70 from "./routes/no_partial_response/injected.tsx"; import * as $70 from "./routes/no_partial_response/index.tsx";
import * as $71 from "./routes/no_partial_response/update.tsx"; import * as $71 from "./routes/no_partial_response/injected.tsx";
import * as $72 from "./routes/partial_slot_inside_island.tsx"; import * as $72 from "./routes/no_partial_response/update.tsx";
import * as $73 from "./routes/partial_slot_inside_island.tsx";
import * as $$0 from "./islands/Counter.tsx"; import * as $$0 from "./islands/Counter.tsx";
import * as $$1 from "./islands/CounterA.tsx"; import * as $$1 from "./islands/CounterA.tsx";
import * as $$2 from "./islands/CounterB.tsx"; import * as $$2 from "./islands/CounterB.tsx";
@@ -147,24 +148,25 @@ const manifest = {
"./routes/keys_dom/index.tsx": $52, "./routes/keys_dom/index.tsx": $52,
"./routes/keys_dom/injected.tsx": $53, "./routes/keys_dom/injected.tsx": $53,
"./routes/keys_dom/swap.tsx": $54, "./routes/keys_dom/swap.tsx": $54,
"./routes/loading/index.tsx": $55, "./routes/keys_outside/index.tsx": $55,
"./routes/loading/injected.tsx": $56, "./routes/loading/index.tsx": $56,
"./routes/loading/update.tsx": $57, "./routes/loading/injected.tsx": $57,
"./routes/missing_partial/index.tsx": $58, "./routes/loading/update.tsx": $58,
"./routes/missing_partial/injected.tsx": $59, "./routes/missing_partial/index.tsx": $59,
"./routes/missing_partial/update.tsx": $60, "./routes/missing_partial/injected.tsx": $60,
"./routes/mode/append.tsx": $61, "./routes/missing_partial/update.tsx": $61,
"./routes/mode/index.tsx": $62, "./routes/mode/append.tsx": $62,
"./routes/mode/injected.tsx": $63, "./routes/mode/index.tsx": $63,
"./routes/mode/prepend.tsx": $64, "./routes/mode/injected.tsx": $64,
"./routes/mode/replace.tsx": $65, "./routes/mode/prepend.tsx": $65,
"./routes/no_islands/index.tsx": $66, "./routes/mode/replace.tsx": $66,
"./routes/no_islands/injected.tsx": $67, "./routes/no_islands/index.tsx": $67,
"./routes/no_islands/update.tsx": $68, "./routes/no_islands/injected.tsx": $68,
"./routes/no_partial_response/index.tsx": $69, "./routes/no_islands/update.tsx": $69,
"./routes/no_partial_response/injected.tsx": $70, "./routes/no_partial_response/index.tsx": $70,
"./routes/no_partial_response/update.tsx": $71, "./routes/no_partial_response/injected.tsx": $71,
"./routes/partial_slot_inside_island.tsx": $72, "./routes/no_partial_response/update.tsx": $72,
"./routes/partial_slot_inside_island.tsx": $73,
}, },
islands: { islands: {
"./islands/Counter.tsx": $$0, "./islands/Counter.tsx": $$0,
@@ -0,0 +1,15 @@
import { ComponentChildren } from "preact";
export default function SlotDemo() {
return (
<div>
<Foo key="A">A</Foo>
<Foo key="B">B</Foo>
<Foo key="C">C</Foo>
</div>
);
}
function Foo(props: { children?: ComponentChildren }) {
return <p>{props.children}</p>;
}
+8
View File
@@ -1,6 +1,7 @@
import { assertEquals, assertMatch } from "$std/testing/asserts.ts"; import { assertEquals, assertMatch } from "$std/testing/asserts.ts";
import { Page } from "./deps.ts"; import { Page } from "./deps.ts";
import { import {
assertNoComments,
assertNoPageComments, assertNoPageComments,
assertNotSelector, assertNotSelector,
assertSelector, assertSelector,
@@ -534,6 +535,13 @@ Deno.test("reconciles keyed non island components", async () => {
); );
}); });
Deno.test("don't serialize keys for nodes outside islands or partials", async () => {
await withFakeServe("./tests/fixture_partials/main.ts", async (server) => {
const doc = await server.getHtml("/keys_outside");
assertNoComments(doc);
});
});
Deno.test("partial injection mode", async () => { Deno.test("partial injection mode", async () => {
await withPageName( await withPageName(
"./tests/fixture_partials/main.ts", "./tests/fixture_partials/main.ts",