Skip to content

Commit 09705b9

Browse files
fix: bigint key values erroring (#2923)
Fixes #2802
1 parent 1cfb4bc commit 09705b9

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/plugins/fs_routes/mod_test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,3 +1336,26 @@ Deno.test("support numeric keys", async () => {
13361336
const text = await res.text();
13371337
expect(text).toContain("ok");
13381338
});
1339+
1340+
// Issue https://github.com/denoland/fresh/issues/2802
1341+
Deno.test("support bigint keys", async () => {
1342+
const TestComponent = () => <div>foo</div>;
1343+
1344+
const server = await createServer({
1345+
"routes/index.tsx": {
1346+
default: () => {
1347+
return (
1348+
<>
1349+
<TestComponent key={9007199254740991n} />
1350+
ok
1351+
</>
1352+
);
1353+
},
1354+
},
1355+
});
1356+
1357+
const res = await server.get("/");
1358+
const text = await res.text();
1359+
expect(text).toContain("ok");
1360+
expect(text).toContain("key:9007199254740991");
1361+
});

src/runtime/server/preact_hooks.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ options[OptionsType.VNODE] = (vnode) => {
121121
if (typeof vnode.type === "function") {
122122
if (vnode.type === Partial) {
123123
const props = vnode.props as PartialProps;
124-
const key = normalizeKey(vnode.key ?? "");
124+
const key = normalizeKey(vnode.key);
125125
const mode = !props.mode || props.mode === "replace"
126126
? PartialMode.Replace
127127
: props.mode === "append"
@@ -165,11 +165,10 @@ options[OptionsType.ATTR] = (name, value) => {
165165

166166
const PATCHED = new WeakSet<VNode>();
167167

168-
function normalizeKey(key: string | number) {
169-
if (typeof key === "number") {
170-
key = key.toString();
171-
}
172-
return key.replaceAll(":", "_");
168+
function normalizeKey(key: unknown): string {
169+
const value = key ?? "";
170+
const s = (typeof value !== "string") ? String(value) : value;
171+
return s.replaceAll(":", "_");
173172
}
174173

175174
const oldDiff = options[OptionsType.DIFF];
@@ -204,7 +203,7 @@ options[OptionsType.DIFF] = (vnode) => {
204203
if (island === undefined) {
205204
// Not an island, but we might need to preserve keys
206205
if (vnode.key !== undefined) {
207-
const key = normalizeKey(vnode.key ?? "");
206+
const key = normalizeKey(vnode.key);
208207
const originalType = vnode.type;
209208
vnode.type = (props) => {
210209
const child = h(originalType, props);
@@ -241,7 +240,7 @@ options[OptionsType.DIFF] = (vnode) => {
241240
const child = h(originalType, props);
242241
PATCHED.add(child);
243242

244-
const key = normalizeKey(vnode.key ?? "");
243+
const key = normalizeKey(vnode.key);
245244
return wrapWithMarker(
246245
child,
247246
"island",

0 commit comments

Comments
 (0)