@nuxt/docs 4.0.0-alpha.1 → 4.0.0-alpha.3
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 +2 -2
- package/1.getting-started/13.server.md +1 -1
- package/1.getting-started/15.prerendering.md +1 -1
- package/1.getting-started/17.testing.md +1 -1
- package/1.getting-started/18.upgrade.md +132 -3
- package/2.guide/1.concepts/3.rendering.md +1 -1
- package/2.guide/1.concepts/4.server-engine.md +2 -2
- package/2.guide/1.concepts/8.typescript.md +32 -5
- package/2.guide/2.directory-structure/1.app/.navigation.yml +5 -0
- package/2.guide/2.directory-structure/{1.pages.md → 1.app/1.pages.md} +1 -1
- package/2.guide/2.directory-structure/1.server.md +2 -12
- package/2.guide/2.directory-structure/3.tsconfig.md +14 -3
- package/2.guide/3.going-further/1.experimental-features.md +2 -1
- package/2.guide/3.going-further/11.nightly-release-channel.md +2 -2
- package/2.guide/3.going-further/3.modules.md +0 -2
- package/2.guide/4.recipes/1.custom-routing.md +5 -5
- package/3.api/1.components/4.nuxt-link.md +4 -0
- package/3.api/2.composables/on-prehydrate.md +21 -12
- package/3.api/2.composables/use-async-data.md +1 -1
- package/3.api/2.composables/use-cookie.md +67 -125
- package/3.api/2.composables/use-error.md +30 -7
- package/3.api/2.composables/use-fetch.md +70 -73
- package/3.api/2.composables/use-nuxt-app.md +1 -1
- package/3.api/3.utils/define-nuxt-plugin.md +102 -0
- package/3.api/3.utils/define-page-meta.md +1 -1
- package/3.api/5.kit/11.nitro.md +47 -0
- package/3.api/5.kit/13.logging.md +1 -1
- package/3.api/5.kit/4.autoimports.md +8 -8
- package/3.api/5.kit/6.context.md +1 -1
- package/3.api/6.advanced/1.hooks.md +8 -8
- package/3.api/6.nuxt-config.md +26 -49
- package/5.community/6.roadmap.md +17 -12
- package/6.bridge/2.typescript.md +2 -0
- package/7.migration/2.configuration.md +13 -2
- package/7.migration/7.component-options.md +1 -1
- package/package.json +1 -1
- /package/2.guide/2.directory-structure/{1.assets.md → 1.app/1.assets.md} +0 -0
- /package/2.guide/2.directory-structure/{1.components.md → 1.app/1.components.md} +0 -0
- /package/2.guide/2.directory-structure/{1.composables.md → 1.app/1.composables.md} +0 -0
- /package/2.guide/2.directory-structure/{1.layouts.md → 1.app/1.layouts.md} +0 -0
- /package/2.guide/2.directory-structure/{1.middleware.md → 1.app/1.middleware.md} +0 -0
- /package/2.guide/2.directory-structure/{1.plugins.md → 1.app/1.plugins.md} +0 -0
- /package/2.guide/2.directory-structure/{1.utils.md → 1.app/1.utils.md} +0 -0
- /package/2.guide/2.directory-structure/{3.app-config.md → 1.app/3.app-config.md} +0 -0
- /package/2.guide/2.directory-structure/{3.app.md → 1.app/3.app.md} +0 -0
- /package/2.guide/2.directory-structure/{3.error.md → 1.app/3.error.md} +0 -0
|
@@ -41,7 +41,7 @@ async function handleFormSubmit() {
|
|
|
41
41
|
</script>
|
|
42
42
|
|
|
43
43
|
<template>
|
|
44
|
-
<div v-if="data ==
|
|
44
|
+
<div v-if="data == undefined">
|
|
45
45
|
No data
|
|
46
46
|
</div>
|
|
47
47
|
<div v-else>
|
|
@@ -230,7 +230,7 @@ Read more about `useAsyncData`.
|
|
|
230
230
|
|
|
231
231
|
- `data`: the result of the asynchronous function that is passed in.
|
|
232
232
|
- `refresh`/`execute`: a function that can be used to refresh the data returned by the `handler` function.
|
|
233
|
-
- `clear`: a function that can be used to set `data` to `undefined
|
|
233
|
+
- `clear`: a function that can be used to set `data` to `undefined` (or the value of `options.default()` if provided), set `error` to `undefined`, set `status` to `idle`, and mark any currently pending requests as cancelled.
|
|
234
234
|
- `error`: an error object if the data fetching failed.
|
|
235
235
|
- `status`: a string indicating the status of the data request (`"idle"`, `"pending"`, `"success"`, `"error"`).
|
|
236
236
|
|
|
@@ -18,7 +18,7 @@ Using Nitro gives Nuxt superpowers:
|
|
|
18
18
|
- Universal deployment on any provider (many zero-config)
|
|
19
19
|
- Hybrid rendering
|
|
20
20
|
|
|
21
|
-
Nitro is internally using [h3](https://github.com/
|
|
21
|
+
Nitro is internally using [h3](https://github.com/h3js/h3), a minimal H(TTP) framework built for high performance and portability.
|
|
22
22
|
|
|
23
23
|
:video-accordion{title="Watch a video from Alexander Lichter to understand the responsibilities of Nuxt and Nitro in your application" videoId="DkvgJa-X31k"}
|
|
24
24
|
|
|
@@ -444,7 +444,7 @@ If you prefer to use `@vue/test-utils` on its own for unit testing in Nuxt, and
|
|
|
444
444
|
|
|
445
445
|
2. Create a `vitest.config.ts` with the following content:
|
|
446
446
|
|
|
447
|
-
```ts
|
|
447
|
+
```ts
|
|
448
448
|
import { defineConfig } from 'vitest/config'
|
|
449
449
|
import vue from '@vitejs/plugin-vue'
|
|
450
450
|
|
|
@@ -40,7 +40,7 @@ The nightly release channel `latest` tag is currently tracking the Nuxt v4 branc
|
|
|
40
40
|
|
|
41
41
|
## Testing Nuxt 4
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
Nuxt 4 is **scheduled for release in Q2 2025**. It will include all the features currently available through `compatibilityVersion: 4`.
|
|
44
44
|
|
|
45
45
|
Until the release, it is possible to test many of Nuxt 4's breaking changes from Nuxt version 3.12+.
|
|
46
46
|
|
|
@@ -188,6 +188,10 @@ server/
|
|
|
188
188
|
nuxt.config.ts
|
|
189
189
|
```
|
|
190
190
|
|
|
191
|
+
::note
|
|
192
|
+
With this new structure, the `~` alias now points to the `app/` directory by default (your `srcDir`). This means `~/components` resolves to `app/components/`, `~/pages` to `app/pages/`, etc.
|
|
193
|
+
::
|
|
194
|
+
|
|
191
195
|
</details>
|
|
192
196
|
|
|
193
197
|
👉 For more details, see the [PR implementing this change](https://github.com/nuxt/nuxt/pull/27029).
|
|
@@ -218,7 +222,7 @@ You can also force a v3 folder structure with the following configuration:
|
|
|
218
222
|
export default defineNuxtConfig({
|
|
219
223
|
// This reverts the new srcDir default from `app` back to your root directory
|
|
220
224
|
srcDir: '.',
|
|
221
|
-
// This specifies the directory prefix for `
|
|
225
|
+
// This specifies the directory prefix for `router.options.ts` and `spa-loading-template.html`
|
|
222
226
|
dir: {
|
|
223
227
|
app: 'app'
|
|
224
228
|
}
|
|
@@ -430,7 +434,7 @@ export default defineNuxtConfig({
|
|
|
430
434
|
|
|
431
435
|
#### What Changed
|
|
432
436
|
|
|
433
|
-
When rendering a client-only page (with `ssr: false`), we optionally render a loading screen (from
|
|
437
|
+
When rendering a client-only page (with `ssr: false`), we optionally render a loading screen (from `~/app/spa-loading-template.html` - note that this has also changed to `~/spa-loading-template.html` in Nuxt 4), within the Nuxt app root:
|
|
434
438
|
|
|
435
439
|
```html
|
|
436
440
|
<div id="__nuxt">
|
|
@@ -976,6 +980,93 @@ const importName = genSafeVariableName
|
|
|
976
980
|
You can automate this step by running `npx codemod@latest nuxt/4/template-compilation-changes`
|
|
977
981
|
::
|
|
978
982
|
|
|
983
|
+
### TypeScript Configuration Changes
|
|
984
|
+
|
|
985
|
+
🚦 **Impact Level**: Minimal
|
|
986
|
+
|
|
987
|
+
#### What Changed
|
|
988
|
+
|
|
989
|
+
Nuxt now generates separate TypeScript configurations for different contexts to provide better type-checking experiences:
|
|
990
|
+
|
|
991
|
+
1. **New TypeScript configuration files**: Nuxt now generates additional TypeScript configurations:
|
|
992
|
+
* `.nuxt/tsconfig.app.json` - For your app code (Vue components, composables, etc.)
|
|
993
|
+
* `.nuxt/tsconfig.server.json` - For your server-side code (Nitro/server directory)
|
|
994
|
+
* `.nuxt/tsconfig.node.json` - For your build-time code (modules, `nuxt.config.ts`, etc.)
|
|
995
|
+
* `.nuxt/tsconfig.json` - Legacy configuration for backward compatibility
|
|
996
|
+
|
|
997
|
+
2. **Backward compatibility**: Existing projects that extend `.nuxt/tsconfig.json` will continue to work as before.
|
|
998
|
+
|
|
999
|
+
3. **Opt-in project references**: New projects or those wanting better type checking can adopt TypeScript's project references feature.
|
|
1000
|
+
|
|
1001
|
+
4. **Context-specific type checking**: Each context now has appropriate compiler options and includes/excludes for its specific environment.
|
|
1002
|
+
|
|
1003
|
+
5. **New `typescript.nodeTsConfig` option**: You can now customize the TypeScript configuration for Node.js build-time code.
|
|
1004
|
+
|
|
1005
|
+
#### Reasons for Change
|
|
1006
|
+
|
|
1007
|
+
This change provides several benefits:
|
|
1008
|
+
|
|
1009
|
+
1. **Better type safety**: Each context (app, server, build-time) gets appropriate type checking with context-specific globals and APIs.
|
|
1010
|
+
2. **Improved IDE experience**: Better IntelliSense and error reporting for different parts of your codebase.
|
|
1011
|
+
3. **Cleaner separation**: Server code won't incorrectly suggest client-side APIs and vice versa.
|
|
1012
|
+
4. **Performance**: TypeScript can more efficiently check code with properly scoped configurations.
|
|
1013
|
+
|
|
1014
|
+
For example, auto-imports are not available in your `nuxt.config.ts` (but previously this was not flagged by TypeScript). And while IDEs recognized the separate context hinted by `tsconfig.json` in your `server/` directory, this was not reflected in type-checking (requiring a separate step).
|
|
1015
|
+
|
|
1016
|
+
#### Migration Steps
|
|
1017
|
+
|
|
1018
|
+
**No migration is required** - existing projects will continue to work as before.
|
|
1019
|
+
|
|
1020
|
+
However, to take advantage of improved type checking, you can opt in to the new project references approach:
|
|
1021
|
+
|
|
1022
|
+
1. **Update your root `tsconfig.json`** to use project references:
|
|
1023
|
+
|
|
1024
|
+
```json
|
|
1025
|
+
{
|
|
1026
|
+
"files": [],
|
|
1027
|
+
"references": [
|
|
1028
|
+
{ "path": "./.nuxt/tsconfig.app.json" },
|
|
1029
|
+
{ "path": "./.nuxt/tsconfig.server.json" },
|
|
1030
|
+
{ "path": "./.nuxt/tsconfig.node.json" }
|
|
1031
|
+
]
|
|
1032
|
+
}
|
|
1033
|
+
```
|
|
1034
|
+
|
|
1035
|
+
2. **Remove any manual server `tsconfig.json`** files (like `server/tsconfig.json`) that extended `.nuxt/tsconfig.server.json`.
|
|
1036
|
+
|
|
1037
|
+
3. **Update your type checking scripts** to use the build flag for project references:
|
|
1038
|
+
|
|
1039
|
+
```diff
|
|
1040
|
+
- "typecheck": "nuxt prepare && vue-tsc --noEmit"
|
|
1041
|
+
+ "typecheck": "nuxt prepare && vue-tsc -b --noEmit"
|
|
1042
|
+
```
|
|
1043
|
+
|
|
1044
|
+
4. **Configure Node.js TypeScript options** if needed:
|
|
1045
|
+
<!-- @case-police-ignore tsConfig -->
|
|
1046
|
+
|
|
1047
|
+
```ts
|
|
1048
|
+
export default defineNuxtConfig({
|
|
1049
|
+
typescript: {
|
|
1050
|
+
// Customize app/server TypeScript config
|
|
1051
|
+
tsConfig: {
|
|
1052
|
+
compilerOptions: {
|
|
1053
|
+
strict: true
|
|
1054
|
+
}
|
|
1055
|
+
},
|
|
1056
|
+
// Customize build-time TypeScript config
|
|
1057
|
+
nodeTsConfig: {
|
|
1058
|
+
compilerOptions: {
|
|
1059
|
+
strict: true
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
})
|
|
1064
|
+
```
|
|
1065
|
+
|
|
1066
|
+
5. **Update any CI/build scripts** that run TypeScript checking to ensure they use the new project references approach.
|
|
1067
|
+
|
|
1068
|
+
The new configuration provides better type safety and IntelliSense for projects that opt in, while maintaining full backward compatibility for existing setups.
|
|
1069
|
+
|
|
979
1070
|
### Removal of Experimental Features
|
|
980
1071
|
|
|
981
1072
|
🚦 **Impact Level**: Minimal
|
|
@@ -1000,6 +1091,44 @@ These options have been set to their current values for some time and we do not
|
|
|
1000
1091
|
|
|
1001
1092
|
* `respectNoSSRHeader`is implementable in user-land with [server middleware](https://github.com/nuxt/nuxt/blob/c660b39447f0d5b8790c0826092638d321cd6821/packages/nuxt/src/core/runtime/nitro/no-ssr.ts#L8-L9)
|
|
1002
1093
|
|
|
1094
|
+
### Removal of Top-Level `generate` Configuration
|
|
1095
|
+
|
|
1096
|
+
🚦 **Impact Level**: Minimal
|
|
1097
|
+
|
|
1098
|
+
#### What Changed
|
|
1099
|
+
|
|
1100
|
+
The top-level `generate` configuration option is no longer available in Nuxt 4. This includes all of its properties:
|
|
1101
|
+
|
|
1102
|
+
* `generate.exclude` - for excluding routes from prerendering
|
|
1103
|
+
* `generate.routes` - for specifying routes to prerender
|
|
1104
|
+
|
|
1105
|
+
#### Reasons for Change
|
|
1106
|
+
|
|
1107
|
+
The top level `generate` configuration was a holdover from Nuxt 2. We've supported `nitro.prerender` for a while now, and it is the preferred way to configure prerendering in Nuxt 3+.
|
|
1108
|
+
|
|
1109
|
+
#### Migration Steps
|
|
1110
|
+
|
|
1111
|
+
Replace `generate` configuration with the corresponding `nitro.prerender` options:
|
|
1112
|
+
|
|
1113
|
+
```diff
|
|
1114
|
+
export default defineNuxtConfig({
|
|
1115
|
+
- generate: {
|
|
1116
|
+
- exclude: ['/admin', '/private'],
|
|
1117
|
+
- routes: ['/sitemap.xml', '/robots.txt']
|
|
1118
|
+
- }
|
|
1119
|
+
+ nitro: {
|
|
1120
|
+
+ prerender: {
|
|
1121
|
+
+ ignore: ['/admin', '/private'],
|
|
1122
|
+
+ routes: ['/sitemap.xml', '/robots.txt']
|
|
1123
|
+
+ }
|
|
1124
|
+
+ }
|
|
1125
|
+
})
|
|
1126
|
+
```
|
|
1127
|
+
|
|
1128
|
+
::read-more{to="https://nitro.build/config/#prerender"}
|
|
1129
|
+
Read more about Nitro's prerender configuration options.
|
|
1130
|
+
::
|
|
1131
|
+
|
|
1003
1132
|
## Nuxt 2 vs. Nuxt 3+
|
|
1004
1133
|
|
|
1005
1134
|
In the table below, there is a quick comparison between 3 versions of Nuxt:
|
|
@@ -88,7 +88,7 @@ export default defineNuxtConfig({
|
|
|
88
88
|
```
|
|
89
89
|
|
|
90
90
|
::note
|
|
91
|
-
If you do use `ssr: false`, you should also place an HTML file in `~/
|
|
91
|
+
If you do use `ssr: false`, you should also place an HTML file in `~/spa-loading-template.html` with some HTML you would like to use to render a loading screen that will be rendered until your app is hydrated.
|
|
92
92
|
:read-more{title="SPA Loading Template" to="/docs/api/configuration/nuxt-config#spaloadingtemplate"}
|
|
93
93
|
::
|
|
94
94
|
|
|
@@ -16,7 +16,7 @@ It is shipped with many features:
|
|
|
16
16
|
|
|
17
17
|
## API Layer
|
|
18
18
|
|
|
19
|
-
Server [API endpoints](/docs/guide/directory-structure/server#api-routes) and [Middleware](/docs/guide/directory-structure/server#server-middleware) are added by Nitro that internally uses [h3](https://github.com/
|
|
19
|
+
Server [API endpoints](/docs/guide/directory-structure/server#api-routes) and [Middleware](/docs/guide/directory-structure/server#server-middleware) are added by Nitro that internally uses [h3](https://github.com/h3js/h3).
|
|
20
20
|
|
|
21
21
|
Key features include:
|
|
22
22
|
|
|
@@ -24,7 +24,7 @@ Key features include:
|
|
|
24
24
|
- Handlers can return promises, which will be awaited (`res.end()` and `next()` are also supported)
|
|
25
25
|
- Helper functions for body parsing, cookie handling, redirects, headers and more
|
|
26
26
|
|
|
27
|
-
Check out [the h3 docs](https://github.com/
|
|
27
|
+
Check out [the h3 docs](https://github.com/h3js/h3) for more information.
|
|
28
28
|
|
|
29
29
|
::read-more{to="/docs/guide/directory-structure/server#server-routes"}
|
|
30
30
|
Learn more about the API layer in the `server/` directory.
|
|
@@ -55,7 +55,7 @@ This file contains the types of any modules you are using, as well as the key ty
|
|
|
55
55
|
|
|
56
56
|
Some of the references in the file are to files that are only generated within your `buildDir` (`.nuxt`) and therefore for full typings, you will need to run `nuxt dev` or `nuxt build`.
|
|
57
57
|
|
|
58
|
-
### `.nuxt/tsconfig.json`
|
|
58
|
+
### `.nuxt/tsconfig.app.json`
|
|
59
59
|
|
|
60
60
|
This file contains the recommended basic TypeScript configuration for your project, including resolved aliases injected by Nuxt or modules you are using, so you can get full type support and path auto-complete for aliases like `~/file` or `#build/file`.
|
|
61
61
|
|
|
@@ -74,10 +74,37 @@ Nitro also [auto-generates types](/docs/guide/concepts/server-engine#typed-api-r
|
|
|
74
74
|
::
|
|
75
75
|
|
|
76
76
|
::note
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
77
|
+
For backward compatibility, Nuxt still generates `./.nuxt/tsconfig.json`. However, we recommend using [TypeScript project references](/docs/guide/directory-structure/tsconfig) with the new configuration files (`.nuxt/tsconfig.app.json`, `.nuxt/tsconfig.server.json`, etc.) for better type safety and performance.
|
|
78
|
+
|
|
79
|
+
If you do extend from `./.nuxt/tsconfig.json`, keep in mind that all options will be overwritten by those defined in your `tsconfig.json`. Overwriting options such as `"compilerOptions.paths"` with your own configuration will lead TypeScript to not factor in the module resolutions, which can cause module resolutions such as `#imports` to not be recognized.
|
|
80
|
+
|
|
81
|
+
In case you need to extend options further, you can use the [`alias` property](/docs/api/nuxt-config#alias) within your `nuxt.config`. Nuxt will pick them up and extend the generated TypeScript configurations accordingly.
|
|
82
|
+
::
|
|
83
|
+
|
|
84
|
+
## Project References
|
|
85
|
+
|
|
86
|
+
Nuxt uses [TypeScript project references](https://www.typescriptlang.org/docs/handbook/project-references.html) to improve type-checking performance and provide better IDE support. This feature allows TypeScript to break up your codebase into smaller, more manageable pieces.
|
|
87
|
+
|
|
88
|
+
### How Nuxt Uses Project References
|
|
89
|
+
|
|
90
|
+
When you run `nuxt dev` or `nuxt build`, Nuxt will generate multiple `tsconfig.json` files for different parts of your application.
|
|
91
|
+
|
|
92
|
+
- **`.nuxt/tsconfig.app.json`** - Configuration for your application code
|
|
93
|
+
- **`.nuxt/tsconfig.node.json`** - Configuration for your `nuxt.config` and modules
|
|
94
|
+
- **`.nuxt/tsconfig.server.json`** - Configuration for server-side code (when applicable)
|
|
95
|
+
- **`.nuxt/tsconfig.json`** - Legacy configuration for backward compatibility
|
|
96
|
+
|
|
97
|
+
Each of these files is configured to reference the appropriate dependencies and provide optimal type-checking for their specific context.
|
|
98
|
+
|
|
99
|
+
### Benefits of Project References
|
|
100
|
+
|
|
101
|
+
- **Faster builds**: TypeScript can skip rebuilding unchanged projects
|
|
102
|
+
- **Better IDE performance**: Your IDE can provide faster IntelliSense and error checking
|
|
103
|
+
- **Isolated compilation**: Errors in one part of your application don't prevent compilation of other parts
|
|
104
|
+
- **Clearer dependency management**: Each project explicitly declares its dependencies
|
|
105
|
+
|
|
106
|
+
::note
|
|
107
|
+
The project reference setup is handled automatically by Nuxt. You typically don't need to modify these configurations manually, but understanding how they work can help you troubleshoot type-checking issues.
|
|
81
108
|
::
|
|
82
109
|
|
|
83
110
|
## Strict Checks
|
|
@@ -6,7 +6,7 @@ navigation.icon: i-lucide-folder
|
|
|
6
6
|
---
|
|
7
7
|
|
|
8
8
|
::note
|
|
9
|
-
To reduce your application's bundle size, this directory is **optional**, meaning that [`vue-router`](https://router.vuejs.org) won't be included if you only use [`app.vue`](/docs/guide/directory-structure/app). To force the pages system, set `pages: true` in `nuxt.config` or have a [`
|
|
9
|
+
To reduce your application's bundle size, this directory is **optional**, meaning that [`vue-router`](https://router.vuejs.org) won't be included if you only use [`app.vue`](/docs/guide/directory-structure/app). To force the pages system, set `pages: true` in `nuxt.config` or have a [`router.options.ts`](/docs/guide/recipes/custom-routing#using-approuteroptions).
|
|
10
10
|
::
|
|
11
11
|
|
|
12
12
|
## Usage
|
|
@@ -101,7 +101,7 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
101
101
|
|
|
102
102
|
## Server Utilities
|
|
103
103
|
|
|
104
|
-
Server routes are powered by [
|
|
104
|
+
Server routes are powered by [h3js/h3](https://github.com/h3js/h3) which comes with a handy set of helpers.
|
|
105
105
|
|
|
106
106
|
:read-more{to="https://www.jsdocs.io/package/h3#package-index-functions" title="Available H3 Request Helpers" target="_blank"}
|
|
107
107
|
|
|
@@ -136,16 +136,6 @@ export const defineWrappedResponseHandler = <T extends EventHandlerRequest, D> (
|
|
|
136
136
|
This feature is available from Nuxt >= 3.5
|
|
137
137
|
::
|
|
138
138
|
|
|
139
|
-
To improve clarity within your IDE between the auto-imports from 'nitro' and 'vue', you can add a `~/server/tsconfig.json` with the following content:
|
|
140
|
-
|
|
141
|
-
```json [server/tsconfig.json]
|
|
142
|
-
{
|
|
143
|
-
"extends": "../.nuxt/tsconfig.server.json"
|
|
144
|
-
}
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
Currently, these values won't be respected when type checking ([`nuxt typecheck`](/docs/api/commands/typecheck)), but you should get better type hints in your IDE.
|
|
148
|
-
|
|
149
139
|
## Recipes
|
|
150
140
|
|
|
151
141
|
### Route Parameters
|
|
@@ -448,7 +438,7 @@ export default fromNodeMiddleware((req, res) => {
|
|
|
448
438
|
```
|
|
449
439
|
|
|
450
440
|
::important
|
|
451
|
-
Legacy support is possible using [
|
|
441
|
+
Legacy support is possible using [h3js/h3](https://github.com/h3js/h3), but it is advised to avoid legacy handlers as much as you can.
|
|
452
442
|
::
|
|
453
443
|
|
|
454
444
|
```ts [server/middleware/legacy.ts]
|
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "tsconfig.json"
|
|
3
|
-
description: "Nuxt generates
|
|
3
|
+
description: "Nuxt generates multiple TypeScript configuration files with sensible defaults and your aliases."
|
|
4
4
|
head.title: "tsconfig.json"
|
|
5
5
|
navigation.icon: i-lucide-file
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
Nuxt [automatically generates](/docs/guide/concepts/typescript)
|
|
8
|
+
Nuxt [automatically generates](/docs/guide/concepts/typescript) multiple TypeScript configuration files (`.nuxt/tsconfig.app.json`, `.nuxt/tsconfig.server.json`, `.nuxt/tsconfig.node.json`) with the resolved aliases you are using in your Nuxt project, as well as with other sensible defaults.
|
|
9
9
|
|
|
10
10
|
You can benefit from this by creating a `tsconfig.json` in the root of your project with the following content:
|
|
11
11
|
|
|
12
12
|
```json [tsconfig.json]
|
|
13
13
|
{
|
|
14
|
-
"
|
|
14
|
+
"files": [],
|
|
15
|
+
"references": [
|
|
16
|
+
{
|
|
17
|
+
"path": "./.nuxt/tsconfig.app.json"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"path": "./.nuxt/tsconfig.server.json"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"path": "./.nuxt/tsconfig.node.json"
|
|
24
|
+
}
|
|
25
|
+
]
|
|
15
26
|
}
|
|
16
27
|
```
|
|
17
28
|
|
|
@@ -386,6 +386,7 @@ package-lock.json
|
|
|
386
386
|
yarn.lock
|
|
387
387
|
pnpm-lock.yaml
|
|
388
388
|
tsconfig.json
|
|
389
|
+
bun.lock
|
|
389
390
|
bun.lockb
|
|
390
391
|
```
|
|
391
392
|
|
|
@@ -454,7 +455,7 @@ By setting `experimental.normalizeComponentNames`, these two values match, and V
|
|
|
454
455
|
|
|
455
456
|
## spaLoadingTemplateLocation
|
|
456
457
|
|
|
457
|
-
When rendering a client-only page (with `ssr: false`), we optionally render a loading screen (from
|
|
458
|
+
When rendering a client-only page (with `ssr: false`), we optionally render a loading screen (from `~/spa-loading-template.html`).
|
|
458
459
|
|
|
459
460
|
It can be set to `within`, which will render it like this:
|
|
460
461
|
|
|
@@ -32,7 +32,7 @@ Update `nuxt` dependency inside `package.json`:
|
|
|
32
32
|
}
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
Remove lockfile (`package-lock.json`, `yarn.lock`, `pnpm-lock.yaml`, or `bun.lockb`) and reinstall dependencies.
|
|
35
|
+
Remove lockfile (`package-lock.json`, `yarn.lock`, `pnpm-lock.yaml`, `bun.lock` or `bun.lockb`) and reinstall dependencies.
|
|
36
36
|
|
|
37
37
|
## Opting Out
|
|
38
38
|
|
|
@@ -47,7 +47,7 @@ Update `nuxt` dependency inside `package.json`:
|
|
|
47
47
|
}
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
Remove lockfile (`package-lock.json`, `yarn.lock`, `pnpm-lock.yaml`, or `bun.lockb`) and reinstall dependencies.
|
|
50
|
+
Remove lockfile (`package-lock.json`, `yarn.lock`, `pnpm-lock.yaml`, `bun.lock` or `bun.lockb`) and reinstall dependencies.
|
|
51
51
|
|
|
52
52
|
## Using Nightly `@nuxt/cli`
|
|
53
53
|
|
|
@@ -230,8 +230,6 @@ Learn more about asset injection in [the recipes section](#recipes).
|
|
|
230
230
|
Published modules cannot leverage auto-imports for assets within their runtime directory. Instead, they have to import them explicitly from `#imports` or alike.
|
|
231
231
|
:br :br
|
|
232
232
|
Indeed, auto-imports are not enabled for files within `node_modules` (the location where a published module will eventually live) for performance reasons.
|
|
233
|
-
:br :br
|
|
234
|
-
If you are using the module starter, auto-imports will not be enabled in your playground either.
|
|
235
233
|
::
|
|
236
234
|
|
|
237
235
|
### Tooling
|
|
@@ -13,7 +13,7 @@ Using [router options](/docs/guide/recipes/custom-routing#router-options), you c
|
|
|
13
13
|
|
|
14
14
|
If it returns `null` or `undefined`, Nuxt will fall back to the default routes (useful to modify input array).
|
|
15
15
|
|
|
16
|
-
```ts [
|
|
16
|
+
```ts [router.options.ts]
|
|
17
17
|
import type { RouterConfig } from '@nuxt/schema'
|
|
18
18
|
|
|
19
19
|
export default {
|
|
@@ -83,11 +83,11 @@ The [Nuxt kit](/docs/guide/going-further/kit) provides a few ways [to add routes
|
|
|
83
83
|
|
|
84
84
|
On top of customizing options for [`vue-router`](https://router.vuejs.org/api/interfaces/routeroptions.html), Nuxt offers [additional options](/docs/api/nuxt-config#router) to customize the router.
|
|
85
85
|
|
|
86
|
-
### Using `
|
|
86
|
+
### Using `router.options`
|
|
87
87
|
|
|
88
88
|
This is the recommended way to specify [router options](/docs/api/nuxt-config#router).
|
|
89
89
|
|
|
90
|
-
```ts [
|
|
90
|
+
```ts [router.options.ts]
|
|
91
91
|
import type { RouterConfig } from '@nuxt/schema'
|
|
92
92
|
|
|
93
93
|
export default {
|
|
@@ -109,7 +109,7 @@ export default defineNuxtConfig({
|
|
|
109
109
|
const resolver = createResolver(import.meta.url)
|
|
110
110
|
// add a route
|
|
111
111
|
files.push({
|
|
112
|
-
path: resolver.resolve('./runtime/
|
|
112
|
+
path: resolver.resolve('./runtime/router-options'),
|
|
113
113
|
optional: true
|
|
114
114
|
})
|
|
115
115
|
}
|
|
@@ -170,7 +170,7 @@ export default defineNuxtConfig({
|
|
|
170
170
|
|
|
171
171
|
You can optionally override history mode using a function that accepts the base URL and returns the history mode. If it returns `null` or `undefined`, Nuxt will fallback to the default history.
|
|
172
172
|
|
|
173
|
-
```ts [
|
|
173
|
+
```ts [router.options.ts]
|
|
174
174
|
import type { RouterConfig } from '@nuxt/schema'
|
|
175
175
|
import { createMemoryHistory } from 'vue-router'
|
|
176
176
|
|
|
@@ -51,6 +51,10 @@ In this example, we pass the `id` param to link to the route `~/pages/posts/[id]
|
|
|
51
51
|
Check out the Pages panel in Nuxt DevTools to see the route name and the params it might take.
|
|
52
52
|
::
|
|
53
53
|
|
|
54
|
+
::tip
|
|
55
|
+
When you pass an object into the `to` prop, `<NuxtLink>` will inherit Vue Router’s handling of query parameters. Keys and values will be automatically encoded, so you don’t need to call `encodeURI` or `encodeURIComponent` manually.
|
|
56
|
+
::
|
|
57
|
+
|
|
54
58
|
### Handling Static File and Cross-App Links
|
|
55
59
|
|
|
56
60
|
By default, `<NuxtLink>` uses Vue Router's client side navigation for relative route. When linking to static files in the `/public` directory or to another application hosted on the same domain, it might result in unexpected 404 errors because they are not part of the client routes. In such cases, you can use the `external` prop with `<NuxtLink>` to bypass Vue Router's internal routing mechanism.
|
|
@@ -12,23 +12,33 @@ links:
|
|
|
12
12
|
This composable is available in Nuxt v3.12+.
|
|
13
13
|
::
|
|
14
14
|
|
|
15
|
-
`onPrehydrate` is a composable lifecycle hook that allows you to run a callback on the client immediately before
|
|
16
|
-
Nuxt hydrates the page.
|
|
17
|
-
|
|
15
|
+
`onPrehydrate` is a composable lifecycle hook that allows you to run a callback on the client immediately before Nuxt hydrates the page.
|
|
18
16
|
::note
|
|
19
17
|
This is an advanced utility and should be used with care. For example, [`nuxt-time`](https://github.com/danielroe/nuxt-time/pull/251) and [`@nuxtjs/color-mode`](https://github.com/nuxt-modules/color-mode/blob/main/src/script.js) manipulate the DOM to avoid hydration mismatches.
|
|
20
18
|
::
|
|
21
19
|
|
|
22
20
|
## Usage
|
|
23
21
|
|
|
24
|
-
`onPrehydrate`
|
|
25
|
-
|
|
22
|
+
Call `onPrehydrate` in the setup function of a Vue component (e.g., in `<script setup>`) or in a plugin. It only has an effect when called on the server and will not be included in your client build.
|
|
23
|
+
|
|
24
|
+
## Type
|
|
25
|
+
|
|
26
|
+
```ts [Signature]
|
|
27
|
+
export function onPrehydrate(callback: (el: HTMLElement) => void): void
|
|
28
|
+
export function onPrehydrate(callback: string | ((el: HTMLElement) => void), key?: string): undefined | string
|
|
29
|
+
```
|
|
26
30
|
|
|
27
31
|
## Parameters
|
|
28
32
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
before Nuxt runtime initializes so it should not rely on
|
|
33
|
+
| Parameter | Type | Required | Description |
|
|
34
|
+
| ---- | --- | --- | --- |
|
|
35
|
+
| `callback` | `((el: HTMLElement) => void) \| string` | Yes | A function (or stringified function) to run before Nuxt hydrates. It will be stringified and inlined in the HTML. Should not have external dependencies or reference variables outside the callback. Runs before Nuxt runtime initializes, so it should not rely on Nuxt or Vue context. |
|
|
36
|
+
| `key` | `string` | No | (Advanced) A unique key to identify the prehydrate script, useful for advanced scenarios like multiple root nodes. |
|
|
37
|
+
|
|
38
|
+
## Return Values
|
|
39
|
+
|
|
40
|
+
- Returns `undefined` when called with only a callback function.
|
|
41
|
+
- Returns a string (the prehydrate id) when called with a callback and a key, which can be used to set or access the `data-prehydrate-id` attribute for advanced use cases.
|
|
32
42
|
|
|
33
43
|
## Example
|
|
34
44
|
|
|
@@ -36,19 +46,18 @@ before Nuxt runtime initializes so it should not rely on the Nuxt or Vue context
|
|
|
36
46
|
<script setup lang="ts">
|
|
37
47
|
declare const window: Window
|
|
38
48
|
// ---cut---
|
|
39
|
-
//
|
|
49
|
+
// Run code before Nuxt hydrates
|
|
40
50
|
onPrehydrate(() => {
|
|
41
51
|
console.log(window)
|
|
42
52
|
})
|
|
43
53
|
|
|
44
|
-
//
|
|
54
|
+
// Access the root element
|
|
45
55
|
onPrehydrate((el) => {
|
|
46
56
|
console.log(el.outerHTML)
|
|
47
57
|
// <div data-v-inspector="app.vue:15:3" data-prehydrate-id=":b3qlvSiBeH:"> Hi there </div>
|
|
48
58
|
})
|
|
49
59
|
|
|
50
|
-
//
|
|
51
|
-
// can access/set `data-prehydrate-id` yourself
|
|
60
|
+
// Advanced: access/set `data-prehydrate-id` yourself
|
|
52
61
|
const prehydrateId = onPrehydrate((el) => {})
|
|
53
62
|
</script>
|
|
54
63
|
|
|
@@ -154,7 +154,7 @@ const { data: users2 } = useAsyncData('users', () => $fetch('/api/users'), { imm
|
|
|
154
154
|
- `pending`: the request is in progress
|
|
155
155
|
- `success`: the request has completed successfully
|
|
156
156
|
- `error`: the request has failed
|
|
157
|
-
- `clear`: a function
|
|
157
|
+
- `clear`: a function that can be used to set `data` to `undefined` (or the value of `options.default()` if provided), set `error` to `undefined`, set `status` to `idle`, and mark any currently pending requests as cancelled.
|
|
158
158
|
|
|
159
159
|
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
|
|
160
160
|
|