feat(server): Allow for custom HEAD method implementations (#1175)
Fixes https://github.com/denoland/fresh/issues/1115 Fixes https://github.com/denoland/fresh/issues/1165 --------- Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
This commit is contained in:
co-authored by
Asher Gomez
parent
366c8afef1
commit
e86a126c0a
@@ -325,14 +325,9 @@ export class ServerContext {
|
||||
return Response.redirect(url.href, Status.TemporaryRedirect);
|
||||
}
|
||||
|
||||
// HEAD requests should be handled as GET requests but without the body.
|
||||
const originalMethod = req.method;
|
||||
// Internally, HEAD is handled in the same way as GET.
|
||||
if (req.method === "HEAD") {
|
||||
req = new Request(req.url, { method: "GET", headers: req.headers });
|
||||
}
|
||||
const res = await withMiddlewares(req, connInfo, inner);
|
||||
if (originalMethod === "HEAD") {
|
||||
// Internally, HEAD is handled in the same way as GET if not overridden.
|
||||
if (req.method === "HEAD") {
|
||||
res.body?.cancel();
|
||||
return new Response(null, {
|
||||
headers: res.headers,
|
||||
|
||||
@@ -157,6 +157,11 @@ export function router<T = unknown>(
|
||||
}
|
||||
}
|
||||
|
||||
// If not overridden, HEAD requests should be handled as GET requests but without the body.
|
||||
if (req.method === "HEAD" && !route.methods["HEAD"]) {
|
||||
req = new Request(req.url, { method: "GET", headers: req.headers });
|
||||
}
|
||||
|
||||
for (const [method, handler] of Object.entries(route.methods)) {
|
||||
if (req.method === method) {
|
||||
return {
|
||||
|
||||
+50
-48
@@ -9,30 +9,31 @@ import * as $2 from "./routes/_500.tsx";
|
||||
import * as $3 from "./routes/_app.tsx";
|
||||
import * as $4 from "./routes/_middleware.ts";
|
||||
import * as $5 from "./routes/api/get_only.ts";
|
||||
import * as $6 from "./routes/assetsCaching/index.tsx";
|
||||
import * as $7 from "./routes/books/[id].tsx";
|
||||
import * as $8 from "./routes/connInfo.ts";
|
||||
import * as $9 from "./routes/evil.tsx";
|
||||
import * as $10 from "./routes/failure.ts";
|
||||
import * as $11 from "./routes/index.tsx";
|
||||
import * as $12 from "./routes/intercept.tsx";
|
||||
import * as $13 from "./routes/intercept_args.tsx";
|
||||
import * as $14 from "./routes/islands/index.tsx";
|
||||
import * as $15 from "./routes/islands/root_fragment.tsx";
|
||||
import * as $16 from "./routes/islands/root_fragment_conditional_first.tsx";
|
||||
import * as $17 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $18 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $19 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $20 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $21 from "./routes/layeredMdw/layer2/index.ts";
|
||||
import * as $22 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $23 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $24 from "./routes/middleware_root.ts";
|
||||
import * as $25 from "./routes/not_found.ts";
|
||||
import * as $26 from "./routes/params.tsx";
|
||||
import * as $27 from "./routes/props/[id].tsx";
|
||||
import * as $28 from "./routes/static.tsx";
|
||||
import * as $29 from "./routes/wildcard.tsx";
|
||||
import * as $6 from "./routes/api/head_override.ts";
|
||||
import * as $7 from "./routes/assetsCaching/index.tsx";
|
||||
import * as $8 from "./routes/books/[id].tsx";
|
||||
import * as $9 from "./routes/connInfo.ts";
|
||||
import * as $10 from "./routes/evil.tsx";
|
||||
import * as $11 from "./routes/failure.ts";
|
||||
import * as $12 from "./routes/index.tsx";
|
||||
import * as $13 from "./routes/intercept.tsx";
|
||||
import * as $14 from "./routes/intercept_args.tsx";
|
||||
import * as $15 from "./routes/islands/index.tsx";
|
||||
import * as $16 from "./routes/islands/root_fragment.tsx";
|
||||
import * as $17 from "./routes/islands/root_fragment_conditional_first.tsx";
|
||||
import * as $18 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $19 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $20 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $21 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $22 from "./routes/layeredMdw/layer2/index.ts";
|
||||
import * as $23 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $24 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $25 from "./routes/middleware_root.ts";
|
||||
import * as $26 from "./routes/not_found.ts";
|
||||
import * as $27 from "./routes/params.tsx";
|
||||
import * as $28 from "./routes/props/[id].tsx";
|
||||
import * as $29 from "./routes/static.tsx";
|
||||
import * as $30 from "./routes/wildcard.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/RootFragment.tsx";
|
||||
import * as $$2 from "./islands/RootFragmentWithConditionalFirst.tsx";
|
||||
@@ -47,30 +48,31 @@ const manifest = {
|
||||
"./routes/_app.tsx": $3,
|
||||
"./routes/_middleware.ts": $4,
|
||||
"./routes/api/get_only.ts": $5,
|
||||
"./routes/assetsCaching/index.tsx": $6,
|
||||
"./routes/books/[id].tsx": $7,
|
||||
"./routes/connInfo.ts": $8,
|
||||
"./routes/evil.tsx": $9,
|
||||
"./routes/failure.ts": $10,
|
||||
"./routes/index.tsx": $11,
|
||||
"./routes/intercept.tsx": $12,
|
||||
"./routes/intercept_args.tsx": $13,
|
||||
"./routes/islands/index.tsx": $14,
|
||||
"./routes/islands/root_fragment.tsx": $15,
|
||||
"./routes/islands/root_fragment_conditional_first.tsx": $16,
|
||||
"./routes/layeredMdw/_middleware.ts": $17,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $18,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $19,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $20,
|
||||
"./routes/layeredMdw/layer2/index.ts": $21,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $22,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $23,
|
||||
"./routes/middleware_root.ts": $24,
|
||||
"./routes/not_found.ts": $25,
|
||||
"./routes/params.tsx": $26,
|
||||
"./routes/props/[id].tsx": $27,
|
||||
"./routes/static.tsx": $28,
|
||||
"./routes/wildcard.tsx": $29,
|
||||
"./routes/api/head_override.ts": $6,
|
||||
"./routes/assetsCaching/index.tsx": $7,
|
||||
"./routes/books/[id].tsx": $8,
|
||||
"./routes/connInfo.ts": $9,
|
||||
"./routes/evil.tsx": $10,
|
||||
"./routes/failure.ts": $11,
|
||||
"./routes/index.tsx": $12,
|
||||
"./routes/intercept.tsx": $13,
|
||||
"./routes/intercept_args.tsx": $14,
|
||||
"./routes/islands/index.tsx": $15,
|
||||
"./routes/islands/root_fragment.tsx": $16,
|
||||
"./routes/islands/root_fragment_conditional_first.tsx": $17,
|
||||
"./routes/layeredMdw/_middleware.ts": $18,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $19,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $20,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $21,
|
||||
"./routes/layeredMdw/layer2/index.ts": $22,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $23,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $24,
|
||||
"./routes/middleware_root.ts": $25,
|
||||
"./routes/not_found.ts": $26,
|
||||
"./routes/params.tsx": $27,
|
||||
"./routes/props/[id].tsx": $28,
|
||||
"./routes/static.tsx": $29,
|
||||
"./routes/wildcard.tsx": $30,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
export const handler = {
|
||||
HEAD() {
|
||||
return new Response(null, {
|
||||
status: 204,
|
||||
headers: {
|
||||
"Content-Type": "text/html; charset=utf-8",
|
||||
},
|
||||
});
|
||||
},
|
||||
GET() {
|
||||
return new Response("Get fresh!", {
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
},
|
||||
});
|
||||
},
|
||||
};
|
||||
@@ -66,6 +66,34 @@ Deno.test("/[name] page prerender", async () => {
|
||||
assertStringIncludes(body, "<div>Hello bar</div>");
|
||||
});
|
||||
|
||||
Deno.test("/intercept - HEAD", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/api/head_override", {
|
||||
method: "HEAD",
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, Status.NoContent);
|
||||
assertEquals(resp.body, null);
|
||||
assertEquals(
|
||||
resp.headers.get("content-type"),
|
||||
"text/html; charset=utf-8",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("/intercept - HEAD fallback", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/api/get_only", {
|
||||
method: "HEAD",
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, Status.OK);
|
||||
assertEquals(resp.body, null);
|
||||
assertEquals(
|
||||
resp.headers.get("content-type"),
|
||||
"application/json; charset=utf-8",
|
||||
);
|
||||
});
|
||||
|
||||
Deno.test("/intercept - GET html", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/intercept", {
|
||||
headers: { "accept": "text/html" },
|
||||
|
||||
Reference in New Issue
Block a user