@nuxt/docs 4.0.0-alpha.4 → 4.0.0
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/02.installation.md +5 -5
- package/1.getting-started/18.upgrade.md +79 -83
- package/2.guide/1.concepts/10.nuxt-lifecycle.md +14 -4
- package/2.guide/1.concepts/8.typescript.md +1 -0
- package/2.guide/2.directory-structure/1.shared.md +0 -6
- package/2.guide/2.directory-structure/2.env.md +4 -0
- package/2.guide/2.directory-structure/3.tsconfig.md +4 -1
- package/2.guide/3.going-further/1.events.md +3 -22
- package/2.guide/3.going-further/1.experimental-features.md +11 -10
- package/2.guide/3.going-further/1.features.md +2 -44
- package/2.guide/3.going-further/2.hooks.md +21 -2
- package/2.guide/3.going-further/3.modules.md +25 -0
- package/3.api/2.composables/use-async-data.md +9 -5
- package/3.api/2.composables/use-fetch.md +14 -9
- package/3.api/2.composables/use-runtime-config.md +1 -1
- package/3.api/4.commands/init.md +2 -0
- package/3.api/5.kit/9.plugins.md +0 -48
- package/3.api/6.nuxt-config.md +2 -35
- package/7.migration/2.configuration.md +3 -0
- package/package.json +1 -1
|
@@ -39,23 +39,23 @@ Open a terminal (if you're using [Visual Studio Code](https://code.visualstudio.
|
|
|
39
39
|
::code-group{sync="pm"}
|
|
40
40
|
|
|
41
41
|
```bash [npm]
|
|
42
|
-
npm create nuxt <project-name>
|
|
42
|
+
npm create nuxt <project-name> -- -t v4
|
|
43
43
|
```
|
|
44
44
|
|
|
45
45
|
```bash [yarn]
|
|
46
|
-
yarn create nuxt <project-name>
|
|
46
|
+
yarn create nuxt <project-name> -t v4
|
|
47
47
|
```
|
|
48
48
|
|
|
49
49
|
```bash [pnpm]
|
|
50
|
-
pnpm create nuxt <project-name>
|
|
50
|
+
pnpm create nuxt <project-name> -t v4
|
|
51
51
|
```
|
|
52
52
|
|
|
53
53
|
```bash [bun]
|
|
54
|
-
bun create nuxt <project-name>
|
|
54
|
+
bun create nuxt <project-name> -t v4
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
```bash [deno]
|
|
58
|
-
deno -A npm:create-nuxt@latest <project-name>
|
|
58
|
+
deno -A npm:create-nuxt@latest <project-name> -t v4
|
|
59
59
|
```
|
|
60
60
|
|
|
61
61
|
::
|
|
@@ -34,76 +34,37 @@ bun x nuxt upgrade
|
|
|
34
34
|
|
|
35
35
|
To use the latest Nuxt build and test features before their release, read about the [nightly release channel](/docs/guide/going-further/nightly-release-channel) guide.
|
|
36
36
|
|
|
37
|
-
|
|
38
|
-
The nightly release channel `latest` tag is currently tracking the Nuxt v4 branch, meaning that it is particularly likely to have breaking changes right now — be careful! You can opt in to the 3.x branch nightly releases with `"nuxt": "npm:nuxt-nightly@3x"`.
|
|
39
|
-
::
|
|
40
|
-
|
|
41
|
-
## Testing Nuxt 4
|
|
37
|
+
## Migrating to Nuxt 4
|
|
42
38
|
|
|
43
|
-
Nuxt 4
|
|
39
|
+
Nuxt 4 includes significant improvements and changes. This guide will help you migrate your existing Nuxt 3 application to Nuxt 4.
|
|
44
40
|
|
|
45
|
-
|
|
41
|
+
First, upgrade to Nuxt 4:
|
|
46
42
|
|
|
47
|
-
|
|
43
|
+
::code-group{sync="pm"}
|
|
48
44
|
|
|
49
|
-
|
|
45
|
+
```bash [npm]
|
|
46
|
+
npm install nuxt@^4.0.0-rc
|
|
47
|
+
```
|
|
50
48
|
|
|
51
|
-
|
|
49
|
+
```bash [yarn]
|
|
50
|
+
yarn add nuxt@^4.0.0-rc
|
|
51
|
+
```
|
|
52
52
|
|
|
53
|
-
|
|
53
|
+
```bash [pnpm]
|
|
54
|
+
pnpm add nuxt@^4.0.0-rc
|
|
55
|
+
```
|
|
54
56
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
export default defineNuxtConfig({
|
|
58
|
-
future: {
|
|
59
|
-
compatibilityVersion: 4,
|
|
60
|
-
},
|
|
61
|
-
// To re-enable _all_ Nuxt v3 behavior, set the following options:
|
|
62
|
-
// srcDir: '.',
|
|
63
|
-
// dir: {
|
|
64
|
-
// app: 'app'
|
|
65
|
-
// },
|
|
66
|
-
// experimental: {
|
|
67
|
-
// scanPageMeta: 'after-resolve',
|
|
68
|
-
// sharedPrerenderData: false,
|
|
69
|
-
// compileTemplate: true,
|
|
70
|
-
// resetAsyncDataToUndefined: true,
|
|
71
|
-
// templateUtils: true,
|
|
72
|
-
// relativeWatchPaths: true,
|
|
73
|
-
// normalizeComponentNames: false,
|
|
74
|
-
// spaLoadingTemplateLocation: 'within',
|
|
75
|
-
// parseErrorData: false,
|
|
76
|
-
// pendingWhenIdle: true,
|
|
77
|
-
// alwaysRunFetchOnKeyChange: true,
|
|
78
|
-
// defaults: {
|
|
79
|
-
// useAsyncData: {
|
|
80
|
-
// deep: true
|
|
81
|
-
// }
|
|
82
|
-
// }
|
|
83
|
-
// },
|
|
84
|
-
// features: {
|
|
85
|
-
// inlineStyles: true
|
|
86
|
-
// },
|
|
87
|
-
// unhead: {
|
|
88
|
-
// renderSSRHeadOptions: {
|
|
89
|
-
// omitLineBreaks: false
|
|
90
|
-
// }
|
|
91
|
-
// }
|
|
92
|
-
})
|
|
57
|
+
```bash [bun]
|
|
58
|
+
bun add nuxt@^4.0.0-rc
|
|
93
59
|
```
|
|
94
|
-
::
|
|
95
60
|
|
|
96
|
-
::note
|
|
97
|
-
For now, you need to define the compatibility version in each layer that opts into Nuxt 4 behavior. This will not be required after Nuxt 4 is released.
|
|
98
61
|
::
|
|
99
62
|
|
|
100
|
-
|
|
63
|
+
After upgrading, most Nuxt 4 behaviors are now the default. However, some features can still be configured if you need to maintain backward compatibility during your migration.
|
|
101
64
|
|
|
102
|
-
|
|
65
|
+
The following sections detail the key changes and migrations needed when upgrading to Nuxt 4.
|
|
103
66
|
|
|
104
|
-
|
|
105
|
-
This section is subject to change until the final release, so please check back here regularly if you are testing Nuxt 4 using `compatibilityVersion: 4`.
|
|
106
|
-
::
|
|
67
|
+
Breaking or significant changes are documented below along with migration steps and available configuration options.
|
|
107
68
|
|
|
108
69
|
### Migrating Using Codemods
|
|
109
70
|
|
|
@@ -303,6 +264,64 @@ export default defineNuxtConfig({
|
|
|
303
264
|
})
|
|
304
265
|
```
|
|
305
266
|
|
|
267
|
+
### Corrected Module Loading Order in Layers
|
|
268
|
+
|
|
269
|
+
🚦 **Impact Level**: Minimal
|
|
270
|
+
|
|
271
|
+
#### What Changed
|
|
272
|
+
|
|
273
|
+
The order in which modules are loaded when using [Nuxt layers](/docs/guide/going-further/layers) has been corrected. Previously, modules from the project root were loaded before modules from extended layers, which was the reverse of the expected behavior.
|
|
274
|
+
|
|
275
|
+
Now modules are loaded in the correct order:
|
|
276
|
+
|
|
277
|
+
1. **Layer modules first** (in extend order - deeper layers first)
|
|
278
|
+
2. **Project modules last** (highest priority)
|
|
279
|
+
|
|
280
|
+
This affects both:
|
|
281
|
+
* Modules defined in the `modules` array in `nuxt.config.ts`
|
|
282
|
+
* Auto-discovered modules from the `modules/` directory
|
|
283
|
+
|
|
284
|
+
#### Reasons for Change
|
|
285
|
+
|
|
286
|
+
This change ensures that:
|
|
287
|
+
* Extended layers have lower priority than the consuming project
|
|
288
|
+
* Module execution order matches the intuitive layer inheritance pattern
|
|
289
|
+
* Module configuration and hooks work as expected in multi-layer setups
|
|
290
|
+
|
|
291
|
+
#### Migration Steps
|
|
292
|
+
|
|
293
|
+
**Most projects will not need changes**, as this corrects the loading order to match expected behavior.
|
|
294
|
+
|
|
295
|
+
However, if your project was relying on the previous incorrect order, you may need to:
|
|
296
|
+
|
|
297
|
+
1. **Review module dependencies**: Check if any modules depend on specific loading order
|
|
298
|
+
2. **Adjust module configuration**: If modules were configured to work around the incorrect order
|
|
299
|
+
3. **Test thoroughly**: Ensure all functionality works as expected with the corrected order
|
|
300
|
+
|
|
301
|
+
Example of the new correct order:
|
|
302
|
+
```ts
|
|
303
|
+
// Layer: my-layer/nuxt.config.ts
|
|
304
|
+
export default defineNuxtConfig({
|
|
305
|
+
modules: ['layer-module-1', 'layer-module-2']
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
// Project: nuxt.config.ts
|
|
309
|
+
export default defineNuxtConfig({
|
|
310
|
+
extends: ['./my-layer'],
|
|
311
|
+
modules: ['project-module-1', 'project-module-2']
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
// Loading order (corrected):
|
|
315
|
+
// 1. layer-module-1
|
|
316
|
+
// 2. layer-module-2
|
|
317
|
+
// 3. project-module-1 (can override layer modules)
|
|
318
|
+
// 4. project-module-2 (can override layer modules)
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
If you encounter issues with module order dependencies due to needing to register a hook, consider using the [`modules:done` hook](/docs/guide/going-further/modules#custom-hooks) for modules that need to call a hook. This is run after all other modules have been loaded, which means it is safe to use.
|
|
322
|
+
|
|
323
|
+
👉 See [PR #31507](https://github.com/nuxt/nuxt/pull/31507) and [issue #25719](https://github.com/nuxt/nuxt/issues/25719) for more details.
|
|
324
|
+
|
|
306
325
|
### Deduplication of Route Metadata
|
|
307
326
|
|
|
308
327
|
🚦 **Impact Level**: Minimal
|
|
@@ -490,16 +509,6 @@ Update your custom `error.vue` to remove any additional parsing of `error.data`:
|
|
|
490
509
|
</script>
|
|
491
510
|
```
|
|
492
511
|
|
|
493
|
-
Alternatively, you can disable this change:
|
|
494
|
-
|
|
495
|
-
```ts twoslash [nuxt.config.ts]
|
|
496
|
-
export default defineNuxtConfig({
|
|
497
|
-
experimental: {
|
|
498
|
-
parseErrorData: false
|
|
499
|
-
},
|
|
500
|
-
})
|
|
501
|
-
```
|
|
502
|
-
|
|
503
512
|
### More Granular Inline Styles
|
|
504
513
|
|
|
505
514
|
🚦 **Impact Level**: Moderate
|
|
@@ -697,21 +706,6 @@ If you provide a custom `default` value for `useAsyncData`, this will now be use
|
|
|
697
706
|
|
|
698
707
|
Often users set an appropriately empty value, such as an empty array, to avoid the need to check for `null`/`undefined` when iterating over it. This should be respected when resetting/clearing the data.
|
|
699
708
|
|
|
700
|
-
#### Migration Steps
|
|
701
|
-
|
|
702
|
-
If you encounter any issues you can revert back to the previous behavior, for now, with:
|
|
703
|
-
|
|
704
|
-
```ts twoslash [nuxt.config.ts]
|
|
705
|
-
// @errors: 2353
|
|
706
|
-
export default defineNuxtConfig({
|
|
707
|
-
experimental: {
|
|
708
|
-
resetAsyncDataToUndefined: true,
|
|
709
|
-
}
|
|
710
|
-
})
|
|
711
|
-
```
|
|
712
|
-
|
|
713
|
-
Please report an issue if you are doing so, as we do not plan to keep this as configurable.
|
|
714
|
-
|
|
715
709
|
### Alignment of `pending` value in `useAsyncData` and `useFetch`
|
|
716
710
|
|
|
717
711
|
🚦 **Impact Level**: Medium
|
|
@@ -784,7 +778,7 @@ This change should generally improve the expected behavior, but if you were expe
|
|
|
784
778
|
query: { id },
|
|
785
779
|
immediate: false
|
|
786
780
|
)
|
|
787
|
-
+ watch(id, execute, { once: true })
|
|
781
|
+
+ watch(id, () => execute(), { once: true })
|
|
788
782
|
```
|
|
789
783
|
|
|
790
784
|
To opt out of this behavior:
|
|
@@ -992,6 +986,7 @@ Nuxt now generates separate TypeScript configurations for different contexts to
|
|
|
992
986
|
* `.nuxt/tsconfig.app.json` - For your app code (Vue components, composables, etc.)
|
|
993
987
|
* `.nuxt/tsconfig.server.json` - For your server-side code (Nitro/server directory)
|
|
994
988
|
* `.nuxt/tsconfig.node.json` - For your build-time code (modules, `nuxt.config.ts`, etc.)
|
|
989
|
+
* `.nuxt/tsconfig.shared.json` - For code shared between app and server contexts (like types and non-environment specific utilities)
|
|
995
990
|
* `.nuxt/tsconfig.json` - Legacy configuration for backward compatibility
|
|
996
991
|
|
|
997
992
|
2. **Backward compatibility**: Existing projects that extend `.nuxt/tsconfig.json` will continue to work as before.
|
|
@@ -1027,6 +1022,7 @@ However, to take advantage of improved type checking, you can opt in to the new
|
|
|
1027
1022
|
"references": [
|
|
1028
1023
|
{ "path": "./.nuxt/tsconfig.app.json" },
|
|
1029
1024
|
{ "path": "./.nuxt/tsconfig.server.json" },
|
|
1025
|
+
{ "path": "./.nuxt/tsconfig.shared.json" },
|
|
1030
1026
|
{ "path": "./.nuxt/tsconfig.node.json" }
|
|
1031
1027
|
]
|
|
1032
1028
|
}
|
|
@@ -76,17 +76,27 @@ Any redirection on the server will result in a `Location:` header being sent to
|
|
|
76
76
|
|
|
77
77
|
:read-more{to="/docs/guide/directory-structure/middleware"}
|
|
78
78
|
|
|
79
|
-
### Step 6:
|
|
79
|
+
### Step 6: Render Page and Components
|
|
80
80
|
|
|
81
|
-
Nuxt
|
|
81
|
+
Nuxt renders the page and its components and fetches any required data with `useFetch` and `useAsyncData` during this step. Since there are no dynamic updates and no DOM operations occur on the server, Vue lifecycle hooks such as `onBeforeMount`, `onMounted`, and subsequent hooks are **NOT** executed during SSR.
|
|
82
|
+
|
|
83
|
+
By default, Vue pauses dependency tracking during SSR for better performance.
|
|
84
|
+
|
|
85
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
86
|
+
There is no reactivity on the server side because Vue SSR renders the app top-down as static HTML, making it impossible to go back and modify content that has already been rendered.
|
|
87
|
+
::
|
|
82
88
|
|
|
83
89
|
::important
|
|
84
90
|
You should avoid code that produces side effects that need cleanup in root scope of `<script setup>`. An example of such side effects is setting up timers with `setInterval`. In client-side only code we may setup a timer and then tear it down in `onBeforeUnmount` or `onUnmounted`. However, because the unmount hooks will never be called during SSR, the timers will stay around forever. To avoid this, move your side-effect code into `onMounted` instead.
|
|
85
91
|
::
|
|
86
92
|
|
|
87
|
-
|
|
93
|
+
::tip{icon="i-lucide-video" to="https://youtu.be/dZSNW07sO-A" target="_blank"}
|
|
94
|
+
Watch a video from Daniel Roe explaining Server Rendering and Global State.
|
|
95
|
+
::
|
|
96
|
+
|
|
97
|
+
### Step 7: Generate HTML Output
|
|
88
98
|
|
|
89
|
-
After all
|
|
99
|
+
After all required data is fetched and the components are rendered, Nuxt combines the rendered components with settings from `unhead` to generate a complete HTML document. This HTML, along with the associated data, is then sent back to the client to complete the SSR process.
|
|
90
100
|
|
|
91
101
|
::callout{icon="i-lucide-lightbulb"}
|
|
92
102
|
After rendering the Vue application to HTML, Nuxt calls the [`app:rendered`](/docs/api/advanced/hooks#app-hooks-runtime) hook.
|
|
@@ -92,6 +92,7 @@ When you run `nuxt dev` or `nuxt build`, Nuxt will generate multiple `tsconfig.j
|
|
|
92
92
|
- **`.nuxt/tsconfig.app.json`** - Configuration for your application code
|
|
93
93
|
- **`.nuxt/tsconfig.node.json`** - Configuration for your `nuxt.config` and modules
|
|
94
94
|
- **`.nuxt/tsconfig.server.json`** - Configuration for server-side code (when applicable)
|
|
95
|
+
- **`.nuxt/tsconfig.shared.json`** - For code shared between app and server contexts (like types and non-environment specific utilities)
|
|
95
96
|
- **`.nuxt/tsconfig.json`** - Legacy configuration for backward compatibility
|
|
96
97
|
|
|
97
98
|
Each of these files is configured to reference the appropriate dependencies and provide optimal type-checking for their specific context.
|
|
@@ -15,12 +15,6 @@ The `shared/` directory is available in Nuxt v3.14+.
|
|
|
15
15
|
Code in the `shared/` directory cannot import any Vue or Nitro code.
|
|
16
16
|
::
|
|
17
17
|
|
|
18
|
-
::warning
|
|
19
|
-
Auto-imports are not enabled by default in Nuxt v3 to prevent breaking changes in existing projects.
|
|
20
|
-
|
|
21
|
-
To use these auto-imported utils and types, you must first [set `future.compatibilityVersion: 4` in your `nuxt.config.ts`](/docs/getting-started/upgrade#opting-in-to-nuxt-4).
|
|
22
|
-
::
|
|
23
|
-
|
|
24
18
|
:video-accordion{title="Watch a video from Vue School on sharing utils and types between app and server" videoId="nnAR-MO3q5M"}
|
|
25
19
|
|
|
26
20
|
## Usage
|
|
@@ -55,6 +55,10 @@ Since `.env` files are not used in production, you must explicitly set environme
|
|
|
55
55
|
|
|
56
56
|
* Many cloud service providers, such as Vercel, Netlify, and AWS, provide interfaces for setting environment variables via their dashboards, CLI tools or configuration files.
|
|
57
57
|
|
|
58
|
+
::important
|
|
59
|
+
`runtimeConfig` [won't pick up environment variables that don't start with `NUXT_` in production] (https://nuxt.com/docs/guide/going-further/runtime-config#environment-variables).
|
|
60
|
+
::
|
|
61
|
+
|
|
58
62
|
## Production Preview
|
|
59
63
|
|
|
60
64
|
For local production preview purpose, we recommend using [`nuxt preview`](/docs/api/commands/preview) since using this command, the `.env` file will be loaded into `process.env` for convenience. Note that this command requires dependencies to be installed in the package directory.
|
|
@@ -5,7 +5,7 @@ head.title: "tsconfig.json"
|
|
|
5
5
|
navigation.icon: i-lucide-file
|
|
6
6
|
---
|
|
7
7
|
|
|
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.
|
|
8
|
+
Nuxt [automatically generates](/docs/guide/concepts/typescript) multiple TypeScript configuration files (`.nuxt/tsconfig.app.json`, `.nuxt/tsconfig.server.json`, `.nuxt/tsconfig.node.json` and `.nuxt/tsconfig.shared.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
|
|
|
@@ -19,6 +19,9 @@ You can benefit from this by creating a `tsconfig.json` in the root of your proj
|
|
|
19
19
|
{
|
|
20
20
|
"path": "./.nuxt/tsconfig.server.json"
|
|
21
21
|
},
|
|
22
|
+
{
|
|
23
|
+
"path": "./.nuxt/tsconfig.shared.json"
|
|
24
|
+
},
|
|
22
25
|
{
|
|
23
26
|
"path": "./.nuxt/tsconfig.node.json"
|
|
24
27
|
}
|
|
@@ -58,25 +58,6 @@ await nuxtApp.callHook('app:user:registered', {
|
|
|
58
58
|
You can inspect all events using the **Nuxt DevTools** Hooks panel.
|
|
59
59
|
::
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
```ts
|
|
66
|
-
import { HookResult } from "@nuxt/schema";
|
|
67
|
-
|
|
68
|
-
declare module '#app' {
|
|
69
|
-
interface RuntimeNuxtHooks {
|
|
70
|
-
'your-nuxt-runtime-hook': () => HookResult
|
|
71
|
-
}
|
|
72
|
-
interface NuxtHooks {
|
|
73
|
-
'your-nuxt-hook': () => HookResult
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
declare module 'nitropack/types' {
|
|
78
|
-
interface NitroRuntimeHooks {
|
|
79
|
-
'your-nitro-hook': () => void;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
```
|
|
61
|
+
::read-more{to="/docs/guide/going-further/hooks"}
|
|
62
|
+
Learn more about Nuxt's built-in hooks and how to extend them
|
|
63
|
+
::
|
|
@@ -3,7 +3,7 @@ title: "Experimental Features"
|
|
|
3
3
|
description: "Enable Nuxt experimental features to unlock new possibilities."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Nuxt includes experimental features that you can enable in your configuration file.
|
|
7
7
|
|
|
8
8
|
Internally, Nuxt uses `@nuxt/schema` to define these experimental features. You can refer to the [API documentation](/docs/api/configuration/nuxt-config#experimental) or the [source code](https://github.com/nuxt/nuxt/blob/main/packages/schema/src/config/experimental.ts) for more information.
|
|
9
9
|
|
|
@@ -284,14 +284,14 @@ export default defineNuxtConfig({
|
|
|
284
284
|
|
|
285
285
|
## sharedPrerenderData
|
|
286
286
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
287
|
+
Nuxt automatically shares payload *data* between pages that are prerendered. This can result in a significant performance improvement when prerendering sites that use `useAsyncData` or `useFetch` and fetch the same data in different pages.
|
|
288
|
+
|
|
289
|
+
You can disable this feature if needed.
|
|
290
290
|
|
|
291
291
|
```ts twoslash [nuxt.config.ts]
|
|
292
292
|
export default defineNuxtConfig({
|
|
293
293
|
experimental: {
|
|
294
|
-
sharedPrerenderData:
|
|
294
|
+
sharedPrerenderData: false
|
|
295
295
|
}
|
|
296
296
|
})
|
|
297
297
|
```
|
|
@@ -332,11 +332,11 @@ globalThis.Buffer = globalThis.Buffer || Buffer
|
|
|
332
332
|
|
|
333
333
|
## scanPageMeta
|
|
334
334
|
|
|
335
|
-
|
|
335
|
+
Nuxt exposing some route metadata defined in `definePageMeta` at build-time to modules (specifically `alias`, `name`, `path`, `redirect`, `props` and `middleware`).
|
|
336
336
|
|
|
337
337
|
This only works with static or strings/arrays rather than variables or conditional assignment. See [original issue](https://github.com/nuxt/nuxt/issues/24770) for more information and context.
|
|
338
338
|
|
|
339
|
-
|
|
339
|
+
By default page metadata is only scanned after all routes have been registered in `pages:extend`. Then another hook, `pages:resolved` will be called.
|
|
340
340
|
|
|
341
341
|
You can disable this feature if it causes issues in your project.
|
|
342
342
|
|
|
@@ -427,13 +427,14 @@ This allows modules to access additional metadata from the page metadata in the
|
|
|
427
427
|
|
|
428
428
|
## normalizeComponentNames
|
|
429
429
|
|
|
430
|
-
|
|
431
|
-
|
|
430
|
+
Nuxt updates auto-generated Vue component names to match the full component name you would use to auto-import the component.
|
|
431
|
+
|
|
432
|
+
If you encounter issues, you can disable this feature.
|
|
432
433
|
|
|
433
434
|
```ts twoslash [nuxt.config.ts]
|
|
434
435
|
export default defineNuxtConfig({
|
|
435
436
|
experimental: {
|
|
436
|
-
normalizeComponentNames:
|
|
437
|
+
normalizeComponentNames: false
|
|
437
438
|
}
|
|
438
439
|
})
|
|
439
440
|
```
|
|
@@ -39,51 +39,9 @@ There is also a `future` namespace for early opting-in to new features that will
|
|
|
39
39
|
|
|
40
40
|
### compatibilityVersion
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
This configuration option is available in Nuxt v3.12+. Please note, that for now, you need to define the compatibility version in each layer that opts into Nuxt 4 behavior. This will not be required after Nuxt 4 is released.
|
|
44
|
-
::
|
|
42
|
+
This is used for enabling early access to Nuxt features or flags.
|
|
45
43
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
Setting `compatibilityVersion` to `4` changes defaults throughout your
|
|
49
|
-
Nuxt configuration to opt-in to Nuxt v4 behaviour, but you can granularly re-enable Nuxt v3 behaviour
|
|
50
|
-
when testing (see example). Please file issues if so, so that we can
|
|
51
|
-
address in Nuxt or in the ecosystem.
|
|
52
|
-
|
|
53
|
-
```ts
|
|
54
|
-
export default defineNuxtConfig({
|
|
55
|
-
future: {
|
|
56
|
-
compatibilityVersion: 4,
|
|
57
|
-
},
|
|
58
|
-
// To re-enable _all_ Nuxt v3 behaviour, set the following options:
|
|
59
|
-
srcDir: '.',
|
|
60
|
-
dir: {
|
|
61
|
-
app: 'app'
|
|
62
|
-
},
|
|
63
|
-
experimental: {
|
|
64
|
-
scanPageMeta: 'after-resolve',
|
|
65
|
-
sharedPrerenderData: false,
|
|
66
|
-
compileTemplate: true,
|
|
67
|
-
resetAsyncDataToUndefined: true,
|
|
68
|
-
templateUtils: true,
|
|
69
|
-
relativeWatchPaths: true,
|
|
70
|
-
normalizeComponentNames: false
|
|
71
|
-
defaults: {
|
|
72
|
-
useAsyncData: {
|
|
73
|
-
deep: true
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
features: {
|
|
78
|
-
inlineStyles: true
|
|
79
|
-
},
|
|
80
|
-
unhead: {
|
|
81
|
-
renderSSRHeadOptions: {
|
|
82
|
-
omitLineBreaks: false
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
})
|
|
86
|
-
```
|
|
44
|
+
It is not configurable yet in Nuxt 4, but once we begin merging breaking changes for v5, it will be possible to enable it.
|
|
87
45
|
|
|
88
46
|
### typescriptBundlerResolution
|
|
89
47
|
|
|
@@ -74,6 +74,25 @@ export default defineNitroPlugin((nitroApp) => {
|
|
|
74
74
|
Learn more about available Nitro lifecycle hooks.
|
|
75
75
|
::
|
|
76
76
|
|
|
77
|
-
##
|
|
77
|
+
## Adding Custom Hooks
|
|
78
78
|
|
|
79
|
-
|
|
79
|
+
You can define your own custom hooks support by extending Nuxt's hook interfaces.
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import { HookResult } from "@nuxt/schema";
|
|
83
|
+
|
|
84
|
+
declare module '#app' {
|
|
85
|
+
interface RuntimeNuxtHooks {
|
|
86
|
+
'your-nuxt-runtime-hook': () => HookResult
|
|
87
|
+
}
|
|
88
|
+
interface NuxtHooks {
|
|
89
|
+
'your-nuxt-hook': () => HookResult
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
declare module 'nitropack/types' {
|
|
94
|
+
interface NitroRuntimeHooks {
|
|
95
|
+
'your-nitro-hook': () => void;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
```
|
|
@@ -559,6 +559,31 @@ export default defineNuxtModule({
|
|
|
559
559
|
```
|
|
560
560
|
::
|
|
561
561
|
|
|
562
|
+
##### Custom Hooks
|
|
563
|
+
|
|
564
|
+
Modules can also define and call their own hooks, which is a powerful pattern for making your module extensible.
|
|
565
|
+
|
|
566
|
+
If you expect other modules to be able to subscribe to your module's hooks, you should call them in the `modules:done` hook. This ensures that all other modules have had a chance to be set up and register their listeners to your hook during their own `setup` function.
|
|
567
|
+
|
|
568
|
+
```ts
|
|
569
|
+
// my-module/module.ts
|
|
570
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
571
|
+
|
|
572
|
+
export interface ModuleHooks {
|
|
573
|
+
'my-module:custom-hook': (payload: { foo: string }) => void
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
export default defineNuxtModule({
|
|
577
|
+
setup (options, nuxt) {
|
|
578
|
+
// Call your hook in `modules:done`
|
|
579
|
+
nuxt.hook('modules:done', async () => {
|
|
580
|
+
const payload = { foo: 'bar' }
|
|
581
|
+
await nuxt.callHook('my-module:custom-hook', payload)
|
|
582
|
+
})
|
|
583
|
+
}
|
|
584
|
+
})
|
|
585
|
+
```
|
|
586
|
+
|
|
562
587
|
#### Adding Templates/Virtual Files
|
|
563
588
|
|
|
564
589
|
If you need to add a virtual file that can be imported into the user's app, you can use the `addTemplate` utility.
|
|
@@ -142,6 +142,10 @@ const { data: users1 } = useAsyncData('users', () => $fetch('/api/users'), { imm
|
|
|
142
142
|
const { data: users2 } = useAsyncData('users', () => $fetch('/api/users'), { immediate: false })
|
|
143
143
|
```
|
|
144
144
|
|
|
145
|
+
::tip
|
|
146
|
+
Keyed state created using `useAsyncData` can be retrieved across your Nuxt application using [`useNuxtData`](/docs/api/composables/use-nuxt-data).
|
|
147
|
+
::
|
|
148
|
+
|
|
145
149
|
## Return Values
|
|
146
150
|
|
|
147
151
|
- `data`: the result of the asynchronous function that is passed in.
|
|
@@ -159,7 +163,7 @@ const { data: users2 } = useAsyncData('users', () => $fetch('/api/users'), { imm
|
|
|
159
163
|
By default, Nuxt waits until a `refresh` is finished before it can be executed again.
|
|
160
164
|
|
|
161
165
|
::note
|
|
162
|
-
If you have not fetched data on the server (for example, with `server: false`), then the data _will not_ be fetched until hydration completes. This means even if you await [`useAsyncData`](/docs/api/composables/use-async-data) on the client side, `data` will remain `
|
|
166
|
+
If you have not fetched data on the server (for example, with `server: false`), then the data _will not_ be fetched until hydration completes. This means even if you await [`useAsyncData`](/docs/api/composables/use-async-data) on the client side, `data` will remain `undefined` within `<script setup>`.
|
|
163
167
|
::
|
|
164
168
|
|
|
165
169
|
## Type
|
|
@@ -170,7 +174,7 @@ function useAsyncData<DataT, DataE>(
|
|
|
170
174
|
options?: AsyncDataOptions<DataT>
|
|
171
175
|
): AsyncData<DataT, DataE>
|
|
172
176
|
function useAsyncData<DataT, DataE>(
|
|
173
|
-
key:
|
|
177
|
+
key: MaybeRefOrGetter<string>,
|
|
174
178
|
handler: (nuxtApp?: NuxtApp) => Promise<DataT>,
|
|
175
179
|
options?: AsyncDataOptions<DataT>
|
|
176
180
|
): Promise<AsyncData<DataT, DataE>>
|
|
@@ -184,7 +188,7 @@ type AsyncDataOptions<DataT> = {
|
|
|
184
188
|
default?: () => DataT | Ref<DataT> | null
|
|
185
189
|
transform?: (input: DataT) => DataT | Promise<DataT>
|
|
186
190
|
pick?: string[]
|
|
187
|
-
watch?:
|
|
191
|
+
watch?: MultiWatchSources | false
|
|
188
192
|
getCachedData?: (key: string, nuxtApp: NuxtApp, ctx: AsyncDataRequestContext) => DataT | undefined
|
|
189
193
|
}
|
|
190
194
|
|
|
@@ -194,11 +198,11 @@ type AsyncDataRequestContext = {
|
|
|
194
198
|
}
|
|
195
199
|
|
|
196
200
|
type AsyncData<DataT, ErrorT> = {
|
|
197
|
-
data: Ref<DataT |
|
|
201
|
+
data: Ref<DataT | undefined>
|
|
198
202
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
|
199
203
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
|
200
204
|
clear: () => void
|
|
201
|
-
error: Ref<ErrorT |
|
|
205
|
+
error: Ref<ErrorT | undefined>
|
|
202
206
|
status: Ref<AsyncDataRequestStatus>
|
|
203
207
|
};
|
|
204
208
|
|
|
@@ -82,6 +82,10 @@ const { data: post } = await useFetch(() => `/api/posts/${id.value}`)
|
|
|
82
82
|
|
|
83
83
|
When using `useFetch` with the same URL and options in multiple components, they will share the same `data`, `error` and `status` refs. This ensures consistency across components.
|
|
84
84
|
|
|
85
|
+
::tip
|
|
86
|
+
Keyed state created using `useFetch` can be retrieved across your Nuxt application using [`useNuxtData`](/docs/api/composables/use-nuxt-data).
|
|
87
|
+
::
|
|
88
|
+
|
|
85
89
|
::warning
|
|
86
90
|
`useFetch` is a reserved function name transformed by the compiler, so you should not name your own function `useFetch`.
|
|
87
91
|
::
|
|
@@ -103,7 +107,7 @@ function useFetch<DataT, ErrorT>(
|
|
|
103
107
|
): Promise<AsyncData<DataT, ErrorT>>
|
|
104
108
|
|
|
105
109
|
type UseFetchOptions<DataT> = {
|
|
106
|
-
key?: string
|
|
110
|
+
key?: MaybeRefOrGetter<string>
|
|
107
111
|
method?: string
|
|
108
112
|
query?: SearchParams
|
|
109
113
|
params?: SearchParams
|
|
@@ -119,7 +123,8 @@ type UseFetchOptions<DataT> = {
|
|
|
119
123
|
default?: () => DataT
|
|
120
124
|
transform?: (input: DataT) => DataT | Promise<DataT>
|
|
121
125
|
pick?: string[]
|
|
122
|
-
|
|
126
|
+
$fetch?: typeof globalThis.$fetch
|
|
127
|
+
watch?: MultiWatchSources | false
|
|
123
128
|
}
|
|
124
129
|
|
|
125
130
|
type AsyncDataRequestContext = {
|
|
@@ -128,11 +133,11 @@ type AsyncDataRequestContext = {
|
|
|
128
133
|
}
|
|
129
134
|
|
|
130
135
|
type AsyncData<DataT, ErrorT> = {
|
|
131
|
-
data: Ref<DataT |
|
|
136
|
+
data: Ref<DataT | undefined>
|
|
132
137
|
refresh: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
|
133
138
|
execute: (opts?: AsyncDataExecuteOptions) => Promise<void>
|
|
134
139
|
clear: () => void
|
|
135
|
-
error: Ref<ErrorT |
|
|
140
|
+
error: Ref<ErrorT | undefined>
|
|
136
141
|
status: Ref<AsyncDataRequestStatus>
|
|
137
142
|
}
|
|
138
143
|
|
|
@@ -151,7 +156,7 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
151
156
|
|
|
152
157
|
| Option | Type | Default | Description |
|
|
153
158
|
| ---| --- | --- | --- |
|
|
154
|
-
| `key` | `string
|
|
159
|
+
| `key` | `MaybeRefOrGetter<string>` | auto-gen | Unique key for de-duplication. If not provided, generated from URL and options. |
|
|
155
160
|
| `method` | `string` | `'GET'` | HTTP request method. |
|
|
156
161
|
| `query` | `object` | - | Query/search params to append to the URL. Alias: `params`. Supports refs/computed. |
|
|
157
162
|
| `params` | `object` | - | Alias for `query`. |
|
|
@@ -167,10 +172,10 @@ type AsyncDataRequestStatus = 'idle' | 'pending' | 'success' | 'error'
|
|
|
167
172
|
| `transform` | `(input: DataT) => DataT \| Promise<DataT>` | - | Function to transform the result after resolving. |
|
|
168
173
|
| `getCachedData`| `(key, nuxtApp, ctx) => DataT \| undefined` | - | Function to return cached data. See below for default. |
|
|
169
174
|
| `pick` | `string[]` | - | Only pick specified keys from the result. |
|
|
170
|
-
| `watch` | `
|
|
175
|
+
| `watch` | `MultiWatchSources \| false` | - | Array of reactive sources to watch and auto-refresh. `false` disables watching. |
|
|
171
176
|
| `deep` | `boolean` | `false` | Return data in a deep ref object. |
|
|
172
177
|
| `dedupe` | `'cancel' \| 'defer'` | `'cancel'` | Avoid fetching same key more than once at a time. |
|
|
173
|
-
| `$fetch` | `typeof
|
|
178
|
+
| `$fetch` | `typeof globalThis.$fetch` | - | Custom $fetch implementation. |
|
|
174
179
|
|
|
175
180
|
::note
|
|
176
181
|
All fetch options can be given a `computed` or `ref` value. These will be watched and new requests made automatically with any new values if they are updated.
|
|
@@ -189,10 +194,10 @@ This only caches data when `experimental.payloadExtraction` in `nuxt.config` is
|
|
|
189
194
|
|
|
190
195
|
| Name | Type | Description |
|
|
191
196
|
| --- | --- |--- |
|
|
192
|
-
| `data` | `Ref<DataT \|
|
|
197
|
+
| `data` | `Ref<DataT \| undefined>` | The result of the asynchronous fetch. |
|
|
193
198
|
| `refresh` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Function to manually refresh the data. By default, Nuxt waits until a `refresh` is finished before it can be executed again. |
|
|
194
199
|
| `execute` | `(opts?: AsyncDataExecuteOptions) => Promise<void>` | Alias for `refresh`. |
|
|
195
|
-
| `error` | `Ref<ErrorT \|
|
|
200
|
+
| `error` | `Ref<ErrorT \| undefined>` | Error object if the data fetching failed. |
|
|
196
201
|
| `status` | `Ref<'idle' \| 'pending' \| 'success' \| 'error'>` | Status of the data request. See below for possible values. |
|
|
197
202
|
| `clear` | `() => void` | Resets `data` to `undefined` (or the value of `options.default()` if provided), `error` to `undefined`, set `status` to `idle`, and cancels any pending requests. |
|
|
198
203
|
|
|
@@ -55,7 +55,7 @@ Variables that need to be accessible on the server are added directly inside `ru
|
|
|
55
55
|
To access runtime config, we can use `useRuntimeConfig()` composable:
|
|
56
56
|
|
|
57
57
|
```ts [server/api/test.ts]
|
|
58
|
-
export default defineEventHandler((event) => {
|
|
58
|
+
export default defineEventHandler(async (event) => {
|
|
59
59
|
const config = useRuntimeConfig(event)
|
|
60
60
|
|
|
61
61
|
// Access public variables
|
package/3.api/4.commands/init.md
CHANGED
|
@@ -38,6 +38,8 @@ Option | Default | Description
|
|
|
38
38
|
`--gitInit` | | Initialize git repository
|
|
39
39
|
`--shell` | | Start shell after installation in project directory
|
|
40
40
|
`--packageManager` | | Package manager choice (npm, pnpm, yarn, bun)
|
|
41
|
+
`--modules` | | Nuxt modules to install (comma separated without spaces)
|
|
42
|
+
`--no-modules` | | Skip module installation prompt
|
|
41
43
|
<!--/init-opts-->
|
|
42
44
|
|
|
43
45
|
## Environment variables
|
package/3.api/5.kit/9.plugins.md
CHANGED
|
@@ -214,51 +214,3 @@ export default defineNuxtPlugin({
|
|
|
214
214
|
```
|
|
215
215
|
|
|
216
216
|
::
|
|
217
|
-
|
|
218
|
-
#### Using an EJS template to generate a plugin
|
|
219
|
-
|
|
220
|
-
You can also use an EJS template to generate your plugin. Options can be passed through the `options` property and then used within the EJS template to generate the plugin content.
|
|
221
|
-
|
|
222
|
-
::code-group
|
|
223
|
-
|
|
224
|
-
```ts [module.ts]
|
|
225
|
-
import { addPluginTemplate, createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
226
|
-
|
|
227
|
-
export default defineNuxtModule({
|
|
228
|
-
setup (options, nuxt) {
|
|
229
|
-
const { resolve } = createResolver(import.meta.url)
|
|
230
|
-
|
|
231
|
-
addPluginTemplate({
|
|
232
|
-
src: resolve('templates/plugin.ejs'),
|
|
233
|
-
filename: 'plugin.mjs',
|
|
234
|
-
options: {
|
|
235
|
-
ssr: nuxt.options.ssr,
|
|
236
|
-
},
|
|
237
|
-
})
|
|
238
|
-
},
|
|
239
|
-
})
|
|
240
|
-
```
|
|
241
|
-
|
|
242
|
-
```ts [templates/plugin.ejs]
|
|
243
|
-
import { VueFire, useSSRInitialState } from 'vuefire'
|
|
244
|
-
import { defineNuxtPlugin } from '#imports'
|
|
245
|
-
|
|
246
|
-
export default defineNuxtPlugin((nuxtApp) => {
|
|
247
|
-
const firebaseApp = nuxtApp.$firebaseApp
|
|
248
|
-
nuxtApp.vueApp.use(VueFire, { firebaseApp })
|
|
249
|
-
|
|
250
|
-
<% if(options.ssr) { %>
|
|
251
|
-
if (import.meta.server) {
|
|
252
|
-
nuxtApp.payload.vuefire = useSSRInitialState(undefined, firebaseApp)
|
|
253
|
-
} else if (nuxtApp.payload?.vuefire) {
|
|
254
|
-
useSSRInitialState(nuxtApp.payload.vuefire, firebaseApp)
|
|
255
|
-
}
|
|
256
|
-
<% } %>
|
|
257
|
-
})
|
|
258
|
-
```
|
|
259
|
-
|
|
260
|
-
::
|
|
261
|
-
|
|
262
|
-
::warning
|
|
263
|
-
If you set `compatibilityVersion` to `4`, Nuxt no longer uses `lodash.template` to compile templates by default. You can still enable it via the `experimental.compileTemplate` option, but support for EJS templates will be removed entirely in the next major version.
|
|
264
|
-
::
|
package/3.api/6.nuxt-config.md
CHANGED
|
@@ -1281,42 +1281,9 @@ If set to 'production' or `true`, JS will be disabled in production mode only.
|
|
|
1281
1281
|
|
|
1282
1282
|
### `compatibilityVersion`
|
|
1283
1283
|
|
|
1284
|
-
|
|
1284
|
+
This is used for enabling early access to Nuxt features or flags.
|
|
1285
1285
|
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
- **Type**: `number`
|
|
1289
|
-
- **Default:** `3`
|
|
1290
|
-
|
|
1291
|
-
**Example**:
|
|
1292
|
-
```ts
|
|
1293
|
-
export default defineNuxtConfig({
|
|
1294
|
-
future: {
|
|
1295
|
-
compatibilityVersion: 4,
|
|
1296
|
-
},
|
|
1297
|
-
// To re-enable _all_ Nuxt v3 behaviour, set the following options:
|
|
1298
|
-
srcDir: '.',
|
|
1299
|
-
dir: {
|
|
1300
|
-
app: 'app'
|
|
1301
|
-
},
|
|
1302
|
-
experimental: {
|
|
1303
|
-
compileTemplate: true,
|
|
1304
|
-
templateUtils: true,
|
|
1305
|
-
relativeWatchPaths: true,
|
|
1306
|
-
resetAsyncDataToUndefined: true,
|
|
1307
|
-
defaults: {
|
|
1308
|
-
useAsyncData: {
|
|
1309
|
-
deep: true
|
|
1310
|
-
}
|
|
1311
|
-
}
|
|
1312
|
-
},
|
|
1313
|
-
unhead: {
|
|
1314
|
-
renderSSRHeadOptions: {
|
|
1315
|
-
omitLineBreaks: false
|
|
1316
|
-
}
|
|
1317
|
-
}
|
|
1318
|
-
})
|
|
1319
|
-
```
|
|
1286
|
+
It is not configurable yet in Nuxt 4, but once we begin merging breaking changes for v5, it will be possible to enable it.
|
|
1320
1287
|
|
|
1321
1288
|
### `multiApp`
|
|
1322
1289
|
|