Skip to content

Commit

Permalink
Manully remove response body for HEAD requests
Browse files Browse the repository at this point in the history
It also manully set the Content-Length when reqeust method is HEAD
  • Loading branch information
LitoMore committed Sep 17, 2024
1 parent 4207704 commit 2e3410c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,21 @@ export const iconHandler: Handler = (request, _info, params) => {
const darkModeColor = params?.pathname.groups.darkModeColor;
const icon = getSimpleIcon(iconSlug);
if (icon) {
const isHeadRequestMethod = request.method === 'HEAD';
const iconSvg = getIconSvg(icon, color, darkModeColor, viewbox, size);
return new Response(iconSvg, {
const response = new Response(isHeadRequestMethod ? null : iconSvg, {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'image/svg+xml',
'Cache-Control': cacheForSevenDaysHeader,
'Content-Type': 'image/svg+xml',
},
});
if (isHeadRequestMethod) {
// Only manully set the Content-Length when the request method is HEAD.
// Because the Content-Length is automatically set by the Response constructor.
response.headers.set('Content-Length', iconSvg.length.toString());
}
return response;
}

return new Response(null, {
Expand Down

0 comments on commit 2e3410c

Please sign in to comment.