From be467d3cdbe36a9fe662dc4819495264f2d0c21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sun, 29 Mar 2026 18:33:14 +0000 Subject: [PATCH] fix: warn when Partial with append/prepend mode is missing key prop (#3738) ## Summary - Adds a `console.warn` during SSR when a `` uses `mode="append"` or `mode="prepend"` without a `key` prop - Without a key, Preact cannot correctly reconcile prepended/appended children, leading to subtle rendering bugs - No warning for `mode="replace"` (default) since keys aren't required there --------- Co-authored-by: Claude Opus 4.6 (1M context) --- docs/latest/advanced/partials.md | 10 ++- .../fresh/src/runtime/server/preact_hooks.ts | 13 +++ packages/fresh/tests/partials_test.tsx | 89 +++++++++++++++++++ 3 files changed, 108 insertions(+), 4 deletions(-) diff --git a/docs/latest/advanced/partials.md b/docs/latest/advanced/partials.md index ac891a60..252bf40f 100644 --- a/docs/latest/advanced/partials.md +++ b/docs/latest/advanced/partials.md @@ -156,7 +156,7 @@ shops for example. export default function AddToCartPartial() { return ( <> - + {/* Render the new cart item here */} @@ -187,7 +187,7 @@ export default function LogView() { const lines = getNewLogLines(); return ( - + {lines.map((line) => { return
  • {line}
  • ; })} @@ -196,8 +196,10 @@ export default function LogView() { } ``` -> [info]: When picking the `prepend` or `append` mode, make sure to add keys to -> the elements. +> [warn]: When using `prepend` or `append` mode, you **must** add a `key` prop +> to the `` component. Without it, Preact cannot distinguish new +> children from existing ones, leading to subtle rendering bugs. Fresh will log +> a warning if it detects a missing key on an append/prepend partial. ## Bypassing or disabling Partials diff --git a/packages/fresh/src/runtime/server/preact_hooks.ts b/packages/fresh/src/runtime/server/preact_hooks.ts index 320439bb..1f591666 100644 --- a/packages/fresh/src/runtime/server/preact_hooks.ts +++ b/packages/fresh/src/runtime/server/preact_hooks.ts @@ -194,6 +194,19 @@ options[OptionsType.DIFF] = (vnode) => { ` components cannot be used inside islands.`, ); } + + const mode = (vnode.props as PartialProps).mode; + if ( + (mode === "append" || mode === "prepend") && + vnode.key == null + ) { + // deno-lint-ignore no-console + console.warn( + ` is missing a "key" prop. ` + + `Without a key, Preact cannot correctly reconcile ${mode}ed children. ` + + `Add a unique key to fix this.`, + ); + } } else if ( !PATCHED.has(vnode) ) { diff --git a/packages/fresh/tests/partials_test.tsx b/packages/fresh/tests/partials_test.tsx index aae84209..c0a2abcb 100644 --- a/packages/fresh/tests/partials_test.tsx +++ b/packages/fresh/tests/partials_test.tsx @@ -15,6 +15,7 @@ import { } from "./test_utils.tsx"; import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx"; import { expect } from "@std/expect"; +import { assertSpyCalls, spy } from "@std/testing/mock"; import { PartialInIsland } from "./fixtures_islands/PartialInIsland.tsx"; import { FakeServer } from "../src/test_utils.ts"; import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx"; @@ -2775,3 +2776,91 @@ Deno.test({ }); }, }); + +Deno.test({ + name: "partials - warns when append/prepend missing key", + fn: async () => { + const app = testApp() + .get("/", (ctx) => { + return ctx.render( + + +

    content

    +
    + +

    content

    +
    +
    , + ); + }); + + const warnSpy = spy(console, "warn"); + try { + const server = new FakeServer(app.handler()); + await server.get("/"); + assertSpyCalls(warnSpy, 2); + expect(String(warnSpy.calls[0].args[0])).toContain("no-key-append"); + expect(String(warnSpy.calls[0].args[0])).toContain("append"); + expect(String(warnSpy.calls[1].args[0])).toContain("no-key-prepend"); + expect(String(warnSpy.calls[1].args[0])).toContain("prepend"); + } finally { + warnSpy.restore(); + } + }, +}); + +Deno.test({ + name: "partials - no warning when append/prepend has key", + fn: async () => { + const app = testApp() + .get("/", (ctx) => { + return ctx.render( + + +

    content

    +
    + +

    content

    +
    +
    , + ); + }); + + const warnSpy = spy(console, "warn"); + try { + const server = new FakeServer(app.handler()); + await server.get("/"); + assertSpyCalls(warnSpy, 0); + } finally { + warnSpy.restore(); + } + }, +}); + +Deno.test({ + name: "partials - no warning for replace mode without key", + fn: async () => { + const app = testApp() + .get("/", (ctx) => { + return ctx.render( + + +

    content

    +
    + +

    content

    +
    +
    , + ); + }); + + const warnSpy = spy(console, "warn"); + try { + const server = new FakeServer(app.handler()); + await server.get("/"); + assertSpyCalls(warnSpy, 0); + } finally { + warnSpy.restore(); + } + }, +});