fix: HEAD when only GET is registered in .route() (#3587)

This commit is contained in:
Marvin Hagemeister
2025-10-30 14:59:12 +01:00
committed by GitHub
parent c7af11eb7c
commit 7fcae1d895
3 changed files with 34 additions and 6 deletions
+21
View File
@@ -788,6 +788,27 @@ Deno.test("App support HEAD if only GET is registered", async () => {
expect(res.status).toEqual(200); expect(res.status).toEqual(200);
}); });
Deno.test(
"App support HEAD in .route() when only GET is registered",
async () => {
const app = new App()
.route("/", {
handler: {
GET() {
return { data: {} };
},
},
component: () => <h1>foo</h1>,
});
const server = new FakeServer(app.handler());
const res = await server.head("/");
expect(res.body).toEqual(null);
expect(res.status).toEqual(200);
},
);
Deno.test("App - .appWrapper()", async () => { Deno.test("App - .appWrapper()", async () => {
const app = new App() const app = new App()
.appWrapper(({ Component }) => ( .appWrapper(({ Component }) => (
+2 -2
View File
@@ -93,8 +93,8 @@ Deno.test("fsRoutes - registers HTTP methods on router", async () => {
await res.body?.cancel(); await res.body?.cancel();
if (method === "GET" && other === "HEAD") { if (method === "GET" && other === "HEAD") {
// When only a HEAD route is registered it should return 404 // GET route + HEAD request should return 200
expect(res.status).toEqual(404); expect(res.status).toEqual(200);
} else { } else {
expect(res.status).toEqual(405); expect(res.status).toEqual(405);
} }
+11 -4
View File
@@ -4,7 +4,7 @@ import { type Method, patternToSegments } from "./router.ts";
import type { LayoutConfig, Route } from "./types.ts"; import type { LayoutConfig, Route } from "./types.ts";
import { type Context, getInternals } from "./context.ts"; import { type Context, getInternals } from "./context.ts";
import { recordSpanError, tracer } from "./otel.ts"; import { recordSpanError, tracer } from "./otel.ts";
import { isHandlerByMethod } from "./handlers.ts"; import { type HandlerFn, isHandlerByMethod } from "./handlers.ts";
import { import {
type AsyncAnyComponent, type AsyncAnyComponent,
type PageProps, type PageProps,
@@ -167,9 +167,16 @@ export async function renderRoute<State>(
attributes: { "fresh.span_type": "fs_routes/handler" }, attributes: { "fresh.span_type": "fs_routes/handler" },
}, async (span) => { }, async (span) => {
try { try {
const fn = isHandlerByMethod(handlers) let fn: HandlerFn<unknown, State> | null = null;
? handlers[method] ?? null if (isHandlerByMethod(handlers)) {
: handlers; if (handlers[method] !== undefined) {
fn = handlers[method];
} else if (method === "HEAD" && handlers.GET !== undefined) {
fn = handlers.GET;
}
} else {
fn = handlers;
}
if (fn === null) return await ctx.next(); if (fn === null) return await ctx.next();