Skip to content

Commit

Permalink
fix(start): enable HEAD requests to API HEAD/GET routes
Browse files Browse the repository at this point in the history
Fixes #1657
  • Loading branch information
paularmstrong committed Oct 26, 2024
1 parent e45e499 commit e08d3b2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
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

0 comments on commit e08d3b2

Please sign in to comment.