From 84136667867b84735963b95d1d72da02d8c69ec2 Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Sun, 7 Jul 2024 11:09:11 -0600 Subject: [PATCH] fix: support numeric keys when rendering (#2584) I'm trying to migrate one of my projects which publishes a plugin, and this was blocking me. Looks like key is typed as `export type Key = string | number | any;`, so we might need to make more changes here in the future. --- src/plugins/fs_routes/mod_test.tsx | 22 ++++++++++++++++++++++ src/runtime/server/preact_hooks.tsx | 7 +++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/plugins/fs_routes/mod_test.tsx b/src/plugins/fs_routes/mod_test.tsx index a5b2208c..9cbd0d7c 100644 --- a/src/plugins/fs_routes/mod_test.tsx +++ b/src/plugins/fs_routes/mod_test.tsx @@ -1150,3 +1150,25 @@ Deno.test("fsRoutes - default GET route doesn't override existing handler", asyn "

true

", ); }); + +Deno.test("support numeric keys", async () => { + const TestComponent = () =>
foo
; + + const server = await createServer({ + "routes/index.tsx": { + default: () => { + return ( + <> + + + ok + + ); + }, + }, + }); + + const res = await server.get("/"); + const text = await res.text(); + expect(text).toContain("ok"); +}); diff --git a/src/runtime/server/preact_hooks.tsx b/src/runtime/server/preact_hooks.tsx index 687a37a5..fc4bd2a4 100644 --- a/src/runtime/server/preact_hooks.tsx +++ b/src/runtime/server/preact_hooks.tsx @@ -165,8 +165,11 @@ options[OptionsType.ATTR] = (name, value) => { const PATCHED = new WeakSet(); -function normalizeKey(str: string) { - return str.replaceAll(":", "_"); +function normalizeKey(key: string | number) { + if (typeof key === "number") { + key = key.toString(); + } + return key.replaceAll(":", "_"); } const oldDiff = options[OptionsType.DIFF];