From 3b73449a935d2b1c8131dfd54796eb7cca8d6be5 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 4 Oct 2023 22:07:06 +0200 Subject: [PATCH] fix: only render keyed comments when needed (#1859) --- src/server/rendering/preact_hooks.ts | 15 +++- src/server/rendering/state.ts | 2 + tests/fixture_partials/fresh.gen.ts | 74 ++++++++++--------- .../routes/keys_outside/index.tsx | 15 ++++ tests/partials_test.ts | 8 ++ 5 files changed, 77 insertions(+), 37 deletions(-) create mode 100644 tests/fixture_partials/routes/keys_outside/index.tsx diff --git a/src/server/rendering/preact_hooks.ts b/src/server/rendering/preact_hooks.ts index 769ba25f..44b29f04 100644 --- a/src/server/rendering/preact_hooks.ts +++ b/src/server/rendering/preact_hooks.ts @@ -286,6 +286,8 @@ options.__b = (vnode: VNode>) => { island && !patched.has(vnode) ) { + current.islandDepth++; + // Check if an island is rendered inside another island, not just // passed as a child.In that case we treat it like a normal // Component. Example: @@ -359,6 +361,7 @@ options.__b = (vnode: VNode>) => { // deno-lint-ignore no-explicit-any } else if (vnode.type === (Partial as any)) { current.partialCount++; + current.partialDepth++; if (hasIslandOwner(current, vnode)) { throw new Error( ` components cannot be used inside islands.`, @@ -373,7 +376,9 @@ options.__b = (vnode: VNode>) => { vnode.props.children, `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); vnode.type = Fragment; vnode.props = { @@ -396,6 +401,14 @@ options.__r = (vnode) => { options.diffed = (vnode: VNode>) => { if (typeof vnode.type === "function") { if (vnode.type !== Fragment) { + if (current) { + if (islandByComponent.has(vnode.type)) { + current.islandDepth--; + } else if (vnode.type === Partial as ComponentType) { + current.partialDepth--; + } + } + ownerStack.pop(); } else if (vnode.props.__freshHead) { if (current) { diff --git a/src/server/rendering/state.ts b/src/server/rendering/state.ts index 67fbec64..d953b230 100644 --- a/src/server/rendering/state.ts +++ b/src/server/rendering/state.ts @@ -40,6 +40,8 @@ export class RenderState { error: Error | null = null; isPartial: boolean; partialCount = 0; + partialDepth = 0; + islandDepth = 0; url: URL; constructor( diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts index d682bc98..7e3dfbc4 100644 --- a/tests/fixture_partials/fresh.gen.ts +++ b/tests/fixture_partials/fresh.gen.ts @@ -57,24 +57,25 @@ import * as $51 from "./routes/keys_components/swap.tsx"; import * as $52 from "./routes/keys_dom/index.tsx"; import * as $53 from "./routes/keys_dom/injected.tsx"; import * as $54 from "./routes/keys_dom/swap.tsx"; -import * as $55 from "./routes/loading/index.tsx"; -import * as $56 from "./routes/loading/injected.tsx"; -import * as $57 from "./routes/loading/update.tsx"; -import * as $58 from "./routes/missing_partial/index.tsx"; -import * as $59 from "./routes/missing_partial/injected.tsx"; -import * as $60 from "./routes/missing_partial/update.tsx"; -import * as $61 from "./routes/mode/append.tsx"; -import * as $62 from "./routes/mode/index.tsx"; -import * as $63 from "./routes/mode/injected.tsx"; -import * as $64 from "./routes/mode/prepend.tsx"; -import * as $65 from "./routes/mode/replace.tsx"; -import * as $66 from "./routes/no_islands/index.tsx"; -import * as $67 from "./routes/no_islands/injected.tsx"; -import * as $68 from "./routes/no_islands/update.tsx"; -import * as $69 from "./routes/no_partial_response/index.tsx"; -import * as $70 from "./routes/no_partial_response/injected.tsx"; -import * as $71 from "./routes/no_partial_response/update.tsx"; -import * as $72 from "./routes/partial_slot_inside_island.tsx"; +import * as $55 from "./routes/keys_outside/index.tsx"; +import * as $56 from "./routes/loading/index.tsx"; +import * as $57 from "./routes/loading/injected.tsx"; +import * as $58 from "./routes/loading/update.tsx"; +import * as $59 from "./routes/missing_partial/index.tsx"; +import * as $60 from "./routes/missing_partial/injected.tsx"; +import * as $61 from "./routes/missing_partial/update.tsx"; +import * as $62 from "./routes/mode/append.tsx"; +import * as $63 from "./routes/mode/index.tsx"; +import * as $64 from "./routes/mode/injected.tsx"; +import * as $65 from "./routes/mode/prepend.tsx"; +import * as $66 from "./routes/mode/replace.tsx"; +import * as $67 from "./routes/no_islands/index.tsx"; +import * as $68 from "./routes/no_islands/injected.tsx"; +import * as $69 from "./routes/no_islands/update.tsx"; +import * as $70 from "./routes/no_partial_response/index.tsx"; +import * as $71 from "./routes/no_partial_response/injected.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 $$1 from "./islands/CounterA.tsx"; import * as $$2 from "./islands/CounterB.tsx"; @@ -147,24 +148,25 @@ const manifest = { "./routes/keys_dom/index.tsx": $52, "./routes/keys_dom/injected.tsx": $53, "./routes/keys_dom/swap.tsx": $54, - "./routes/loading/index.tsx": $55, - "./routes/loading/injected.tsx": $56, - "./routes/loading/update.tsx": $57, - "./routes/missing_partial/index.tsx": $58, - "./routes/missing_partial/injected.tsx": $59, - "./routes/missing_partial/update.tsx": $60, - "./routes/mode/append.tsx": $61, - "./routes/mode/index.tsx": $62, - "./routes/mode/injected.tsx": $63, - "./routes/mode/prepend.tsx": $64, - "./routes/mode/replace.tsx": $65, - "./routes/no_islands/index.tsx": $66, - "./routes/no_islands/injected.tsx": $67, - "./routes/no_islands/update.tsx": $68, - "./routes/no_partial_response/index.tsx": $69, - "./routes/no_partial_response/injected.tsx": $70, - "./routes/no_partial_response/update.tsx": $71, - "./routes/partial_slot_inside_island.tsx": $72, + "./routes/keys_outside/index.tsx": $55, + "./routes/loading/index.tsx": $56, + "./routes/loading/injected.tsx": $57, + "./routes/loading/update.tsx": $58, + "./routes/missing_partial/index.tsx": $59, + "./routes/missing_partial/injected.tsx": $60, + "./routes/missing_partial/update.tsx": $61, + "./routes/mode/append.tsx": $62, + "./routes/mode/index.tsx": $63, + "./routes/mode/injected.tsx": $64, + "./routes/mode/prepend.tsx": $65, + "./routes/mode/replace.tsx": $66, + "./routes/no_islands/index.tsx": $67, + "./routes/no_islands/injected.tsx": $68, + "./routes/no_islands/update.tsx": $69, + "./routes/no_partial_response/index.tsx": $70, + "./routes/no_partial_response/injected.tsx": $71, + "./routes/no_partial_response/update.tsx": $72, + "./routes/partial_slot_inside_island.tsx": $73, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture_partials/routes/keys_outside/index.tsx b/tests/fixture_partials/routes/keys_outside/index.tsx new file mode 100644 index 00000000..24565b30 --- /dev/null +++ b/tests/fixture_partials/routes/keys_outside/index.tsx @@ -0,0 +1,15 @@ +import { ComponentChildren } from "preact"; + +export default function SlotDemo() { + return ( +
+ A + B + C +
+ ); +} + +function Foo(props: { children?: ComponentChildren }) { + return

{props.children}

; +} diff --git a/tests/partials_test.ts b/tests/partials_test.ts index 631e2923..f7118eb2 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -1,6 +1,7 @@ import { assertEquals, assertMatch } from "$std/testing/asserts.ts"; import { Page } from "./deps.ts"; import { + assertNoComments, assertNoPageComments, assertNotSelector, 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 () => { await withPageName( "./tests/fixture_partials/main.ts",