diff --git a/src/dev/error.ts b/src/dev/error.ts
index e58b5a3d6c..e1a26682ca 100644
--- a/src/dev/error.ts
+++ b/src/dev/error.ts
@@ -1,14 +1,21 @@
-import { H3Event, setResponseStatus } from "h3";
+import {
+ H3Event,
+ setResponseHeader,
+ setResponseStatus,
+ getResponseStatus,
+ getResponseStatusText,
+ send,
+} from "h3";
import { NitroErrorHandler } from "../types";
function errorHandler(error: any, event: H3Event) {
- event.node.res.setHeader("Content-Type", "text/html; charset=UTF-8");
+ setResponseHeader(event, "Content-Type", "text/html; charset=UTF-8");
setResponseStatus(event, 503, "Server Unavailable");
let body;
let title;
if (error) {
- title = `${event.node.res.statusCode} ${event.node.res.statusMessage}`;
+ title = `${getResponseStatus(event)} ${getResponseStatusText(event)}`;
body = `${error.stack}
`;
} else {
title = "Reloading server...";
@@ -16,11 +23,9 @@ function errorHandler(error: any, event: H3Event) {
"";
}
- if (event.handled) {
- return;
- }
-
- event.node.res.end(`
+ send(
+ event,
+ `
@@ -43,7 +48,8 @@ function errorHandler(error: any, event: H3Event) {
-`);
+`
+ );
}
export default errorHandler as NitroErrorHandler;
diff --git a/src/runtime/error.ts b/src/runtime/error.ts
index 7bae15e9f3..ef88e18e82 100644
--- a/src/runtime/error.ts
+++ b/src/runtime/error.ts
@@ -1,5 +1,5 @@
// import ansiHTML from 'ansi-html'
-import { setResponseStatus } from "h3";
+import { setResponseHeader, setResponseStatus, send } from "h3";
import type { NitroErrorHandler } from "../types";
import { normalizeError, isJsonRequest } from "./utils";
@@ -44,14 +44,12 @@ export default function (error, event) {
setResponseStatus(event, statusCode, statusMessage);
- if (!event.handled) {
- if (isJsonRequest(event)) {
- event.node.res.setHeader("Content-Type", "application/json");
- event.node.res.end(JSON.stringify(errorObject));
- } else {
- event.node.res.setHeader("Content-Type", "text/html");
- event.node.res.end(renderHTMLError(errorObject));
- }
+ if (isJsonRequest(event)) {
+ setResponseHeader(event, "Content-Type", "application/json");
+ send(event, JSON.stringify(errorObject));
+ } else {
+ setResponseHeader(event, "Content-Type", "text/html");
+ send(event, renderHTMLError(errorObject));
}
};
diff --git a/src/runtime/renderer.ts b/src/runtime/renderer.ts
index 41cb9e5d55..71c9b6889d 100644
--- a/src/runtime/renderer.ts
+++ b/src/runtime/renderer.ts
@@ -1,4 +1,12 @@
-import { H3Event, eventHandler, setResponseStatus } from "h3";
+import {
+ H3Event,
+ eventHandler,
+ getResponseStatus,
+ send,
+ setResponseHeader,
+ setResponseHeaders,
+ setResponseStatus,
+} from "h3";
import { useNitroApp } from "./app";
export interface RenderResponse {
@@ -16,25 +24,22 @@ export function defineRenderHandler(handler: RenderHandler) {
return eventHandler(async (event) => {
// TODO: Use serve-placeholder
if (event.path.endsWith("/favicon.ico")) {
- if (!event.handled) {
- event.node.res.setHeader("Content-Type", "image/x-icon");
- event.node.res.end(
- "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
- );
- }
+ setResponseHeader(event, "Content-Type", "image/x-icon");
+ send(
+ event,
+ "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
+ );
return;
}
const response = await handler(event);
if (!response) {
- if (!event.handled) {
- event.node.res.statusCode =
- event.node.res.statusCode === 200 ? 500 : event.node.res.statusCode;
- event.node.res.end(
- "No response returned from render handler: " + event.path
- );
- }
- return;
+ const _currentStatus = getResponseStatus(event);
+ setResponseStatus(event, _currentStatus === 200 ? 500 : _currentStatus);
+ return send(
+ event,
+ "No response returned from render handler: " + event.path
+ );
}
// Allow hooking and modifying response
@@ -46,10 +51,10 @@ export function defineRenderHandler(handler: RenderHandler) {
// TODO: Caching support
// Send headers
- if (!event.node.res.headersSent && response.headers) {
- for (const header in response.headers) {
- event.node.res.setHeader(header, response.headers[header]);
- }
+ if (response.headers) {
+ setResponseHeaders(event, response.headers);
+ }
+ if (response.statusCode || response.statusMessage) {
setResponseStatus(event, response.statusCode, response.statusMessage);
}
diff --git a/src/runtime/static.ts b/src/runtime/static.ts
index e5e1375011..ee0e98ccce 100644
--- a/src/runtime/static.ts
+++ b/src/runtime/static.ts
@@ -1,4 +1,13 @@
-import { eventHandler, createError, getRequestHeader } from "h3";
+import {
+ eventHandler,
+ createError,
+ getRequestHeader,
+ getResponseHeader,
+ setResponseHeader,
+ setResponseStatus,
+ removeResponseHeader,
+ send,
+} from "h3";
import {
decodePath,
joinURL,
@@ -39,7 +48,7 @@ export default eventHandler((event) => {
"",
];
if (encodings.length > 1) {
- event.node.res.setHeader("Vary", "Accept-Encoding");
+ setResponseHeader(event, "Vary", "Accept-Encoding");
}
for (const encoding of encodings) {
@@ -55,7 +64,7 @@ export default eventHandler((event) => {
if (!asset) {
if (isPublicAssetURL(id)) {
- event.node.res.removeHeader("cache-control");
+ removeResponseHeader(event, "Cache-Control");
throw createError({
statusMessage: "Cannot find static asset " + id,
statusCode: 404,
@@ -66,10 +75,8 @@ export default eventHandler((event) => {
const ifNotMatch = getRequestHeader(event, "if-none-match") === asset.etag;
if (ifNotMatch) {
- if (!event.handled) {
- event.node.res.statusCode = 304;
- event.node.res.end();
- }
+ setResponseStatus(event, 304, "Not Modified");
+ send(event, "");
return;
}
@@ -80,31 +87,29 @@ export default eventHandler((event) => {
asset.mtime &&
new Date(ifModifiedSinceH) >= mtimeDate
) {
- if (!event.handled) {
- event.node.res.statusCode = 304;
- event.node.res.end();
- }
+ setResponseStatus(event, 304, "Not Modified");
+ send(event, "");
return;
}
- if (asset.type && !event.node.res.getHeader("Content-Type")) {
- event.node.res.setHeader("Content-Type", asset.type);
+ if (asset.type && !getResponseHeader(event, "Content-Type")) {
+ setResponseHeader(event, "Content-Type", asset.type);
}
- if (asset.etag && !event.node.res.getHeader("ETag")) {
- event.node.res.setHeader("ETag", asset.etag);
+ if (asset.etag && !getResponseHeader(event, "ETag")) {
+ setResponseHeader(event, "ETag", asset.etag);
}
- if (asset.mtime && !event.node.res.getHeader("Last-Modified")) {
- event.node.res.setHeader("Last-Modified", mtimeDate.toUTCString());
+ if (asset.mtime && !getResponseHeader(event, "Last-Modified")) {
+ setResponseHeader(event, "Last-Modified", mtimeDate.toUTCString());
}
- if (asset.encoding && !event.node.res.getHeader("Content-Encoding")) {
- event.node.res.setHeader("Content-Encoding", asset.encoding);
+ if (asset.encoding && !getResponseHeader(event, "Content-Encoding")) {
+ setResponseHeader(event, "Content-Encoding", asset.encoding);
}
- if (asset.size > 0 && !event.node.res.getHeader("Content-Length")) {
- event.node.res.setHeader("Content-Length", asset.size);
+ if (asset.size > 0 && !getResponseHeader(event, "Content-Length")) {
+ setResponseHeader(event, "Content-Length", asset.size);
}
return readAsset(id);