@nuxt/docs-nightly 5.0.0-2611091007-dc6155c → 5.0.0-2611092151-df60729
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/1.getting-started/04.views.md +4 -2
- package/3.guide/1.concepts/2.nuxt-lifecycle.md +1 -1
- package/3.guide/4.modules/9.server-compatibility.md +50 -2
- package/3.guide/6.going-further/2.hooks.md +38 -4
- package/3.guide/6.going-further/5.server-imports.md +1 -1
- package/4.api/5.kit/12.nitro.md +46 -1
- package/4.api/6.advanced/1.hooks.md +22 -10
- package/package.json +1 -1
|
@@ -158,10 +158,12 @@ export default definePlugin((nitroApp) => {
|
|
|
158
158
|
// This will be an object representation of the html template.
|
|
159
159
|
console.log(html)
|
|
160
160
|
html.head.push(`<meta name="description" content="My custom description" />`)
|
|
161
|
+
// Response headers set here are sent with the page.
|
|
162
|
+
event.res.headers.set('x-extended', 'true')
|
|
161
163
|
})
|
|
162
|
-
// You can also intercept the response here.
|
|
163
|
-
nitroApp.hooks.hook('render:response', (response, { event }) => { console.log(response) })
|
|
164
164
|
})
|
|
165
165
|
```
|
|
166
166
|
|
|
167
|
+
A Nitro plugin only runs when Nuxt runs on Nitro. A module can register the same hook from a [renderer plugin](/docs/guide/modules/server-compatibility#renderer-plugins), which runs under any server builder.
|
|
168
|
+
|
|
167
169
|
:read-more{to="/docs/guide/going-further/hooks"}
|
|
@@ -104,7 +104,7 @@ After rendering the Vue application to HTML, Nuxt calls the [`app:rendered`](/do
|
|
|
104
104
|
::
|
|
105
105
|
|
|
106
106
|
::callout{icon="i-lucide-lightbulb"}
|
|
107
|
-
Before finalizing and sending the HTML,
|
|
107
|
+
Before finalizing and sending the HTML, the renderer calls the [`render:html`](/docs/api/advanced/hooks#renderer-hooks-runtime-server-side) hook. This hook allows you to manipulate the generated HTML, such as injecting additional scripts or modifying meta tags.
|
|
108
108
|
::
|
|
109
109
|
|
|
110
110
|
::
|
|
@@ -172,9 +172,57 @@ If your module exposes utilities that take the event (through an alias, `addServ
|
|
|
172
172
|
|
|
173
173
|
`getNitroVersion` reports the Nitro major, not which server API the host runs: a host with a different `server.builder` runs none of the Nitro ones.
|
|
174
174
|
|
|
175
|
+
### Renderer Plugins
|
|
176
|
+
|
|
177
|
+
If your module registers render hooks, such as `render:html` to add a script to the `<head>` of every page, register a renderer plugin with `addRendererPlugin`. Every SSR renderer calls it once, before it renders, with the hooks it calls while rendering. It runs under any server builder and in whatever runtime the server renders in, including deploy targets such as Cloudflare Workers.
|
|
178
|
+
|
|
179
|
+
::code-group
|
|
180
|
+
|
|
181
|
+
```ts [module.ts]
|
|
182
|
+
import { addRendererPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
183
|
+
|
|
184
|
+
export default defineNuxtModule({
|
|
185
|
+
meta: { name: 'my-module' },
|
|
186
|
+
setup () {
|
|
187
|
+
const { resolve } = createResolver(import.meta.url)
|
|
188
|
+
|
|
189
|
+
addRendererPlugin(resolve('./runtime/server/renderer'))
|
|
190
|
+
},
|
|
191
|
+
})
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
```ts [runtime/server/renderer.ts]
|
|
195
|
+
import type { RendererPlugin } from 'nuxt/server'
|
|
196
|
+
|
|
197
|
+
export default (({ hooks }) => {
|
|
198
|
+
hooks.hook('render:html', (html) => {
|
|
199
|
+
html.head.push('<meta name="my-module" content="enabled">')
|
|
200
|
+
})
|
|
201
|
+
}) satisfies RendererPlugin
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
::
|
|
205
|
+
|
|
206
|
+
The plugin can register `render:route`, `render:html`, `render:html:chunk`, `render:html:close` and `render:island`. `render:route` and `render:html` are called for every page the renderer produces, including error pages and routes with an `ssr: false` route rule; `render:html:chunk` and `render:html:close` only for a streamed response, and `render:island` for a server component. Each is called before the same hook registered on the server runtime, such as by a Nitro plugin. The plugin is called synchronously, and a promise it returns is not awaited.
|
|
207
|
+
|
|
208
|
+
There is no `render:response` hook. To set a response header for a page, set it on `event.res.headers` from `render:html`, which is called before the response is created.
|
|
209
|
+
|
|
210
|
+
On a version of Nuxt without renderer plugins, `addRendererPlugin` registers the same file as a Nitro plugin, which needs `@nuxt/kit` with `addRendererPlugin` in your module's `dependencies`. Nitro calls it with its app, whose `hooks` are the ones the renderer calls, so the plugin works there too as long as it only reads `hooks` and imports nothing a server of that version cannot resolve. If it does, pass the Nitro plugin to register instead:
|
|
211
|
+
|
|
212
|
+
```ts [module.ts]
|
|
213
|
+
addRendererPlugin({
|
|
214
|
+
nuxt: resolve('./runtime/server/renderer'),
|
|
215
|
+
nitro2: resolve('./runtime/server/plugin.legacy'),
|
|
216
|
+
})
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
::note
|
|
220
|
+
A static SPA built with `@nuxt/vite-server` (`ssr: false`) has no server, so its document is rendered through the `render:html` hooks of renderer plugins as a prerendered route: once per build, for the base URL, with `import.meta.prerender` set. Anything the hooks add is the same for every visitor. In development, the document is rendered for each navigation. No other renderer hook is called for it.
|
|
221
|
+
::
|
|
222
|
+
|
|
175
223
|
### Server Plugins
|
|
176
224
|
|
|
177
|
-
Server plugins take variants too, although they are specific to Nitro so there is no generic `nuxt` variant to register.
|
|
225
|
+
Server plugins take variants too, although they are specific to Nitro so there is no generic `nuxt` variant to register. A plugin that only registers render hooks can be a [renderer plugin](#renderer-plugins) instead.
|
|
178
226
|
|
|
179
227
|
::tip
|
|
180
228
|
Because `definePlugin()` from `nitro` and `defineNitroPlugin()` from `nitropack/runtime` are identity functions, you may be able to replace them with a type annotation to write code that's portable for both `nitro2` and `nitro3`.
|
|
@@ -264,7 +312,7 @@ Builder-specific types, such as `H3Event` for `useRequestEvent()` or the full Ni
|
|
|
264
312
|
|
|
265
313
|
### Plugins, Storage and Caching
|
|
266
314
|
|
|
267
|
-
A module built on
|
|
315
|
+
A module built on Nitro plugins, storage or caching has no `nuxt` variant, because `nuxt/server` doesn't cover those areas. Register its files as `nitro2` now, and add a `nitro3` variant when you port them.
|
|
268
316
|
|
|
269
317
|
## Nitro-specific Code
|
|
270
318
|
|
|
@@ -53,17 +53,51 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
53
53
|
Explore all available App hooks.
|
|
54
54
|
::
|
|
55
55
|
|
|
56
|
+
## Renderer Hooks (Runtime)
|
|
57
|
+
|
|
58
|
+
The Nuxt renderer calls these hooks while it renders a page, such as `render:html` to change the HTML before it is sent. A module registers them from a renderer plugin, which runs under any server builder.
|
|
59
|
+
|
|
60
|
+
::code-group
|
|
61
|
+
|
|
62
|
+
```ts [module.ts]
|
|
63
|
+
import { addRendererPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
64
|
+
|
|
65
|
+
export default defineNuxtModule({
|
|
66
|
+
setup () {
|
|
67
|
+
const { resolve } = createResolver(import.meta.url)
|
|
68
|
+
addRendererPlugin(resolve('./runtime/server/renderer'))
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
```ts [runtime/server/renderer.ts]
|
|
74
|
+
import type { RendererPlugin } from 'nuxt/server'
|
|
75
|
+
|
|
76
|
+
export default (({ hooks }) => {
|
|
77
|
+
hooks.hook('render:html', (html) => {
|
|
78
|
+
html.bodyAppend.push('<hr>Appended by custom plugin')
|
|
79
|
+
})
|
|
80
|
+
}) satisfies RendererPlugin
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
::
|
|
84
|
+
|
|
85
|
+
When Nuxt runs on Nitro, a server plugin can also register them on `nitroApp.hooks`.
|
|
86
|
+
|
|
87
|
+
::read-more{to="/docs/api/advanced/hooks#renderer-hooks-runtime-server-side"}
|
|
88
|
+
Explore all available renderer hooks.
|
|
89
|
+
::
|
|
90
|
+
|
|
56
91
|
## Server Hooks (Runtime)
|
|
57
92
|
|
|
58
|
-
These hooks are available for [server plugins](/docs/directory-structure/server#server-plugins) to hook into Nitro's runtime behavior.
|
|
93
|
+
These hooks are available for [server plugins](/docs/directory-structure/server#server-plugins) to hook into Nitro's runtime behavior, so they are only called when Nuxt runs on Nitro.
|
|
59
94
|
|
|
60
95
|
```ts [~~/server/plugins/test.ts]
|
|
61
96
|
import { definePlugin } from 'nitro'
|
|
62
97
|
|
|
63
98
|
export default definePlugin((nitroApp) => {
|
|
64
|
-
nitroApp.hooks.hook('
|
|
65
|
-
console.log('
|
|
66
|
-
html.bodyAppend.push('<hr>Appended by custom plugin')
|
|
99
|
+
nitroApp.hooks.hook('request', (event) => {
|
|
100
|
+
console.log('request', event.url.pathname)
|
|
67
101
|
})
|
|
68
102
|
|
|
69
103
|
nitroApp.hooks.hook('response', (response, event) => {
|
|
@@ -196,7 +196,7 @@ These areas mean reaching outside `nuxt/server`, because Nuxt can't provide them
|
|
|
196
196
|
|
|
197
197
|
- **Storage.** `useKV()` comes from `nitro/kv`, and so does the driver configuration behind it.
|
|
198
198
|
- **Caching.** `defineCachedHandler()` and `defineCachedFunction()` come from `nitro/cache`. For caching that runs anywhere, depend on a cache library directly.
|
|
199
|
-
- **Server plugins.** `definePlugin()` comes from `nitro`. Inside a plugin, `useServerHooks()` from `nuxt/server` registers the renderer hooks (including `render:html`) and those declared on `NuxtServerHooks`; Nitro's own hooks, such as `request`, `response` and `error`, come from `useNitroHooks()` in `nitro/app`.
|
|
199
|
+
- **Server plugins.** `definePlugin()` comes from `nitro`. A module that only registers render hooks can register a [renderer plugin](/docs/guide/modules/server-compatibility#renderer-plugins) instead, which runs under any server builder. Inside a plugin, `useServerHooks()` from `nuxt/server` registers the renderer hooks (including `render:html`) and those declared on `NuxtServerHooks`; Nitro's own hooks, such as `request`, `response` and `error`, come from `useNitroHooks()` in `nitro/app`.
|
|
200
200
|
- **Lazy handlers.** `lazyEventHandler()` comes from `nitro/h3`.
|
|
201
201
|
- **Tasks.** `defineTask()` and `runTask()` come from `nitro/task`.
|
|
202
202
|
|
package/4.api/5.kit/12.nitro.md
CHANGED
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
Nitro is an open source TypeScript framework to build ultra-fast web servers. Nuxt uses Nitro as its server engine. You can use `useNitro` to access the Nitro instance (or `tryUseNitro` if your module should also work without Nitro), `addServerHandler` to add a server handler, `addDevServerHandler` to add a server handler to be used only in development mode, `addNitroPlugin` to add a plugin to extend Nitro's runtime behavior, and `addPrerenderRoutes` to add routes to be prerendered by Nitro.
|
|
11
|
+
Nitro is an open source TypeScript framework to build ultra-fast web servers. Nuxt uses Nitro as its server engine. You can use `useNitro` to access the Nitro instance (or `tryUseNitro` if your module should also work without Nitro), `addServerHandler` to add a server handler, `addDevServerHandler` to add a server handler to be used only in development mode, `addNitroPlugin` to add a plugin to extend Nitro's runtime behavior, `addRendererPlugin` to register render hooks under any server builder, and `addPrerenderRoutes` to add routes to be prerendered by Nitro.
|
|
12
12
|
|
|
13
13
|
## `addServerHandler`
|
|
14
14
|
|
|
@@ -304,6 +304,51 @@ export default definePlugin((nitroApp) => {
|
|
|
304
304
|
|
|
305
305
|
::
|
|
306
306
|
|
|
307
|
+
## `addRendererPlugin`
|
|
308
|
+
|
|
309
|
+
Add a plugin to the SSR renderer, which every renderer calls once, before it renders, with the hooks it calls while rendering. It runs under any server builder.
|
|
310
|
+
|
|
311
|
+
::read-more{to="/docs/guide/modules/server-compatibility#renderer-plugins"}
|
|
312
|
+
See how renderer plugins run on each server builder, and on versions of Nuxt without them.
|
|
313
|
+
::
|
|
314
|
+
|
|
315
|
+
### Usage
|
|
316
|
+
|
|
317
|
+
```ts twoslash
|
|
318
|
+
import { addRendererPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
319
|
+
|
|
320
|
+
export default defineNuxtModule({
|
|
321
|
+
setup () {
|
|
322
|
+
const { resolve } = createResolver(import.meta.url)
|
|
323
|
+
addRendererPlugin(resolve('./runtime/server/renderer.ts'))
|
|
324
|
+
},
|
|
325
|
+
})
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Type
|
|
329
|
+
|
|
330
|
+
```ts
|
|
331
|
+
function addRendererPlugin (plugin: string | { nuxt?: string, nitro2?: string, nitro3?: string }): void
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
### Parameters
|
|
335
|
+
|
|
336
|
+
| Property | Type | Required | Description |
|
|
337
|
+
|----------|----------|----------|-------------|
|
|
338
|
+
| `plugin` | `string \| { nuxt?: string, nitro2?: string, nitro3?: string }` | `true` | Path to the renderer plugin, or the renderer plugin as `nuxt` alongside the Nitro plugins to register instead on a version of Nuxt without renderer plugins. The renderer plugin must export a default function that accepts `{ hooks }`. |
|
|
339
|
+
|
|
340
|
+
### Example
|
|
341
|
+
|
|
342
|
+
```ts [runtime/server/renderer.ts]
|
|
343
|
+
import type { RendererPlugin } from 'nuxt/server'
|
|
344
|
+
|
|
345
|
+
export default (({ hooks }) => {
|
|
346
|
+
hooks.hook('render:html', (html) => {
|
|
347
|
+
html.head.push('<meta name="my-module" content="enabled">')
|
|
348
|
+
})
|
|
349
|
+
}) satisfies RendererPlugin
|
|
350
|
+
```
|
|
351
|
+
|
|
307
352
|
## `addPrerenderRoutes`
|
|
308
353
|
|
|
309
354
|
Add routes to be prerendered to Nitro.
|
|
@@ -94,16 +94,28 @@ Check the [schema source code](https://github.com/nuxt/nuxt/blob/main/packages/s
|
|
|
94
94
|
| `rsbuild:compile` | `options` | Called right before the first compilation of an environment. |
|
|
95
95
|
| `rsbuild:compiled` | `options` | Called after an environment has been compiled. |
|
|
96
96
|
|
|
97
|
+
## Renderer Hooks (Runtime, Server-Side)
|
|
98
|
+
|
|
99
|
+
The Nuxt renderer calls these hooks while it renders a page, under any server builder. A module registers them from a [renderer plugin](/docs/api/kit/nitro#addrendererplugin). When Nuxt runs on Nitro, a [server plugin](/docs/directory-structure/server#server-plugins) can also register them on `nitroApp.hooks`. Their types are declared on `NuxtServerRendererHooks`, exported from `nuxt/server`.
|
|
100
|
+
|
|
101
|
+
| Hook | Arguments | Description |
|
|
102
|
+
|---------------------|--------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
103
|
+
| `render:route` | `context, { event }` | Called once per request, before rendering begins. Set `context.prefersStream` to `false` to render the request without streaming. |
|
|
104
|
+
| `render:html` | `html, { event, streaming? }` | Called with the parts of the HTML document (`htmlAttrs`, `head`, `bodyAttrs`, `bodyPrepend`, `body` and `bodyAppend`) before they are joined. For a streamed response, it is called before the shell is sent, with `streaming: true`, and changes to `body` and `bodyAppend` are dropped. |
|
|
105
|
+
| `render:html:chunk` | `context, { event }` | Called for each chunk of a streamed response before it is sent. Replace `context.chunk`, a `Uint8Array`, to transform it. |
|
|
106
|
+
| `render:html:close` | `context, { event }` | Called when the body of a streamed response has been rendered, before the closing tags. Push to `context.bodyAppend` to add markup at the end of the body. |
|
|
107
|
+
| `render:island` | `islandResponse, { event, islandContext }` | Called before a server component response is sent. Server components are only rendered on Nitro. |
|
|
108
|
+
|
|
109
|
+
There is no `render:response` hook. Response headers set on `event.res.headers` from `render:html` are sent with the page.
|
|
110
|
+
|
|
97
111
|
## Nitro App Hooks (Runtime, Server-Side)
|
|
98
112
|
|
|
99
|
-
See [Nitro](https://nitro.build/guide/plugins#available-hooks) for all available hooks.
|
|
113
|
+
These hooks are called by Nitro, so they are only available when Nuxt runs on Nitro. See [Nitro](https://nitro.build/guide/plugins#available-hooks) for all available hooks.
|
|
100
114
|
|
|
101
|
-
| Hook
|
|
102
|
-
|
|
103
|
-
| `dev:ssr-logs`
|
|
104
|
-
| `
|
|
105
|
-
| `
|
|
106
|
-
| `
|
|
107
|
-
| `
|
|
108
|
-
| `request` | `event` | Called when a request is received. | [event](https://github.com/h3js/h3/blob/f6ceb5581043dc4d8b6eab91e9be4531e0c30f8e/src/types.ts#L38) |
|
|
109
|
-
| `response` | `response, event` | Called before sending the response. | [response](https://developer.mozilla.org/en-US/docs/Web/API/Response) |
|
|
115
|
+
| Hook | Arguments | Description | Types |
|
|
116
|
+
|----------------|---------------------|-------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
117
|
+
| `dev:ssr-logs` | `{ path, logs }` | Called at the end of a request cycle with an array of server-side logs. | - |
|
|
118
|
+
| `close` | - | Called when Nitro is closed. | - |
|
|
119
|
+
| `error` | `error, { event? }` | Called when an error occurs. | [error](https://github.com/nitrojs/nitro/blob/d20ffcbd16fc4003b774445e1a01e698c2bb078a/src/types/runtime/nitro.ts#L48), [event](https://github.com/h3js/h3/blob/f6ceb5581043dc4d8b6eab91e9be4531e0c30f8e/src/types.ts#L38) |
|
|
120
|
+
| `request` | `event` | Called when a request is received. | [event](https://github.com/h3js/h3/blob/f6ceb5581043dc4d8b6eab91e9be4531e0c30f8e/src/types.ts#L38) |
|
|
121
|
+
| `response` | `response, event` | Called before sending the response. | [response](https://developer.mozilla.org/en-US/docs/Web/API/Response) |
|