From c72f534369d6ddcc6e35f69c57b2407a879eeb53 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 6 Dec 2023 09:33:36 +0100 Subject: [PATCH] fix: route param fallback not empty string (#2146) --- src/server/router.ts | 4 +--- tests/fixture/fresh.gen.ts | 2 ++ tests/fixture/routes/std.tsx | 9 +++++++++ tests/main_test.ts | 8 ++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 tests/fixture/routes/std.tsx diff --git a/src/server/router.ts b/src/server/router.ts index a362ee43..c4e5068e 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -292,9 +292,7 @@ export function getParamsAndRoute( for (const key in matched) { const value = matched[key]; - if (value !== undefined) { - groups[key] = decodeURIComponent(value); - } + groups[key] = value !== undefined ? decodeURIComponent(value) : ""; } return { route: route, diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 75aec47e..feaf1c2e 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -80,6 +80,7 @@ import * as $state_middleware_foo_middleware from "./routes/state-middleware/foo import * as $state_middleware_foo_index from "./routes/state-middleware/foo/index.tsx"; import * as $static from "./routes/static.tsx"; import * as $status_overwrite from "./routes/status_overwrite.tsx"; +import * as $std from "./routes/std.tsx"; import * as $umlaut_äöüß from "./routes/umlaut-äöüß.tsx"; import * as $wildcard from "./routes/wildcard.tsx"; import * as $Counter from "./islands/Counter.tsx"; @@ -198,6 +199,7 @@ const manifest = { "./routes/state-middleware/foo/index.tsx": $state_middleware_foo_index, "./routes/static.tsx": $static, "./routes/status_overwrite.tsx": $status_overwrite, + "./routes/std.tsx": $std, "./routes/umlaut-äöüß.tsx": $umlaut_äöüß, "./routes/wildcard.tsx": $wildcard, }, diff --git a/tests/fixture/routes/std.tsx b/tests/fixture/routes/std.tsx new file mode 100644 index 00000000..51d5a102 --- /dev/null +++ b/tests/fixture/routes/std.tsx @@ -0,0 +1,9 @@ +import { PageProps, RouteConfig } from "$fresh/server.ts"; + +export default function Page(props: PageProps) { + return
{JSON.stringify(props.params, null, 2)}
; +} + +export const config: RouteConfig = { + routeOverride: "/std{@:version}?/:path*", +}; diff --git a/tests/main_test.ts b/tests/main_test.ts index cc9aa6ab..6be9bdf5 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -1212,3 +1212,11 @@ Deno.test("Expose config in ctx", async () => { }); }); }); + +Deno.test("empty string fallback for optional params", async () => { + await withFakeServe("./tests/fixture/main.ts", async (server) => { + const doc = await server.getHtml(`/std/foo`); + const data = JSON.parse(doc.querySelector("pre")?.textContent!); + assertEquals(data, { path: "foo", version: "" }); + }); +});