feat: Allow passing props to renderNotFound method (#1310)

This commit is contained in:
Kamil Ogórek
2023-06-16 17:12:01 +02:00
committed by GitHub
parent 12fc103daa
commit d5a8692b78
5 changed files with 23 additions and 6 deletions
+3 -1
View File
@@ -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 });
},
+9 -2
View File
@@ -91,7 +91,7 @@ export interface HandlerContext<Data = unknown, State = Record<string, unknown>>
data?: Data,
options?: RenderOptions,
) => Response | Promise<Response>;
renderNotFound: () => Response | Promise<Response>;
renderNotFound: (data?: Data) => Response | Promise<Response>;
state: State;
}
@@ -135,13 +135,20 @@ export interface AppModule {
// --- UNKNOWN PAGE ---
export interface UnknownPageProps {
// deno-lint-ignore no-explicit-any
export interface UnknownPageProps<T = any> {
/** 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<State = Record<string, unknown>>
+7 -2
View File
@@ -1,5 +1,10 @@
import { UnknownPageProps } from "$fresh/server.ts";
export default function NotFoundPage({ url }: UnknownPageProps) {
return <p>404 not found: {url.pathname}</p>;
export default function NotFoundPage({ data, url }: UnknownPageProps) {
return (
<>
<p>404 not found: {url.pathname}</p>
{data?.hello && <p>Hello {data.hello}</p>}
</>
);
}
+3 -1
View File
@@ -2,6 +2,8 @@ import { Handlers } from "../../../server.ts";
export const handler: Handlers = {
GET(_req, ctx) {
return ctx.renderNotFound();
return ctx.renderNotFound({
hello: "Dino",
});
},
};
+1
View File
@@ -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");
},
});