From d5a8692b7894b212004778623db4967a945148b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Fri, 16 Jun 2023 17:12:01 +0200 Subject: [PATCH] feat: Allow passing props to renderNotFound method (#1310) --- docs/concepts/error-pages.md | 4 +++- src/server/types.ts | 11 +++++++++-- tests/fixture/routes/_404.tsx | 9 +++++++-- tests/fixture/routes/not_found.ts | 4 +++- tests/main_test.ts | 1 + 5 files changed, 23 insertions(+), 6 deletions(-) diff --git a/docs/concepts/error-pages.md b/docs/concepts/error-pages.md index d1a4d078..dc5cd30d 100644 --- a/docs/concepts/error-pages.md +++ b/docs/concepts/error-pages.md @@ -36,7 +36,9 @@ export const handler: Handlers = { async GET(req, ctx) { const blogpost = await fetchBlogpost(ctx.params.slug); if (!blogpost) { - return ctx.renderNotFound(); + return ctx.renderNotFound({ + custom: "prop", + }); } return ctx.render({ blogpost }); }, diff --git a/src/server/types.ts b/src/server/types.ts index 760e24cc..a3559136 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -91,7 +91,7 @@ export interface HandlerContext> data?: Data, options?: RenderOptions, ) => Response | Promise; - renderNotFound: () => Response | Promise; + renderNotFound: (data?: Data) => Response | Promise; state: State; } @@ -135,13 +135,20 @@ export interface AppModule { // --- UNKNOWN PAGE --- -export interface UnknownPageProps { +// deno-lint-ignore no-explicit-any +export interface UnknownPageProps { /** The URL of the request that resulted in this page being rendered. */ url: URL; /** The route matcher (e.g. /blog/:id) that the request matched for this page * to be rendered. */ route: string; + + /** + * Additional data passed into `HandlerContext.renderNotFound`. Defaults to + * `undefined`. + */ + data: T; } export interface UnknownHandlerContext> diff --git a/tests/fixture/routes/_404.tsx b/tests/fixture/routes/_404.tsx index ca00eb96..48279ee2 100644 --- a/tests/fixture/routes/_404.tsx +++ b/tests/fixture/routes/_404.tsx @@ -1,5 +1,10 @@ import { UnknownPageProps } from "$fresh/server.ts"; -export default function NotFoundPage({ url }: UnknownPageProps) { - return

404 not found: {url.pathname}

; +export default function NotFoundPage({ data, url }: UnknownPageProps) { + return ( + <> +

404 not found: {url.pathname}

+ {data?.hello &&

Hello {data.hello}

} + + ); } diff --git a/tests/fixture/routes/not_found.ts b/tests/fixture/routes/not_found.ts index 1732de1e..b90959d5 100644 --- a/tests/fixture/routes/not_found.ts +++ b/tests/fixture/routes/not_found.ts @@ -2,6 +2,8 @@ import { Handlers } from "../../../server.ts"; export const handler: Handlers = { GET(_req, ctx) { - return ctx.renderNotFound(); + return ctx.renderNotFound({ + hello: "Dino", + }); }, }; diff --git a/tests/main_test.ts b/tests/main_test.ts index dc0ee2f1..cd398cb3 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -565,6 +565,7 @@ Deno.test({ assertEquals(resp.status, 404); const body = await resp.text(); assertStringIncludes(body, "404 not found: /not_found"); + assertStringIncludes(body, "Hello Dino"); }, });