From 63cb208f8bec386261f292faf96884f7dfbf7bf6 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Thu, 15 May 2025 17:15:30 +0200 Subject: [PATCH] fix(core): support method handler on _error, _404 and _500 routes (#2955) This PR adds support for using method handlers in `_error`, `_404` and `_500` files. Fixes https://github.com/denoland/fresh/issues/2953 --- src/plugins/fs_routes/mod.ts | 22 ++++++++++- src/plugins/fs_routes/mod_test.tsx | 62 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/plugins/fs_routes/mod.ts b/src/plugins/fs_routes/mod.ts index c70553a7..5ead0050 100644 --- a/src/plugins/fs_routes/mod.ts +++ b/src/plugins/fs_routes/mod.ts @@ -258,7 +258,16 @@ export async function fsRoutes( ? undefined : typeof handlers === "function" ? handlers - : undefined; // FIXME: Method handler + : ((ctx) => { + const { method } = ctx.req; + if (!Array.isArray(handlers)) { + const maybeFn = handlers[method as Method]; + if (maybeFn !== undefined) { + return maybeFn(ctx); + } + } + return ctx.next(); + }) as HandlerFn; const errorComponents = components.slice(); if (mod.component !== null) { errorComponents.push(mod.component); @@ -285,7 +294,16 @@ export async function fsRoutes( ? undefined : typeof handlers === "function" ? handlers - : undefined; // FIXME: Method handler + : ((ctx) => { + const { method } = ctx.req; + if (!Array.isArray(handlers)) { + const maybeFn = handlers[method as Method]; + if (maybeFn !== undefined) { + return maybeFn(ctx); + } + } + return ctx.next(); + }) as HandlerFn; const notFoundComponents = components.slice(); if (mod.component !== null) { notFoundComponents.push(mod.component); diff --git a/src/plugins/fs_routes/mod_test.tsx b/src/plugins/fs_routes/mod_test.tsx index 40d92e8f..291356e0 100644 --- a/src/plugins/fs_routes/mod_test.tsx +++ b/src/plugins/fs_routes/mod_test.tsx @@ -646,6 +646,28 @@ Deno.test("fsRoutes - _404", async () => { expect(content).toContain("Custom 404 - Not Found"); }); +Deno.test("fsRoutes - _404 method handler", async () => { + const server = await createServer({ + "routes/_404.tsx": { + handlers: { + GET() { + return new Response("ok"); + }, + }, + }, + "routes/index.tsx": { + handlers: { + GET() { + return new Response("fail"); + }, + }, + }, + }); + + const res = await server.get("/invalid"); + expect(await res.text()).toEqual("ok"); +}); + Deno.test("fsRoutes - _500", async () => { const server = await createServer({ "routes/_500.tsx": { @@ -665,6 +687,26 @@ Deno.test("fsRoutes - _500", async () => { expect(content).toContain("Custom Error Page"); }); +Deno.test("fsRoutes - _500 method handler", async () => { + const server = await createServer({ + "routes/_500.tsx": { + handlers: { + GET() { + return new Response("ok"); + }, + }, + }, + "routes/index.tsx": { + handlers: () => { + throw new Error("fail"); + }, + }, + }); + + const res = await server.get("/"); + expect(await res.text()).toEqual("ok"); +}); + Deno.test("fsRoutes - _error", async () => { const server = await createServer({ "routes/_error.tsx": { @@ -730,6 +772,26 @@ Deno.test("fsRoutes - _error nested throw", async () => { expect(await res.text()).toEqual("ok"); }); +Deno.test("fsRoutes - _error method handler", async () => { + const server = await createServer({ + "routes/_error.tsx": { + handlers: { + GET() { + return new Response("ok"); + }, + }, + }, + "routes/index.tsx": { + handlers: () => { + throw new Error("fail"); + }, + }, + }); + + const res = await server.get("/"); + expect(await res.text()).toEqual("ok"); +}); + Deno.test("fsRoutes - _error render component", async () => { const server = await createServer({ "routes/_error.tsx": {