@nuxt/docs-nightly 4.6.0-29837544.9d4d1eda → 4.6.0-29838870.2da76d24

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.
@@ -60,6 +60,9 @@ Defaults throughout your Nuxt configuration then change to opt in to Nuxt 5 beha
60
60
  - **`clearNuxtState` resets to defaults**: `clearNuxtState` resets state to the initial value of [`useState`](/docs/api/composables/use-state) instead of setting it to `undefined`
61
61
  - **Non-async `callHook`**: `callHook` may return `void` instead of always returning a `Promise`
62
62
  - **Early return from `navigateTo`**: [`experimental.navigateToEarlyReturn`](/docs/guide/going-further/experimental-features#navigatetoearlyreturn) stops a middleware continuing after it redirects
63
+ - **Server-only head composables**: `useServerHead`, `useServerHeadSafe` and `useServerSeoMeta` are no longer auto-imported, `unhead.legacy` is ignored, Unhead's legacy plugins are no longer registered and Capo sorting is always on
64
+ - **PostCSS defaults off**: `autoprefixer` and `cssnano` are no longer configured unless you enable them in `postcss.plugins`
65
+ - **Keyed composables require `source`**: a [`optimization.keyedComposables`](/docs/api/nuxt-config#keyedcomposables) entry without a string `source` is no longer matched through its auto-import
63
66
  - **Comment node placeholders**: client-only components use [comment nodes instead of `<div>`](/docs/guide/going-further/experimental-features#clientnodeplaceholder) as SSR placeholders
64
67
  - **Typed pages**: [`experimental.typedPages`](/docs/guide/going-further/experimental-features#typedpages) is enabled for type-checked routing
65
68
  - **Typed `$fetch`**: [`experimental.routeTypedFetch`](/docs/guide/going-further/experimental-features#routetypedfetch) types `$fetch` and `useFetch` from the generated route types
@@ -127,17 +127,20 @@ If we can't tell, code will be treated as `nitro2`.
127
127
 
128
128
  ## Nitro-specific Code
129
129
 
130
- `nuxt/server` covers request and response work: handlers, errors, query and body reading, headers, cookies, redirects, route rules and runtime config. But Nitro covers a lot more, so if you need any of these areas, you might need to stay on Nitro's own imports inside a `nitro2` or `nitro3` file.
130
+ `nuxt/server` covers request and response work: handlers, errors, router params, query and body reading and validation, headers, CORS, cookies, sessions, redirects, route rules, runtime config and app config. But Nitro covers a lot more, so if you need any of these areas, you might need to stay on Nitro's own imports inside a `nitro2` or `nitro3` file.
131
131
 
132
132
  Use the specifiers of the version the file is for. A file that imports `nitro/*` is read as Nitro 3 code, so a `nitro2` file must import `h3` and `nitropack/*` instead:
133
133
 
134
134
  | Area | In a `nitro2` file | In a `nitro3` file |
135
135
  | --- | --- | --- |
136
136
  | Storage: `useStorage()` and its driver configuration | `nitropack/runtime` | `nitro/storage` |
137
- | Sessions: `useSession`, `getSession`, `updateSession`, `clearSession` | `h3` | `nitro/h3` |
138
137
  | Caching: `defineCachedEventHandler()`, `defineCachedFunction()` | `nitropack/runtime` | `nitro/cache` |
138
+ | Server plugins | `defineNitroPlugin()` from `nitropack/runtime` | `definePlugin()` from `nitro` |
139
+ | Runtime hooks, including `render:html` | `useNitroApp().hooks` from `nitropack/runtime` | `useNitroHooks()` from `nitro/app` |
140
+ | App-relative fetch | `useNitroApp().localFetch` from `nitropack/runtime` | `serverFetch()` from `nitro` |
141
+ | Lazy handlers: `lazyEventHandler()` | `h3` | `nitro/h3` |
139
142
 
140
- Nitro's runtime hooks (`useNitroApp().hooks`, including `render:html`) and tasks are also not supported by `nuxt/server`.
143
+ Tasks are also not supported by `nuxt/server`.
141
144
 
142
145
  ::read-more{to="/docs/guide/going-further/server-imports#reaching-past-the-surface"}
143
146
  See which h3 helpers work with the portable event, and which need Nitro-specific utilities.
@@ -36,21 +36,24 @@ This code can run under `@nuxt/nitro-server` and under `@nuxt/vite-server`, and
36
36
  | `createError`, `isNuxtError` | Construct and recognise an HTTP error. |
37
37
  | `getRequestURL` | The URL of the request. |
38
38
  | `getRequestHeader`, `getRequestHeaders` | Read request headers. |
39
- | `getQuery` | Read the query string. |
40
- | `readBody` | Read and parse the request body. |
39
+ | `getRouterParam`, `getRouterParams` | Read the dynamic segments matched for the request. |
40
+ | `getRequestIP` | The client IP address, with [forwarded headers trusted on request](#client-ip). |
41
+ | `getQuery`, `getValidatedQuery` | Read the query string, optionally [validated](#validation). |
42
+ | `readBody`, `readValidatedBody` | Read and parse the request body, optionally [validated](#validation). |
41
43
  | `getCookie`, `setCookie`, `deleteCookie` | Read and write cookies. |
42
44
  | `setResponseStatus` | Set the response status and reason phrase. |
43
- | `setResponseHeader`, `setResponseHeaders` | Set response headers. |
45
+ | `handleCors` | Apply [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CORS) headers and answer preflight requests. |
44
46
  | `sendRedirect` | Redirect the request. |
45
47
  | `useSession`, `getSession`, `updateSession`, `clearSession` | Read and write a [sealed session cookie](#sessions). |
46
48
  | `deriveSecret` | A purpose-specific secret [derived from `appSecret`](#deriving-secrets). |
47
49
  | `getRouteRules` | The [route rules](/docs/guide/concepts/rendering#route-rules) matched for the request. |
48
50
  | `useRuntimeConfig` | The server's [runtime configuration](/docs/guide/going-further/runtime-config). |
51
+ | `useAppConfig` | The [app config](/docs/directory-structure/app/app-config), as a copy per request when passed the event. |
49
52
  | `toNuxtRequestEvent` | The event in the shape the server runtime provides. |
50
53
 
51
- Types are exported alongside the utilities: `RequestEvent`, `RequestEventContext`, `NuxtRequestEvent`, `EventHandler`, `AppRouteRules`, `ServerRoutes`, `NuxtError`, `NuxtErrorDetails`, `NuxtErrorJSON`, `NuxtErrorLike`, `Session`, `SessionConfig`, `SessionData`, `SessionEvent`, `SessionManager`, `SessionPassword` and `SessionUpdate`.
54
+ Types are exported alongside the utilities: `RequestEvent`, `RequestEventContext`, `NuxtRequestEvent`, `EventHandler`, `AppRouteRules`, `ServerRoutes`, `CorsOptions`, `ValidateResult`, `NuxtError`, `NuxtErrorDetails`, `NuxtErrorJSON`, `NuxtErrorLike`, `Session`, `SessionConfig`, `SessionData`, `SessionEvent`, `SessionManager`, `SessionPassword` and `SessionUpdate`.
52
55
 
53
- These names are [auto-imported](/docs/directory-structure/server) in server code, so the import statement above is optional. Writing it keeps the file working in a project with server auto-imports off, and is what a module should do.
56
+ These names are not auto-imported from `nuxt/server`. In server code, the [auto-imported](/docs/directory-structure/server) names of the same spelling are h3's own helpers, which take the h3 event and behave differently in places (`defineEventHandler`, `handleCors`, `getRouterParams` and `readValidatedBody` among them), so import from `nuxt/server` explicitly. That includes `defineEventHandler`: the `nuxt/server` helpers read the event it hands the handler.
54
57
 
55
58
  ::tip
56
59
  `defineEventHandler` preserves your handler's return type, which is what types [`$fetch` and `useFetch`](/docs/getting-started/data-fetching) calls to the route. Annotate the value you return rather than the handler.
@@ -69,6 +72,8 @@ interface RequestEvent {
69
72
  }
70
73
  ```
71
74
 
75
+ To set, read, append or remove a response header, use `event.res.headers` directly: it is a standard `Headers` object.
76
+
72
77
  Nitro adds more on top, such as `event.node` and `event.waitUntil()`, but these four core properties work in all runtimes, and need no helpers:
73
78
 
74
79
  ```ts [server/api/echo.post.ts]
@@ -80,6 +85,28 @@ export default defineEventHandler(async (event) => {
80
85
 
81
86
  `NuxtRequestEvent` is the same request in the shape the configured builder provides, which is h3 v1's `H3Event` under `@nuxt/nitro-server`. You can get a type-safe version of the event for the server builder you're using by calling `toNuxtRequestEvent(event)`.
82
87
 
88
+ ## Validation
89
+
90
+ `readValidatedBody` and `getValidatedQuery` take any [Standard Schema](https://standardschema.dev) (Zod, Valibot, ArkType and others), or a function that returns the validated value, `true` to accept the input or `false` to reject it.
91
+
92
+ ```ts [server/api/users.post.ts]
93
+ import { defineEventHandler, readValidatedBody } from 'nuxt/server'
94
+ import { z } from 'zod'
95
+
96
+ export default defineEventHandler(async (event) => {
97
+ const user = await readValidatedBody(event, z.object({ name: z.string() }))
98
+ return { created: user.name }
99
+ })
100
+ ```
101
+
102
+ Invalid input is rejected with a `400` whose `data.issues` lists what failed. Pass `onError` to reject it with a different error.
103
+
104
+ ## Client IP
105
+
106
+ `getRequestIP(event)` returns the address the server runtime reports for the connection, or `undefined` if it reports none. No forwarded header is trusted by default, because a client can send any value in one.
107
+
108
+ Behind a proxy you control, pass `{ xForwardedFor: true }` to read the first entry of `X-Forwarded-For` instead. Only do this when the proxy overwrites the header: a proxy that appends to it leaves a client-sent value first.
109
+
83
110
  ## Sessions
84
111
 
85
112
  `useSession` reads the session for the request, sealing a new one into a cookie when there is none to read. The data is sealed with [iron](https://github.com/brc-dd/iron-webcrypto), so it lives in the cookie rather than in server storage, and the cookie defaults to `httpOnly`, `secure`, `sameSite: 'lax'` and `path: '/'`.
@@ -126,38 +153,37 @@ Most of those helpers take the event as it is:
126
153
 
127
154
  ```ts [server/api/upload.post.ts]
128
155
  import { defineEventHandler, toNuxtRequestEvent } from 'nuxt/server'
129
- import { readValidatedBody } from 'h3'
156
+ import { readMultipartFormData } from 'h3'
130
157
 
131
158
  export default defineEventHandler(async (event) => {
132
- const body = await readValidatedBody(toNuxtRequestEvent(event), schema)
133
- return { received: body }
159
+ const parts = await readMultipartFormData(toNuxtRequestEvent(event))
160
+ return { received: parts?.length ?? 0 }
134
161
  })
135
162
  ```
136
163
 
137
- Under `@nuxt/nitro-server` the event a handler receives is a view of the h3 v1 event, so `toNuxtRequestEvent(event)` is what h3's own helpers take: `readValidatedBody`, `getValidatedQuery`, `getRouterParam`, `getRouterParams`, `getRequestIP`, `assertMethod`, the session helpers, the CORS helpers (such as `handleCors`), `proxy`, `proxyRequest`, `fetchWithEvent` and `writeEarlyHints`, as well as reading `event.node` or `event.waitUntil()`.
164
+ Under `@nuxt/nitro-server` the event a handler receives is a view of the h3 v1 event, so `toNuxtRequestEvent(event)` is what h3's own helpers take: `readRawBody`, `readFormData`, `readMultipartFormData`, `assertMethod`, `proxy`, `proxyRequest`, `fetchWithEvent` and `writeEarlyHints`, as well as reading `event.node` or `event.waitUntil()`.
138
165
 
139
166
  Nitro's own APIs mostly take no event at all: `defineCachedEventHandler()`, `useStorage()`, `useDatabase()` and tasks.
140
167
 
141
168
  ::tip
142
169
  If you find you are writing Nitro-specific code, you may simply prefer to import `defineEventHandler` from `h3` instead:
143
170
 
144
- ```ts [server/api/cors.ts]
145
- import { defineEventHandler, handleCors } from 'h3'
171
+ ```ts [server/api/proxy.ts]
172
+ import { defineEventHandler, proxyRequest } from 'h3'
146
173
 
147
- export default defineEventHandler((event) => {
148
- handleCors(event, { origin: '*' })
149
- return { ok: true }
150
- })
174
+ export default defineEventHandler(event => proxyRequest(event, 'https://example.com'))
151
175
  ```
152
176
  ::
153
177
 
154
178
  ## What Isn't Portable
155
179
 
156
- Three key areas you may need will mean you need to reach outside `nuxt/server`, because Nuxt can't provide them for every builder:
180
+ These areas mean reaching outside `nuxt/server`, because Nuxt can't provide them for every builder:
157
181
 
158
182
  - **Storage.** `useStorage()` comes from `nitropack/runtime`, and so does the driver configuration behind it.
159
- - **Sessions.** The sealed-session helpers come from `h3`. They accept the runtime's event, but importing them pins the file to Nitro.
160
- - **Caching.** `defineCachedEventHandler()` and `defineCachedFunction()` come from `nitropack/runtime`.
183
+ - **Caching.** `defineCachedEventHandler()` and `defineCachedFunction()` come from `nitropack/runtime`. For caching that runs anywhere, depend on a cache library directly.
184
+ - **Server plugins and hooks.** `defineNitroPlugin()` and the runtime hooks (`useNitroApp().hooks`, including `render:html`) come from `nitropack/runtime`.
185
+ - **App-relative fetch.** `useNitroApp().localFetch` comes from `nitropack/runtime`. It calls a route of the app without an origin, which needs the runtime's router.
186
+ - **Lazy handlers.** `lazyEventHandler()` comes from `h3`.
161
187
 
162
188
  ## Server Code Only
163
189
 
@@ -1605,6 +1605,17 @@ If set to true, this will type check in development. You can restrict this to bu
1605
1605
 
1606
1606
  **See**: [Nuxt TypeScript docs](/docs/guide/concepts/typescript)
1607
1607
 
1608
+ ## typesDir
1609
+
1610
+ Define the directory where generated types and `tsconfig.json` files will be placed.
1611
+
1612
+ This defaults to your `buildDir`, and is separate from it so that the configurations referenced by your project `tsconfig.json` stay in place even when Nuxt builds elsewhere (as it does when building a project that has already been run in development).
1613
+
1614
+ If a relative path is specified, it will be relative to your `rootDir`.
1615
+
1616
+ - **Type**: `string`
1617
+ - **Default:** `"/<rootDir>/.nuxt"`
1618
+
1608
1619
  ## unhead
1609
1620
 
1610
1621
  An object that allows us to configure the `unhead` nuxt module.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuxt/docs-nightly",
3
- "version": "4.6.0-29837544.9d4d1eda",
3
+ "version": "4.6.0-29838870.2da76d24",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/nuxt/nuxt.git",