@nuxt/docs-nightly 5.0.0-29554472.d5d8ce2f → 5.0.0-29554612.0e08003b
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/10.data-fetching.md +1 -2
- package/1.getting-started/13.server.md +3 -1
- package/1.getting-started/18.upgrade.md +145 -0
- package/2.directory-structure/1.app/3.error.md +0 -4
- package/2.directory-structure/1.modules.md +2 -0
- package/2.directory-structure/1.server.md +4 -2
- package/3.guide/4.modules/4.recipes-advanced.md +1 -1
- package/3.guide/6.going-further/10.runtime-config.md +1 -1
- package/3.guide/6.going-further/2.hooks.md +1 -1
- package/4.api/2.composables/use-runtime-config.md +3 -3
- package/4.api/5.kit/11.nitro.md +14 -10
- package/4.api/5.kit/7.pages.md +1 -1
- package/4.api/6.nuxt-config.md +3 -3
- package/7.migration/8.runtime-config.md +1 -1
- package/package.json +1 -1
|
@@ -607,7 +607,6 @@ However, when calling `useFetch` with a relative URL on the server, Nuxt will us
|
|
|
607
607
|
If you want to pass on/proxy cookies in the other direction, from an internal request back to the client, you will need to handle this yourself.
|
|
608
608
|
|
|
609
609
|
```ts [app/composables/fetch.ts]
|
|
610
|
-
import { appendResponseHeader } from 'h3'
|
|
611
610
|
import type { H3Event } from 'h3'
|
|
612
611
|
|
|
613
612
|
export const fetchWithCookie = async (event: H3Event, url: string) => {
|
|
@@ -617,7 +616,7 @@ export const fetchWithCookie = async (event: H3Event, url: string) => {
|
|
|
617
616
|
const cookies = res.headers.getSetCookie()
|
|
618
617
|
/* Attach each cookie to our incoming Request */
|
|
619
618
|
for (const cookie of cookies) {
|
|
620
|
-
|
|
619
|
+
event.res.headers.append('set-cookie', cookie)
|
|
621
620
|
}
|
|
622
621
|
/* Return the data of the response */
|
|
623
622
|
return res._data
|
|
@@ -29,6 +29,8 @@ You can easily manage the server-only part of your Nuxt app, from API endpoints
|
|
|
29
29
|
Both endpoints and middleware can be defined like this:
|
|
30
30
|
|
|
31
31
|
```ts twoslash [server/api/test.ts]
|
|
32
|
+
import { defineEventHandler } from 'nitro/h3'
|
|
33
|
+
|
|
32
34
|
export default defineEventHandler(async (event) => {
|
|
33
35
|
// ... Do whatever you want here
|
|
34
36
|
})
|
|
@@ -74,7 +76,7 @@ export default defineNuxtConfig({
|
|
|
74
76
|
'/api/*': { cache: { maxAge: 60 * 60 } },
|
|
75
77
|
// Redirection to avoid 404
|
|
76
78
|
'/old-page': {
|
|
77
|
-
redirect: { to: '/new-page',
|
|
79
|
+
redirect: { to: '/new-page', status: 302 },
|
|
78
80
|
},
|
|
79
81
|
// ...
|
|
80
82
|
},
|
|
@@ -203,6 +203,151 @@ Most of the migration is handled by Nuxt internally, but there are some user-fac
|
|
|
203
203
|
See the full Vite 8 migration guide for all breaking changes and migration steps.
|
|
204
204
|
::
|
|
205
205
|
|
|
206
|
+
### Migration to Nitro v3
|
|
207
|
+
|
|
208
|
+
🚦 **Impact Level**: Significant
|
|
209
|
+
|
|
210
|
+
#### What Changed
|
|
211
|
+
|
|
212
|
+
Nuxt 5 upgrades to [Nitro v3](https://nitro.build/blog/v3-beta), which is a major rewrite of the server engine. Nitro v3 is built on [srvx](https://srvx.h3.dev) and [h3 v2](https://h3.dev), adopting Web standard `Request`/`Response` APIs throughout. This brings performance improvements and a more consistent API, but includes several breaking changes to server-side code.
|
|
213
|
+
|
|
214
|
+
::important
|
|
215
|
+
We are still working on Nitro v3 integration so you should expect further changes, as well as additional work to make migration more straightforward.
|
|
216
|
+
::
|
|
217
|
+
|
|
218
|
+
::read-more{to="https://nitro.build/blog/v3-beta" target="_blank"}
|
|
219
|
+
Read the Nitro v3 beta announcement for a full overview.
|
|
220
|
+
::
|
|
221
|
+
|
|
222
|
+
::read-more{to="https://nitro.build/docs/migration" target="_blank"}
|
|
223
|
+
See the full Nitro v3 migration guide for all breaking changes.
|
|
224
|
+
::
|
|
225
|
+
|
|
226
|
+
The sections below highlight changes that are most relevant to Nuxt application developers and module authors.
|
|
227
|
+
|
|
228
|
+
#### Package and Import Path Changes
|
|
229
|
+
|
|
230
|
+
The `nitropack` package has been renamed to `nitro`. All import paths have changed:
|
|
231
|
+
|
|
232
|
+
| Before | After |
|
|
233
|
+
|---|---|
|
|
234
|
+
| `nitropack` | `nitro` |
|
|
235
|
+
| `nitropack/types` | `nitro/types` |
|
|
236
|
+
| `nitropack/runtime` | `nitro/runtime` |
|
|
237
|
+
| `h3` (for server utilities) | `nitro/h3` |
|
|
238
|
+
|
|
239
|
+
Auto-imports within server routes (`defineEventHandler`, `getQuery`, `readBody`, `useRuntimeConfig`, etc.) continue to work without changes.
|
|
240
|
+
|
|
241
|
+
If you have explicit imports in server code, update them:
|
|
242
|
+
|
|
243
|
+
```diff
|
|
244
|
+
- import { defineEventHandler, getQuery } from 'h3'
|
|
245
|
+
+ import { defineEventHandler, getQuery } from 'nitro/h3'
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
**For module authors**, type augmentations must target the new module path:
|
|
249
|
+
|
|
250
|
+
```diff
|
|
251
|
+
- declare module 'nitropack/types' {
|
|
252
|
+
+ declare module 'nitro/types' {
|
|
253
|
+
interface NitroRouteRules {
|
|
254
|
+
myModule?: { /* ... */ }
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
#### Error Handling: `status`/`statusText` replace `statusCode`/`statusMessage`
|
|
260
|
+
|
|
261
|
+
h3 v2 renames the error properties to align with Web standards:
|
|
262
|
+
|
|
263
|
+
```diff
|
|
264
|
+
createError({
|
|
265
|
+
- statusCode: 404,
|
|
266
|
+
- statusMessage: 'Not Found',
|
|
267
|
+
+ status: 404,
|
|
268
|
+
+ statusText: 'Not Found',
|
|
269
|
+
})
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
In server routes, the error class is now `HTTPError` (replacing `createError` from `h3`):
|
|
273
|
+
|
|
274
|
+
```diff
|
|
275
|
+
- import { createError } from 'h3'
|
|
276
|
+
+ import { HTTPError } from 'nitro/h3'
|
|
277
|
+
|
|
278
|
+
export default defineEventHandler(() => {
|
|
279
|
+
- throw createError({ statusCode: 400, statusMessage: 'Bad request' })
|
|
280
|
+
+ throw new HTTPError({ status: 400, statusText: 'Bad request' })
|
|
281
|
+
})
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
::note
|
|
285
|
+
In the Vue part of your app (the `app/` directory), Nuxt's `createError` composable continues to work and is the recommended way to throw errors.
|
|
286
|
+
::
|
|
287
|
+
|
|
288
|
+
#### Server Event API Changes (h3 v2)
|
|
289
|
+
|
|
290
|
+
The `H3Event` object now uses Web standard APIs:
|
|
291
|
+
|
|
292
|
+
**Request properties:**
|
|
293
|
+
```diff
|
|
294
|
+
- event.path // string
|
|
295
|
+
+ event.url.pathname // URL object - use .pathname, .search, .hash
|
|
296
|
+
|
|
297
|
+
- event.method // string
|
|
298
|
+
+ event.req.method // via Web Request object
|
|
299
|
+
|
|
300
|
+
- event.node.req.headers // Node.js IncomingHttpHeaders
|
|
301
|
+
+ event.req.headers // Web Headers API (.get(), .set(), .has())
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
**Response properties:**
|
|
305
|
+
```diff
|
|
306
|
+
- event.node.res.statusCode = 200
|
|
307
|
+
+ event.res.status = 200
|
|
308
|
+
|
|
309
|
+
- event.node.res.statusMessage = 'OK'
|
|
310
|
+
+ event.res.statusText = 'OK'
|
|
311
|
+
|
|
312
|
+
- setResponseHeader(event, 'x-custom', 'value')
|
|
313
|
+
+ event.res.headers.set('x-custom', 'value')
|
|
314
|
+
|
|
315
|
+
- appendResponseHeader(event, 'set-cookie', cookie)
|
|
316
|
+
+ event.res.headers.append('set-cookie', cookie)
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
#### `useRuntimeConfig()` No Longer Accepts `event`
|
|
320
|
+
|
|
321
|
+
In Nitro v3, `useRuntimeConfig()` no longer requires (or accepts) an `event` argument in server routes:
|
|
322
|
+
|
|
323
|
+
```diff
|
|
324
|
+
export default defineEventHandler((event) => {
|
|
325
|
+
- const config = useRuntimeConfig(event)
|
|
326
|
+
+ const config = useRuntimeConfig()
|
|
327
|
+
})
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
#### Route Rules: `statusCode` Renamed to `status`
|
|
331
|
+
|
|
332
|
+
If you define redirect route rules, the property name has changed:
|
|
333
|
+
|
|
334
|
+
```diff
|
|
335
|
+
export default defineNuxtConfig({
|
|
336
|
+
routeRules: {
|
|
337
|
+
'/old-page': {
|
|
338
|
+
- redirect: { to: '/new-page', statusCode: 302 },
|
|
339
|
+
+ redirect: { to: '/new-page', status: 302 },
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
})
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
#### For Module Authors: Additional Changes
|
|
346
|
+
|
|
347
|
+
- **Nitro plugin imports**: Use `import { defineNitroPlugin } from 'nitro/runtime'` for explicit imports (auto-imports still work).
|
|
348
|
+
- **Runtime hooks**: `nitroApp.hooks.hook('beforeResponse', ...)` and `nitroApp.hooks.hook('afterResponse', ...)` have been replaced by `nitroApp.hooks.hook('response', ...)`.
|
|
349
|
+
- **`getRouteRules()` from `nitro/app`**: On the server, the Nitro helper changed from `getRouteRules(event)` to `getRouteRules(method, pathname)`, which returns `{ routeRules }`.
|
|
350
|
+
|
|
206
351
|
### Non-Async `callHook`
|
|
207
352
|
|
|
208
353
|
🚦 **Impact Level**: Minimal
|
|
@@ -19,9 +19,11 @@ Nuxt automatically scans files inside these directories to register API and serv
|
|
|
19
19
|
|
|
20
20
|
Each file should export a default function defined with `defineEventHandler()` or `eventHandler()` (alias).
|
|
21
21
|
|
|
22
|
-
The handler can directly return JSON data, a `Promise`, or
|
|
22
|
+
The handler can directly return JSON data, a `Promise`, or a `Response` object.
|
|
23
23
|
|
|
24
24
|
```ts twoslash [server/api/hello.ts]
|
|
25
|
+
import { defineEventHandler } from 'nitro/h3'
|
|
26
|
+
|
|
25
27
|
export default defineEventHandler((event) => {
|
|
26
28
|
return {
|
|
27
29
|
hello: 'world',
|
|
@@ -322,7 +324,7 @@ export default defineEventHandler((event) => {
|
|
|
322
324
|
::code-group
|
|
323
325
|
```ts [server/api/foo.ts]
|
|
324
326
|
export default defineEventHandler(async (event) => {
|
|
325
|
-
const config = useRuntimeConfig(
|
|
327
|
+
const config = useRuntimeConfig()
|
|
326
328
|
|
|
327
329
|
const repo = await $fetch('https://api.github.com/repos/nuxt/nuxt', {
|
|
328
330
|
headers: {
|
|
@@ -144,7 +144,7 @@ export default defineNuxtModule({
|
|
|
144
144
|
interface MyModuleNitroRules {
|
|
145
145
|
myModule?: { foo: 'bar' }
|
|
146
146
|
}
|
|
147
|
-
declare module '
|
|
147
|
+
declare module 'nitro/types' {
|
|
148
148
|
interface NitroRouteRules extends MyModuleNitroRules {}
|
|
149
149
|
interface NitroRouteConfig extends MyModuleNitroRules {}
|
|
150
150
|
}
|
|
@@ -136,7 +136,7 @@ You can access runtime config within the server routes as well using `useRuntime
|
|
|
136
136
|
|
|
137
137
|
```ts [server/api/test.ts]
|
|
138
138
|
export default defineEventHandler(async (event) => {
|
|
139
|
-
const { apiSecret } = useRuntimeConfig(
|
|
139
|
+
const { apiSecret } = useRuntimeConfig()
|
|
140
140
|
const result = await $fetch('https://my.api.com/test', {
|
|
141
141
|
headers: {
|
|
142
142
|
Authorization: `Bearer ${apiSecret}`,
|
|
@@ -18,7 +18,7 @@ const config = useRuntimeConfig()
|
|
|
18
18
|
|
|
19
19
|
```ts [server/api/foo.ts]
|
|
20
20
|
export default defineEventHandler((event) => {
|
|
21
|
-
const config = useRuntimeConfig(
|
|
21
|
+
const config = useRuntimeConfig()
|
|
22
22
|
})
|
|
23
23
|
```
|
|
24
24
|
|
|
@@ -56,7 +56,7 @@ To access runtime config, we can use `useRuntimeConfig()` composable:
|
|
|
56
56
|
|
|
57
57
|
```ts [server/api/test.ts]
|
|
58
58
|
export default defineEventHandler(async (event) => {
|
|
59
|
-
const config = useRuntimeConfig(
|
|
59
|
+
const config = useRuntimeConfig()
|
|
60
60
|
|
|
61
61
|
// Access public variables
|
|
62
62
|
const result = await $fetch(`/test`, {
|
|
@@ -132,7 +132,7 @@ And then access the new CDN url using `config.app.cdnURL`.
|
|
|
132
132
|
|
|
133
133
|
```ts [server/api/foo.ts]
|
|
134
134
|
export default defineEventHandler((event) => {
|
|
135
|
-
const config = useRuntimeConfig(
|
|
135
|
+
const config = useRuntimeConfig()
|
|
136
136
|
|
|
137
137
|
// Access cdnURL universally
|
|
138
138
|
const cdnURL = config.app.cdnURL
|
package/4.api/5.kit/11.nitro.md
CHANGED
|
@@ -73,6 +73,8 @@ export default defineNuxtModule({
|
|
|
73
73
|
```
|
|
74
74
|
|
|
75
75
|
```ts twoslash [runtime/robots.get.ts]
|
|
76
|
+
import { defineEventHandler } from 'nitro/h3'
|
|
77
|
+
|
|
76
78
|
export default defineEventHandler(() => {
|
|
77
79
|
return {
|
|
78
80
|
body: `User-agent: *\nDisallow: /`,
|
|
@@ -96,7 +98,7 @@ Adds a Nitro server handler to be used only in development mode. This handler wi
|
|
|
96
98
|
### Usage
|
|
97
99
|
|
|
98
100
|
```ts twoslash
|
|
99
|
-
import { defineEventHandler } from 'h3'
|
|
101
|
+
import { defineEventHandler } from 'nitro/h3'
|
|
100
102
|
import { addDevServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
101
103
|
|
|
102
104
|
export default defineNuxtModule({
|
|
@@ -117,7 +119,7 @@ export default defineNuxtModule({
|
|
|
117
119
|
|
|
118
120
|
```ts twoslash
|
|
119
121
|
// @errors: 2391
|
|
120
|
-
import type { NitroDevEventHandler } from '
|
|
122
|
+
import type { NitroDevEventHandler } from 'nitro/types'
|
|
121
123
|
// ---cut---
|
|
122
124
|
function addDevServerHandler (handler: NitroDevEventHandler): void
|
|
123
125
|
```
|
|
@@ -198,7 +200,7 @@ You can read more about Nitro plugins in the [Nitro documentation](https://nitro
|
|
|
198
200
|
::
|
|
199
201
|
|
|
200
202
|
::warning
|
|
201
|
-
It is necessary to explicitly import `defineNitroPlugin` from `
|
|
203
|
+
It is necessary to explicitly import `defineNitroPlugin` from `nitro/runtime` within your plugin file. The same requirement applies to utilities such as `useRuntimeConfig`.
|
|
202
204
|
::
|
|
203
205
|
|
|
204
206
|
### Usage
|
|
@@ -242,17 +244,15 @@ export default defineNuxtModule({
|
|
|
242
244
|
```
|
|
243
245
|
|
|
244
246
|
```ts [runtime/plugin.ts]
|
|
247
|
+
import { defineNitroPlugin } from 'nitro/runtime'
|
|
248
|
+
|
|
245
249
|
export default defineNitroPlugin((nitroApp) => {
|
|
246
250
|
nitroApp.hooks.hook('request', (event) => {
|
|
247
|
-
console.log('on request', event.
|
|
248
|
-
})
|
|
249
|
-
|
|
250
|
-
nitroApp.hooks.hook('beforeResponse', (event, { body }) => {
|
|
251
|
-
console.log('on response', event.path, { body })
|
|
251
|
+
console.log('on request', event.req.url)
|
|
252
252
|
})
|
|
253
253
|
|
|
254
|
-
nitroApp.hooks.hook('
|
|
255
|
-
console.log('on
|
|
254
|
+
nitroApp.hooks.hook('response', async (res) => {
|
|
255
|
+
console.log('on response', await res.text())
|
|
256
256
|
})
|
|
257
257
|
})
|
|
258
258
|
```
|
|
@@ -415,6 +415,8 @@ export function useApiSecret () {
|
|
|
415
415
|
You can then use the `useApiSecret` function in your server code:
|
|
416
416
|
|
|
417
417
|
```ts twoslash [runtime/server/api/hello.ts]
|
|
418
|
+
import { defineEventHandler } from 'nitro/h3'
|
|
419
|
+
|
|
418
420
|
const useApiSecret = (): string => ''
|
|
419
421
|
// ---cut---
|
|
420
422
|
export default defineEventHandler(() => {
|
|
@@ -493,6 +495,8 @@ export function hello () {
|
|
|
493
495
|
You can then use the `hello` function in your server code.
|
|
494
496
|
|
|
495
497
|
```ts twoslash [runtime/server/api/hello.ts]
|
|
498
|
+
import { defineEventHandler } from 'nitro/h3'
|
|
499
|
+
|
|
496
500
|
function hello () {
|
|
497
501
|
return 'Hello from server utils!'
|
|
498
502
|
}
|
package/4.api/5.kit/7.pages.md
CHANGED
package/4.api/6.nuxt-config.md
CHANGED
|
@@ -1494,9 +1494,9 @@ Modules to generate deep aliases for within `compilerOptions.paths`. This does n
|
|
|
1494
1494
|
- **Default**
|
|
1495
1495
|
```json
|
|
1496
1496
|
[
|
|
1497
|
-
"
|
|
1498
|
-
"
|
|
1499
|
-
"
|
|
1497
|
+
"nitro/types",
|
|
1498
|
+
"nitro/runtime",
|
|
1499
|
+
"nitro",
|
|
1500
1500
|
"defu",
|
|
1501
1501
|
"h3",
|
|
1502
1502
|
"consola",
|
|
@@ -42,7 +42,7 @@ console.log(config.public.apiBase)
|
|
|
42
42
|
|
|
43
43
|
```ts [server/api/hello.ts]
|
|
44
44
|
export default defineEventhandler((event) => {
|
|
45
|
-
const config = useRuntimeConfig(
|
|
45
|
+
const config = useRuntimeConfig()
|
|
46
46
|
// In server, you can now access config.apiSecret, in addition to config.public
|
|
47
47
|
console.log(config.apiSecret)
|
|
48
48
|
console.log(config.public.apiBase)
|