Skip to content

Commit af7f156

Browse files
committed
perf: reduce error handler bundle impact
1 parent ce388de commit af7f156

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

src/runtime/internal/error/prod.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import type { H3Event, HTTPError, HTTPEvent } from "h3";
2-
import { getRequestURL } from "h3";
3-
import { defineNitroErrorHandler } from "./utils";
42
import type { InternalHandlerResponse } from "./utils";
53
import { FastResponse } from "srvx";
4+
import type { NitroErrorHandler } from "nitro/types";
65

7-
export default defineNitroErrorHandler(
8-
function defaultNitroErrorHandler(error, event) {
9-
const res = defaultHandler(error, event);
10-
return new FastResponse(JSON.stringify(res.body, null, 2), res);
11-
}
12-
);
6+
const errorHandler: NitroErrorHandler = (error, event) => {
7+
const res = defaultHandler(error, event);
8+
return new FastResponse(
9+
typeof res.body === "string" ? res.body : JSON.stringify(res.body, null, 2),
10+
res
11+
);
12+
};
13+
14+
export default errorHandler;
1315

1416
export function defaultHandler(
1517
error: HTTPError,
@@ -18,8 +20,7 @@ export function defaultHandler(
1820
): InternalHandlerResponse {
1921
const isSensitive = error.unhandled;
2022
const status = error.status || 500;
21-
// prettier-ignore
22-
const url = getRequestURL(event, { xForwardedHost: true, xForwardedProto: true })
23+
const url = (event as H3Event).url || new URL(event.req.url);
2324

2425
if (status === 404) {
2526
const baseURL = import.meta.baseURL || "/";
@@ -47,13 +48,9 @@ export function defaultHandler(
4748
// Send response
4849
const headers: HeadersInit = {
4950
"content-type": "application/json",
50-
// Prevent browser from guessing the MIME types of resources.
5151
"x-content-type-options": "nosniff",
52-
// Prevent error page from being embedded in an iframe
5352
"x-frame-options": "DENY",
54-
// Prevent browsers from sending the Referer header
5553
"referrer-policy": "no-referrer",
56-
// Disable the execution of any js
5754
"content-security-policy": "script-src 'none'; frame-ancestors 'none';",
5855
};
5956

test/minimal/minimal.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ const fixtureDir = fileURLToPath(new URL("./", import.meta.url));
99
const tmpDir = fileURLToPath(new URL(".tmp", import.meta.url));
1010

1111
const sizeThresholds: Record<string, [number, number]> = {
12-
rollup: [27, 16],
13-
rolldown: [181, 181],
14-
vite: [28, 14],
12+
rollup: [24, 15],
13+
rolldown: [178, 178],
14+
vite: [27, 13],
1515
};
1616

1717
describe("minimal fixture", () => {

0 commit comments

Comments
 (0)