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
This commit is contained in:
Marvin Hagemeister
2025-05-15 17:15:30 +02:00
committed by GitHub
parent b852fe7deb
commit 63cb208f8b
2 changed files with 82 additions and 2 deletions
+20 -2
View File
@@ -258,7 +258,16 @@ export async function fsRoutes<State>(
? 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<unknown, State>;
const errorComponents = components.slice();
if (mod.component !== null) {
errorComponents.push(mod.component);
@@ -285,7 +294,16 @@ export async function fsRoutes<State>(
? 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<unknown, State>;
const notFoundComponents = components.slice();
if (mod.component !== null) {
notFoundComponents.push(mod.component);
+62
View File
@@ -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": {