Skip to content

Commit

Permalink
added try..catch for reroute directive handling
Browse files Browse the repository at this point in the history
  • Loading branch information
friedemannsommer authored Jan 30, 2024
1 parent f3d7729 commit 38d0011
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/astro/src/runtime/server/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ export async function renderEndpoint(
const response = await handler.call(mod, context);
// Endpoints explicitly returning 404 or 500 response status should
// NOT be subject to rerouting to 404.astro or 500.astro.
response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');
return response;
return setRerouteDirective(response);
}

// This is separated into a dedicated function,
// to allow the JavaScript runtime to optimize
// the `renderEndpoint` implementation.
// For more details: https://web.dev/articles/speed-v8#therefore_5
function setRerouteDirective(response: Response): Response {
try {
response.headers.set(REROUTE_DIRECTIVE_HEADER, 'no');

return response;
} catch (_) {
const extendableHeaders = new Headers(response.headers);

extendableHeaders.set(REROUTE_DIRECTIVE_HEADER, 'no');

return new Response(response.body, {
headers: extendableHeaders,
status: response.status,
statusText: response.statusText,
});
}
}

0 comments on commit 38d0011

Please sign in to comment.