From 2ec980ca454073f4b3895763a693f7cbd73761c6 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Tue, 15 Feb 2022 13:24:18 +0100 Subject: [PATCH] fix: `PageProps.params` never contains arrays, only strings (#91) --- src/runtime/types.ts | 4 ++-- tests/fixture/pages/wildcard.tsx | 15 +++++++++++++++ tests/fixture/routes.gen.ts | 22 ++++++++++++---------- tests/main_test.ts | 14 +++++++++++--- 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 tests/fixture/pages/wildcard.tsx diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 3aff83a2..b16389bc 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -14,9 +14,9 @@ export interface PageProps< * For the `/foo/:bar` route with url `/foo/123`, `params` would be * `{ bar: '123' }`. For a route with no matchers, `params` would be `{}`. For * a wildcard route, like `/foo/:path*` with url `/foo/bar/baz`, `params` would - * be `{ path: [ 'bar', 'baz' ] }`. + * be `{ path: 'bar/baz' }`. */ - params: Record; + params: Record; /** * Additional data passed into `HandlerContext.render`. Not present by diff --git a/tests/fixture/pages/wildcard.tsx b/tests/fixture/pages/wildcard.tsx new file mode 100644 index 00000000..41b0a266 --- /dev/null +++ b/tests/fixture/pages/wildcard.tsx @@ -0,0 +1,15 @@ +/** @jsx h */ + +import { h, PageConfig, PageProps } from "../deps.ts"; + +export default function WildcardPage({ params }: PageProps) { + if (typeof params.path === "string") { + return

{params.path}

; + } else { + return

Not a string.

; + } +} + +export const config: PageConfig = { + routeOverride: "/foo/:path*", +}; diff --git a/tests/fixture/routes.gen.ts b/tests/fixture/routes.gen.ts index 4aac94ea..cc90f17e 100644 --- a/tests/fixture/routes.gen.ts +++ b/tests/fixture/routes.gen.ts @@ -10,11 +10,12 @@ import * as $4 from "./pages/props/[id].tsx"; import * as $5 from "./pages/failure.ts"; import * as $6 from "./pages/_middleware.ts"; import * as $7 from "./pages/books/[id].tsx"; -import * as $8 from "./pages/api/get_only.ts"; -import * as $9 from "./pages/_500.tsx"; -import * as $10 from "./pages/intercept.tsx"; -import * as $11 from "./pages/_404.tsx"; -import * as $12 from "./pages/[name].tsx"; +import * as $8 from "./pages/wildcard.tsx"; +import * as $9 from "./pages/api/get_only.ts"; +import * as $10 from "./pages/_500.tsx"; +import * as $11 from "./pages/intercept.tsx"; +import * as $12 from "./pages/_404.tsx"; +import * as $13 from "./pages/[name].tsx"; const routes = { pages: { @@ -26,11 +27,12 @@ const routes = { "./pages/failure.ts": $5, "./pages/_middleware.ts": $6, "./pages/books/[id].tsx": $7, - "./pages/api/get_only.ts": $8, - "./pages/_500.tsx": $9, - "./pages/intercept.tsx": $10, - "./pages/_404.tsx": $11, - "./pages/[name].tsx": $12, + "./pages/wildcard.tsx": $8, + "./pages/api/get_only.ts": $9, + "./pages/_500.tsx": $10, + "./pages/intercept.tsx": $11, + "./pages/_404.tsx": $12, + "./pages/[name].tsx": $13, }, baseUrl: import.meta.url, }; diff --git a/tests/main_test.ts b/tests/main_test.ts index 7def112c..ecbe1b98 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -39,20 +39,20 @@ Deno.test("/props/123 page prerender", async () => { }); Deno.test("/[name] page prerender", async () => { - const resp = await router(new Request("https://fresh.deno.dev/foo")); + const resp = await router(new Request("https://fresh.deno.dev/bar")); assert(resp); assertEquals(resp.status, 200); assertEquals(resp.headers.get("content-type"), "text/html; charset=utf-8"); const body = await resp.text(); assertStringIncludes(body, "[name].js"); - assertStringIncludes(body, "
Hello foo
"); + assertStringIncludes(body, "
Hello bar
"); assertStringIncludes( body, `