From 76f463298ee0691437ac7b3073af8bd9fa410d96 Mon Sep 17 00:00:00 2001 From: Julien Huang <63512348+huang-julien@users.noreply.github.com> Date: Mon, 26 Sep 2022 11:18:50 +0200 Subject: [PATCH] docs: document nitro hooks (#7782) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Roe Co-authored-by: Damian Głowala <48835293+DamianGlowala@users.noreply.github.com> --- .../2.guide/4.going-further/2.hooks.md | 23 +++++++++++++++++++ docs/content/3.api/4.advanced/1.hooks.md | 7 ++++++ 2 files changed, 30 insertions(+) diff --git a/docs/content/2.guide/4.going-further/2.hooks.md b/docs/content/2.guide/4.going-further/2.hooks.md index 05ce029a12c..636db5e77eb 100644 --- a/docs/content/2.guide/4.going-further/2.hooks.md +++ b/docs/content/2.guide/4.going-further/2.hooks.md @@ -45,3 +45,26 @@ export default defineNuxtPlugin((nuxtApp) => { ::alert{icon=👉} Learn more about [available lifecycle hooks](/api/advanced/hooks) :: + +## Nitro App Hooks (Runtime) + +These hooks are available for [Nitro plugins](https://nitro.unjs.io/guide/advanced/plugins) to hook into Nitro's runtime behavior. + +### Usage within a Nitro Plugin + +```js [~/server/plugins/test.ts] +export default defineNitroPlugin((nitroApp) => { + nitroApp.hooks.hook('render:html', (html, { event }) => { + console.log('render:html', html) + html.bodyAppend.push('
Appended by custom plugin') + }) + + nitroApp.hooks.hook('render:response', (response, { event }) => { + console.log('render:response', response) + }) +}) +``` + +::alert{icon=👉} +Learn more about available [Nitro lifecycle hooks](/api/advanced/hooks#nitro-hooks-runtime-server-side). +:: diff --git a/docs/content/3.api/4.advanced/1.hooks.md b/docs/content/3.api/4.advanced/1.hooks.md index 5a086becdaf..a55c987818b 100644 --- a/docs/content/3.api/4.advanced/1.hooks.md +++ b/docs/content/3.api/4.advanced/1.hooks.md @@ -28,3 +28,10 @@ Hook | Arguments | Environment | Description Check the [schema source code](https://github.com/nuxt/framework/blob/main/packages/schema/src/types/hooks.ts#L55) for all available hooks. :NeedContribution + +# Nitro App Hooks (runtime, server-side) + +Hook | Arguments | Description | Types +-----------------------|-----------------------|--------------------------------------|------------------ +`render:response` | `response, { event }` | Called before sending the response. | [response](https://github.com/nuxt/framework/blob/71ef8bd3ff207fd51c2ca18d5a8c7140476780c7/packages/nuxt/src/core/runtime/nitro/renderer.ts#L24), [event](https://github.com/unjs/h3/blob/f6ceb5581043dc4d8b6eab91e9be4531e0c30f8e/src/types.ts#L38) +`render:html` | `html, { event }` | Called before constructing the HTML. | [html](https://github.com/nuxt/framework/blob/71ef8bd3ff207fd51c2ca18d5a8c7140476780c7/packages/nuxt/src/core/runtime/nitro/renderer.ts#L15), [event](https://github.com/unjs/h3/blob/f6ceb5581043dc4d8b6eab91e9be4531e0c30f8e/src/types.ts#L38)