Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(renderer): add render:before hook #2770

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
NitroOpenAPIConfig,
NitroRouteConfig,
RenderResponse,
RenderContext,
} from "nitropack/types";

// Core
Expand Down Expand Up @@ -79,9 +80,11 @@ export interface NitroRuntimeHooks {
beforeResponse: NonNullable<AppOptions["onBeforeResponse"]>;
afterResponse: NonNullable<AppOptions["onAfterResponse"]>;

"render:before": (context: RenderContext) => void;

"render:response": (
response: Partial<RenderResponse>,
context: { event: H3Event }
context: RenderContext
) => void;
}

Expand Down
56 changes: 32 additions & 24 deletions src/runtime/internal/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,33 @@ import {
setResponseHeaders,
setResponseStatus,
} from "h3";
import type { RenderHandler } from "nitropack/types";
import type { RenderHandler, RenderContext } from "nitropack/types";
import { useNitroApp } from "./app";
import { useRuntimeConfig } from "./config";

export function defineRenderHandler(handler: RenderHandler) {
export function defineRenderHandler(render: RenderHandler) {
const runtimeConfig = useRuntimeConfig();
return eventHandler(async (event) => {
// TODO: Use serve-placeholder
if (event.path === `${runtimeConfig.app.baseURL}favicon.ico`) {
setResponseHeader(event, "Content-Type", "image/x-icon");
return send(
event,
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
);
}
const nitroApp = useNitroApp();

// Create shared context for hooks
const ctx: RenderContext = { event, render, response: undefined };

// Call initial hook to prepare and optionally custom render
await nitroApp.hooks.callHook("render:before", ctx);

if (!ctx.response /* not handled by hook */) {
// TODO: Use serve-placeholder
if (event.path === `${runtimeConfig.app.baseURL}favicon.ico`) {
setResponseHeader(event, "Content-Type", "image/x-icon");
return send(
event,
"data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
);
}

ctx.response = await ctx.render(event);

const response = await handler(event);
if (!response) {
const _currentStatus = getResponseStatus(event);
setResponseStatus(event, _currentStatus === 200 ? 500 : _currentStatus);
return send(
Expand All @@ -33,23 +42,22 @@ export function defineRenderHandler(handler: RenderHandler) {
);
}

// Allow hooking and modifying response
const nitroApp = useNitroApp();
await nitroApp.hooks.callHook("render:response", response, { event });

// TODO: Warn if response is already handled

// TODO: Caching support
// Allow modifying response
await nitroApp.hooks.callHook("render:response", ctx.response, ctx);

// Send headers
if (response.headers) {
setResponseHeaders(event, response.headers);
if (ctx.response.headers) {
setResponseHeaders(event, ctx.response.headers);
}
if (response.statusCode || response.statusMessage) {
setResponseStatus(event, response.statusCode, response.statusMessage);
if (ctx.response.statusCode || ctx.response.statusMessage) {
setResponseStatus(
event,
ctx.response.statusCode,
ctx.response.statusMessage
);
}

// Send response body
return response.body;
return ctx.response.body;
});
}
6 changes: 6 additions & 0 deletions src/types/runtime/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export type RenderHandler = (
event: H3Event
) => Partial<RenderResponse> | Promise<Partial<RenderResponse>>;

export interface RenderContext {
event: H3Event;
render: RenderHandler;
response?: Partial<RenderResponse>;
}

export interface CapturedErrorContext {
event?: H3Event;
[key: string]: unknown;
Expand Down