Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(start): enable HEAD requests to API HEAD/GET routes #1661

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/perfect-clocks-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

fix(start): enable HEAD requests to API HEAD/GET routes
9 changes: 8 additions & 1 deletion packages/start/config/fs-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class SolidStartClientFileRouter extends BaseFileSystemRouter {
}
}

const HTTP_METHODS = ["GET", "POST", "PUT", "DELETE", "PATCH"];
const HTTP_METHODS = ["HEAD", "GET", "POST", "PUT", "DELETE", "PATCH"];
function createHTTPHandlers(src, exports) {
const handlers = {};
for (const exp of exports) {
Expand All @@ -67,8 +67,15 @@ function createHTTPHandlers(src, exports) {
src: src,
pick: [exp.n]
};
if (exp.n === "GET" && !exports.find(exp => exp.n === "HEAD")) {
handlers.$HEAD = {
src: src,
pick: [exp.n]
};
}
}
}

return handlers;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/start/src/router/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface Route {
children?: Route[];
page?: boolean;
$component?: any;
$HEAD?: any;
$GET?: any;
$POST?: any;
$PUT?: any;
Expand Down Expand Up @@ -57,7 +58,7 @@ function defineRoutes(fileRoutes: Route[]) {
export function matchAPIRoute(path: string, method: string) {
const match = router.lookup(path);
if (match && match.route) {
const handler = match.route[`$${method}`];
const handler = method === "HEAD" ? match.route["$HEAD"] || match.route["$GET"] : match.route[`$${method}`];
if (handler === undefined) return;
return {
handler,
Expand All @@ -67,7 +68,7 @@ export function matchAPIRoute(path: string, method: string) {
}

function containsHTTP(route: Route) {
return route["$GET"] || route["$POST"] || route["$PUT"] || route["$PATCH"] || route["$DELETE"];
return route["$HEAD"] || route["$GET"] || route["$POST"] || route["$PUT"] || route["$PATCH"] || route["$DELETE"];
}

const router = createRouter({
Expand Down
2 changes: 1 addition & 1 deletion packages/start/src/server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function createBaseHandler(
const match = matchAPIRoute(new URL(event.request.url).pathname, event.request.method);
if (match) {
const mod = await match.handler.import();
const fn = mod[event.request.method];
const fn = event.request.method === "HEAD" ? mod["HEAD"] || mod["GET"] : mod[event.request.method];
(event as APIEvent).params = match.params || {};
// @ts-ignore
sharedConfig.context = { event };
Expand Down