Skip to content

Commit

Permalink
fix(ssr): Ensure exceptions listeners once only and removed on respon…
Browse files Browse the repository at this point in the history
…se headers sent
  • Loading branch information
leomp12 committed May 18, 2024
1 parent e5313ed commit f23a6e1
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/ssr/src/lib/serve-storefront.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,15 +320,15 @@ export default async (req: Request, res: Response) => {
const handleException = (err: Error) => {
if (res.headersSent) {
error(err, { onExceptionHandler: 1 });
res.end();
if (!res.writableEnded) res.end();
return;
}
warn(err);
res.set('X-SSR-Error', err.message);
res.set('X-SSR-Error-Stack', err.stack);
fallback(err);
};
process.on('unhandledRejection', (reason) => {
const handleRejection = (reason: unknown) => {
if (reason instanceof Error) {
handleException(reason);
return;
Expand All @@ -338,8 +338,13 @@ export default async (req: Request, res: Response) => {
const err: any = new Error(message);
err.reason = reason;
handleException(err);
};
process.once('unhandledRejection', handleRejection);
process.once('uncaughtException', handleException);
waitingHeadersSent.finally(() => {
process.off('unhandledRejection', handleRejection);
process.off('uncaughtException', handleException);
});
process.on('uncaughtException', handleException);

/*
https://github.com/withastro/astro/blob/main/examples/ssr/server/server.mjs
Expand Down

0 comments on commit f23a6e1

Please sign in to comment.