@nuxt/docs 4.2.2 → 4.3.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 +2 -5
- package/1.getting-started/03.configuration.md +5 -5
- package/1.getting-started/06.styling.md +5 -1
- package/1.getting-started/07.routing.md +1 -1
- package/1.getting-started/08.seo-meta.md +4 -0
- package/1.getting-started/12.error-handling.md +12 -6
- package/1.getting-started/14.layers.md +47 -13
- package/1.getting-started/15.prerendering.md +6 -0
- package/1.getting-started/16.deployment.md +1 -0
- package/1.getting-started/17.testing.md +12 -2
- package/1.getting-started/18.upgrade.md +14 -1
- package/2.directory-structure/1.app/1.components.md +4 -0
- package/2.directory-structure/1.app/1.layouts.md +32 -0
- package/2.directory-structure/1.app/1.pages.md +21 -0
- package/2.directory-structure/1.app/1.plugins.md +3 -0
- package/2.directory-structure/1.app/3.error.md +9 -5
- package/2.directory-structure/1.layers.md +87 -0
- package/2.directory-structure/1.modules.md +12 -2
- package/2.directory-structure/1.node_modules.md +1 -1
- package/2.directory-structure/1.server.md +24 -4
- package/2.directory-structure/2.nuxtrc.md +3 -0
- package/2.directory-structure/3.tsconfig.md +1 -0
- package/2.directory-structure/index.md +4 -0
- package/3.guide/0.index.md +8 -2
- package/3.guide/1.concepts/1.rendering.md +1 -0
- package/3.guide/1.concepts/2.nuxt-lifecycle.md +1 -1
- package/3.guide/1.concepts/5.modules.md +14 -1
- package/3.guide/1.concepts/8.typescript.md +6 -2
- package/3.guide/3.ai/1.mcp.md +22 -0
- package/3.guide/4.modules/.navigation.yml +3 -0
- package/3.guide/4.modules/1.getting-started.md +103 -0
- package/3.guide/4.modules/2.module-anatomy.md +138 -0
- package/3.guide/4.modules/3.recipes-basics.md +330 -0
- package/3.guide/4.modules/4.recipes-advanced.md +243 -0
- package/3.guide/4.modules/5.testing.md +76 -0
- package/3.guide/4.modules/6.best-practices.md +104 -0
- package/3.guide/4.modules/7.ecosystem.md +32 -0
- package/3.guide/4.modules/index.md +36 -0
- package/3.guide/{4.recipes → 5.recipes}/2.vite-plugin.md +4 -0
- package/3.guide/{4.recipes → 5.recipes}/3.custom-usefetch.md +1 -1
- package/3.guide/{4.recipes → 5.recipes}/4.sessions-and-authentication.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/1.experimental-features.md +15 -0
- package/3.guide/{5.going-further → 6.going-further}/1.internals.md +3 -3
- package/3.guide/{5.going-further → 6.going-further}/10.runtime-config.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/2.hooks.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/4.kit.md +1 -1
- package/3.guide/{5.going-further → 6.going-further}/7.layers.md +26 -0
- package/4.api/1.components/1.nuxt-client-fallback.md +4 -0
- package/4.api/1.components/3.nuxt-layout.md +2 -2
- package/4.api/1.components/8.nuxt-island.md +9 -2
- package/4.api/2.composables/use-async-data.md +3 -3
- package/4.api/2.composables/use-error.md +2 -2
- package/4.api/2.composables/use-fetch.md +2 -2
- package/4.api/2.composables/use-head.md +16 -1
- package/4.api/2.composables/use-nuxt-app.md +2 -0
- package/4.api/2.composables/use-state.md +10 -0
- package/4.api/3.utils/create-error.md +6 -6
- package/4.api/3.utils/define-nuxt-route-middleware.md +1 -1
- package/4.api/3.utils/define-page-meta.md +8 -1
- package/4.api/3.utils/set-page-layout.md +36 -0
- package/4.api/3.utils/set-response-status.md +2 -2
- package/4.api/3.utils/show-error.md +3 -3
- package/4.api/4.commands/add.md +1 -1
- package/4.api/5.kit/1.modules.md +15 -2
- package/4.api/5.kit/14.builder.md +14 -2
- package/4.api/6.nuxt-config.md +2 -2
- package/5.community/4.contribution.md +5 -5
- package/5.community/5.framework-contribution.md +1 -1
- package/5.community/6.roadmap.md +1 -1
- package/5.community/7.changelog.md +10 -0
- package/6.bridge/1.overview.md +8 -0
- package/6.bridge/4.plugins-and-middleware.md +1 -1
- package/6.bridge/8.nitro.md +4 -0
- package/7.migration/2.configuration.md +2 -2
- package/7.migration/20.module-authors.md +2 -2
- package/7.migration/7.component-options.md +1 -1
- package/package.json +1 -1
- package/3.guide/5.going-further/3.modules.md +0 -968
- /package/3.guide/{4.recipes → 5.recipes}/.navigation.yml +0 -0
- /package/3.guide/{4.recipes → 5.recipes}/1.custom-routing.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/.navigation.yml +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/1.events.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/1.features.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/11.nightly-release-channel.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/6.nuxt-app.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/9.debugging.md +0 -0
- /package/3.guide/{5.going-further → 6.going-further}/index.md +0 -0
|
@@ -17,9 +17,7 @@ Or follow the steps below to set up a new Nuxt project on your computer.
|
|
|
17
17
|
|
|
18
18
|
## New Project
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
<!-- markdownlint-disable-next-line MD001 -->
|
|
22
|
-
#### Prerequisites
|
|
20
|
+
### Prerequisites
|
|
23
21
|
|
|
24
22
|
- **Node.js** - [`20.x`](https://nodejs.org/en) or newer (but we recommend the [active LTS release](https://github.com/nodejs/release#release-schedule))
|
|
25
23
|
- **Text editor** - There is no IDE requirement, but we recommend [Visual Studio Code](https://code.visualstudio.com/) with the [official Vue extension](https://marketplace.visualstudio.com/items?itemName=Vue.volar) (previously known as Volar) or [WebStorm](https://www.jetbrains.com/webstorm/), which, along with [other JetBrains IDEs](https://www.jetbrains.com/ides/), offers great Nuxt support right out-of-the-box.
|
|
@@ -29,7 +27,6 @@ Or follow the steps below to set up a new Nuxt project on your computer.
|
|
|
29
27
|
::details
|
|
30
28
|
:summary[Additional notes for an optimal setup:]
|
|
31
29
|
- **Node.js**: Make sure to use an even numbered version (20, 22, etc.)
|
|
32
|
-
- **Nuxtr**: Install the community-developed [Nuxtr extension](https://marketplace.visualstudio.com/items?itemName=Nuxtr.nuxtr-vscode)
|
|
33
30
|
- **WSL**: If you are using Windows and experience slow HMR, you may want to try using [WSL (Windows Subsystem for Linux)](https://learn.microsoft.com/en-us/windows/wsl/install) which may solve some performance issues.
|
|
34
31
|
- **Windows slow DNS resolution**: Instead of using `localhost:3000` for local dev server on Windows, use `127.0.0.1` for much faster loading experience on browsers.
|
|
35
32
|
::
|
|
@@ -44,7 +41,7 @@ npm create nuxt@latest <project-name>
|
|
|
44
41
|
```
|
|
45
42
|
|
|
46
43
|
```bash [yarn]
|
|
47
|
-
yarn create nuxt
|
|
44
|
+
yarn create nuxt <project-name>
|
|
48
45
|
```
|
|
49
46
|
|
|
50
47
|
```bash [pnpm]
|
|
@@ -135,13 +135,13 @@ As stated above, `runtimeConfig` and `app.config` are both used to expose variab
|
|
|
135
135
|
|
|
136
136
|
| Feature | `runtimeConfig` | `app.config` |
|
|
137
137
|
|---------------------------|-----------------|--------------|
|
|
138
|
-
| Client
|
|
139
|
-
| Environment
|
|
138
|
+
| Client-side | Hydrated | Bundled |
|
|
139
|
+
| Environment variables | ✅ Yes | ❌ No |
|
|
140
140
|
| Reactive | ✅ Yes | ✅ Yes |
|
|
141
141
|
| Types support | ✅ Partial | ✅ Yes |
|
|
142
|
-
| Configuration per
|
|
143
|
-
| Hot
|
|
144
|
-
| Non
|
|
142
|
+
| Configuration per request | ❌ No | ✅ Yes |
|
|
143
|
+
| Hot module replacement | ❌ No | ✅ Yes |
|
|
144
|
+
| Non-primitive JS types | ❌ No | ✅ Yes |
|
|
145
145
|
|
|
146
146
|
## External Configuration Files
|
|
147
147
|
|
|
@@ -95,6 +95,10 @@ pnpm install animate.css
|
|
|
95
95
|
bun install animate.css
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
+
```bash [deno]
|
|
99
|
+
deno install npm:animate.css
|
|
100
|
+
```
|
|
101
|
+
|
|
98
102
|
::
|
|
99
103
|
|
|
100
104
|
Then you can reference it directly in your pages, layouts and components:
|
|
@@ -517,7 +521,7 @@ Here are a few modules to help you get started:
|
|
|
517
521
|
- [Nuxt UI](https://ui.nuxt.com): A UI Library for Modern Web Apps
|
|
518
522
|
- [Panda CSS](https://panda-css.com/docs/installation/nuxt): CSS-in-JS engine that generates atomic CSS at build time
|
|
519
523
|
|
|
520
|
-
Nuxt modules provide you with a good developer experience out of the box, but remember that if your favorite tool doesn't have a module, it doesn't mean that you can't use it with Nuxt! You can configure it yourself for your own project. Depending on the tool, you might need to use a [Nuxt plugin](/docs/4.x/directory-structure/app/plugins) and/or [make your own module](/docs/4.x/guide/
|
|
524
|
+
Nuxt modules provide you with a good developer experience out of the box, but remember that if your favorite tool doesn't have a module, it doesn't mean that you can't use it with Nuxt! You can configure it yourself for your own project. Depending on the tool, you might need to use a [Nuxt plugin](/docs/4.x/directory-structure/app/plugins) and/or [make your own module](/docs/4.x/guide/modules). Share them with the [community](/modules) if you do!
|
|
521
525
|
|
|
522
526
|
### Easily Load Webfonts
|
|
523
527
|
|
|
@@ -135,7 +135,7 @@ definePageMeta({
|
|
|
135
135
|
|
|
136
136
|
Nuxt offers route validation via the `validate` property in [`definePageMeta()`](/docs/4.x/api/utils/define-page-meta) in each page you wish to validate.
|
|
137
137
|
|
|
138
|
-
The `validate` property accepts the `route` as an argument. You can return a boolean value to determine whether or not this is a valid route to be rendered with this page. If you return `false`, this will cause a 404 error. You can also directly return an object with `
|
|
138
|
+
The `validate` property accepts the `route` as an argument. You can return a boolean value to determine whether or not this is a valid route to be rendered with this page. If you return `false`, this will cause a 404 error. You can also directly return an object with `status`/`statusText` to customize the error returned.
|
|
139
139
|
|
|
140
140
|
If you have a more complex use case, then you can use anonymous route middleware instead.
|
|
141
141
|
|
|
@@ -134,6 +134,10 @@ const title = ref('Hello World')
|
|
|
134
134
|
|
|
135
135
|
It's suggested to wrap your components in either a `<Head>` or `<Html>` components as tags will be deduped more intuitively.
|
|
136
136
|
|
|
137
|
+
::warning
|
|
138
|
+
If you need to duplicate tags across client-server boundaries, apply a `key` attribute on the `<Head>` component.
|
|
139
|
+
::
|
|
140
|
+
|
|
137
141
|
## Types
|
|
138
142
|
|
|
139
143
|
Below are the non-reactive types used for [`useHead`](/docs/4.x/api/composables/use-head), [`app.head`](/docs/4.x/api/nuxt-config#head) and components.
|
|
@@ -99,7 +99,7 @@ const handleError = () => clearError({ redirect: '/' })
|
|
|
99
99
|
|
|
100
100
|
<template>
|
|
101
101
|
<div>
|
|
102
|
-
<h2>{{ error?.
|
|
102
|
+
<h2>{{ error?.status }}</h2>
|
|
103
103
|
<button @click="handleError">
|
|
104
104
|
Clear errors
|
|
105
105
|
</button>
|
|
@@ -140,7 +140,7 @@ If you are running on Node 16 and you set any cookies when rendering your error
|
|
|
140
140
|
### `useError`
|
|
141
141
|
|
|
142
142
|
```ts [TS Signature]
|
|
143
|
-
function useError (): Ref<Error | { url,
|
|
143
|
+
function useError (): Ref<Error | { url, status, statusText, message, description, data }>
|
|
144
144
|
```
|
|
145
145
|
|
|
146
146
|
This function will return the global Nuxt error that is being handled.
|
|
@@ -152,7 +152,7 @@ Read more about `useError` composable.
|
|
|
152
152
|
### `createError`
|
|
153
153
|
|
|
154
154
|
```ts [TS Signature]
|
|
155
|
-
function createError (err: string | { cause, data, message, name, stack,
|
|
155
|
+
function createError (err: string | { cause, data, message, name, stack, status, statusText, fatal }): Error
|
|
156
156
|
```
|
|
157
157
|
|
|
158
158
|
Create an error object with additional metadata. You can pass a string to be set as the error `message` or an object containing error properties. It is usable in both the Vue and Server portions of your app, and is meant to be thrown.
|
|
@@ -168,13 +168,19 @@ const { data } = await useFetch(`/api/movies/${route.params.slug}`)
|
|
|
168
168
|
|
|
169
169
|
if (!data.value) {
|
|
170
170
|
throw createError({
|
|
171
|
-
|
|
172
|
-
|
|
171
|
+
status: 404,
|
|
172
|
+
statusText: 'Page Not Found',
|
|
173
173
|
})
|
|
174
174
|
}
|
|
175
175
|
</script>
|
|
176
176
|
```
|
|
177
177
|
|
|
178
|
+
::tip
|
|
179
|
+
The `statusText` property is intended for short, HTTP-compliant status texts (e.g., "Not Found"). It should only contain horizontal tabs, spaces, and visible ASCII characters (`[\t\u0020-\u007E]`).
|
|
180
|
+
|
|
181
|
+
For any detailed descriptions, multi-line messages, or content with non-ASCII characters, you should always use the `message` property instead.
|
|
182
|
+
::
|
|
183
|
+
|
|
178
184
|
::read-more{to="/docs/4.x/api/utils/create-error"}
|
|
179
185
|
Read more about `createError` util.
|
|
180
186
|
::
|
|
@@ -182,7 +188,7 @@ Read more about `createError` util.
|
|
|
182
188
|
### `showError`
|
|
183
189
|
|
|
184
190
|
```ts [TS Signature]
|
|
185
|
-
function showError (err: string | Error | {
|
|
191
|
+
function showError (err: string | Error | { status, statusText }): Error
|
|
186
192
|
```
|
|
187
193
|
|
|
188
194
|
You can call this function at any point on client-side, or (on server side) directly within middleware, plugins or `setup()` functions. It will trigger a full-screen error page which you can clear with [`clearError`](/docs/4.x/getting-started/error-handling#clearerror).
|
|
@@ -56,6 +56,10 @@ export default defineNuxtConfig({
|
|
|
56
56
|
})
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
+
::note
|
|
60
|
+
If a branch is not specified, this will clone `main`.
|
|
61
|
+
::
|
|
62
|
+
|
|
59
63
|
::tip
|
|
60
64
|
You can override a layer's alias by specifying it in the options next to the layer source.
|
|
61
65
|
|
|
@@ -82,32 +86,60 @@ Nuxt uses [unjs/c12](https://github.com/unjs/c12) and [unjs/giget](https://githu
|
|
|
82
86
|
|
|
83
87
|
When using multiple layers, it's important to understand the override order. Layers with **higher priority** override layers with lower priority when they define the same files or components.
|
|
84
88
|
|
|
85
|
-
|
|
89
|
+
### Priority Order
|
|
90
|
+
|
|
91
|
+
From highest to lowest priority:
|
|
86
92
|
|
|
87
93
|
1. **Your project files** - always have the highest priority
|
|
88
94
|
2. **Auto-scanned layers** from `~~/layers` directory - sorted alphabetically (Z has higher priority than A)
|
|
89
95
|
3. **Layers in `extends`** config - first entry has higher priority than second
|
|
90
96
|
|
|
91
|
-
###
|
|
97
|
+
### Practical Example
|
|
92
98
|
|
|
93
|
-
|
|
94
|
-
|
|
99
|
+
Consider multiple layers defining the same component:
|
|
100
|
+
|
|
101
|
+
```bash [Directory structure]
|
|
102
|
+
layers/
|
|
103
|
+
1.base/
|
|
104
|
+
app/components/Button.vue # Base button style
|
|
105
|
+
2.theme/
|
|
106
|
+
app/components/Button.vue # Themed button (overrides base)
|
|
107
|
+
app/
|
|
108
|
+
components/Button.vue # Project button (overrides all layers)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
In this case:
|
|
112
|
+
- If only layers exist, `2.theme/Button.vue` is used (higher alphabetically)
|
|
113
|
+
- If `app/components/Button.vue` exists in your project, it overrides all layers
|
|
114
|
+
|
|
115
|
+
### Controlling Priority
|
|
116
|
+
|
|
117
|
+
You can prefix layer directories with numbers to control the order:
|
|
118
|
+
|
|
119
|
+
```bash [Directory structure]
|
|
120
|
+
layers/
|
|
121
|
+
1.base/ # Lowest priority
|
|
122
|
+
2.features/ # Medium priority
|
|
123
|
+
3.admin/ # Highest priority (among layers)
|
|
124
|
+
```
|
|
95
125
|
|
|
96
126
|
::tip
|
|
97
|
-
|
|
127
|
+
This pattern is useful for creating base layers with defaults that can be progressively overridden by more specific layers.
|
|
98
128
|
::
|
|
99
129
|
|
|
100
|
-
###
|
|
130
|
+
### When to Use Each
|
|
131
|
+
|
|
132
|
+
- **`~~/layers` directory** - Use for local layers that are part of your project
|
|
133
|
+
- **`extends`** - Use for external dependencies (npm packages, remote repositories) or layers outside your project directory
|
|
134
|
+
|
|
135
|
+
### Full Example with extends
|
|
101
136
|
|
|
102
137
|
```ts [nuxt.config.ts]
|
|
103
138
|
export default defineNuxtConfig({
|
|
104
139
|
extends: [
|
|
105
|
-
// Local layer outside
|
|
106
|
-
'
|
|
107
|
-
//
|
|
108
|
-
'@my-themes/awesome',
|
|
109
|
-
// Remote repository
|
|
110
|
-
'github:my-themes/awesome#v1',
|
|
140
|
+
'../base', // Local layer outside project
|
|
141
|
+
'@my-themes/awesome', // NPM package
|
|
142
|
+
'github:my-themes/awesome#v1', // Remote repository
|
|
111
143
|
],
|
|
112
144
|
})
|
|
113
145
|
```
|
|
@@ -119,7 +151,9 @@ If you also have `~~/layers/custom`, the priority order is:
|
|
|
119
151
|
- `@my-themes/awesome`
|
|
120
152
|
- `github:my-themes/awesome#v1` (lowest)
|
|
121
153
|
|
|
122
|
-
|
|
154
|
+
::read-more{to="/docs/4.x/directory-structure/layers"}
|
|
155
|
+
Learn about the **layers/ directory** to organize and share reusable code, components, composables, and configurations across your Nuxt application.
|
|
156
|
+
::
|
|
123
157
|
|
|
124
158
|
::read-more{to="/docs/4.x/guide/going-further/layers"}
|
|
125
159
|
Read more about layers in the **Layer Author Guide**.
|
|
@@ -32,6 +32,10 @@ pnpm nuxt generate
|
|
|
32
32
|
bun x nuxt generate
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
+
```bash [deno]
|
|
36
|
+
deno x nuxt generate
|
|
37
|
+
```
|
|
38
|
+
|
|
35
39
|
::
|
|
36
40
|
|
|
37
41
|
You can now deploy the `.output/public` directory to any static hosting service or preview it locally with `npx serve .output/public`.
|
|
@@ -54,6 +58,7 @@ Read more about the `nuxt generate` command.
|
|
|
54
58
|
You can manually specify routes that [Nitro](/docs/4.x/guide/concepts/server-engine) will fetch and pre-render during the build or ignore routes that you don't want to pre-render like `/dynamic` in the `nuxt.config` file:
|
|
55
59
|
|
|
56
60
|
```ts twoslash [nuxt.config.ts]
|
|
61
|
+
// @errors: 2353
|
|
57
62
|
export default defineNuxtConfig({
|
|
58
63
|
nitro: {
|
|
59
64
|
prerender: {
|
|
@@ -67,6 +72,7 @@ export default defineNuxtConfig({
|
|
|
67
72
|
You can combine this with the `crawlLinks` option to pre-render a set of routes that the crawler can't discover like your `/sitemap.xml` or `/robots.txt`:
|
|
68
73
|
|
|
69
74
|
```ts twoslash [nuxt.config.ts]
|
|
75
|
+
// @errors: 2353
|
|
70
76
|
export default defineNuxtConfig({
|
|
71
77
|
nitro: {
|
|
72
78
|
prerender: {
|
|
@@ -98,6 +98,7 @@ In addition to Node.js servers and static hosting services, a Nuxt project can b
|
|
|
98
98
|
You can explicitly set the desired preset in the [`nuxt.config.ts`](/docs/4.x/directory-structure/nuxt-config) file:
|
|
99
99
|
|
|
100
100
|
```ts twoslash [nuxt.config.ts]
|
|
101
|
+
// @errors: 2353
|
|
101
102
|
export default defineNuxtConfig({
|
|
102
103
|
nitro: {
|
|
103
104
|
preset: 'node-server',
|
|
@@ -5,7 +5,7 @@ navigation.icon: i-lucide-circle-check
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
::tip
|
|
8
|
-
If you are a module author, you can find more specific information in the [Module Author's guide](/docs/4.x/guide/
|
|
8
|
+
If you are a module author, you can find more specific information in the [Module Author's guide](/docs/4.x/guide/modules/testing).
|
|
9
9
|
::
|
|
10
10
|
|
|
11
11
|
Nuxt offers first-class support for end-to-end and unit testing of your Nuxt application via `@nuxt/test-utils`, a library of test utilities and configuration that currently powers the [tests we use on Nuxt itself](https://github.com/nuxt/nuxt/tree/main/test) and tests throughout the module ecosystem.
|
|
@@ -63,7 +63,14 @@ We currently ship an environment for unit testing code that needs a [Nuxt](https
|
|
|
63
63
|
{
|
|
64
64
|
test: {
|
|
65
65
|
name: 'unit',
|
|
66
|
-
include: ['test/
|
|
66
|
+
include: ['test/unit/*.{test,spec}.ts'],
|
|
67
|
+
environment: 'node',
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
test: {
|
|
72
|
+
name: 'e2e',
|
|
73
|
+
include: ['test/e2e/*.{test,spec}.ts'],
|
|
67
74
|
environment: 'node',
|
|
68
75
|
},
|
|
69
76
|
},
|
|
@@ -761,6 +768,9 @@ pnpm add -D @playwright/test @nuxt/test-utils
|
|
|
761
768
|
```bash [bun]
|
|
762
769
|
bun add --dev @playwright/test @nuxt/test-utils
|
|
763
770
|
```
|
|
771
|
+
```bash [deno]
|
|
772
|
+
deno add --dev npm:@playwright/test npm:@nuxt/test-utils
|
|
773
|
+
```
|
|
764
774
|
::
|
|
765
775
|
|
|
766
776
|
You can provide global Nuxt configuration, with the same configuration details as the `setup()` function mentioned earlier in this section.
|
|
@@ -28,6 +28,10 @@ pnpm nuxt upgrade
|
|
|
28
28
|
bun x nuxt upgrade
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
+
```bash [deno]
|
|
32
|
+
deno x nuxt upgrade
|
|
33
|
+
```
|
|
34
|
+
|
|
31
35
|
::
|
|
32
36
|
|
|
33
37
|
### Nightly Release Channel
|
|
@@ -198,6 +202,10 @@ pnpm add nuxt@^4.0.0
|
|
|
198
202
|
bun add nuxt@^4.0.0
|
|
199
203
|
```
|
|
200
204
|
|
|
205
|
+
```bash [deno]
|
|
206
|
+
deno add npm:nuxt@^4.0.0
|
|
207
|
+
```
|
|
208
|
+
|
|
201
209
|
::
|
|
202
210
|
|
|
203
211
|
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.
|
|
@@ -240,6 +248,11 @@ pnpm dlx codemod@0.18.7 nuxt/4/migration-recipe
|
|
|
240
248
|
bun x codemod@0.18.7 nuxt/4/migration-recipe
|
|
241
249
|
```
|
|
242
250
|
|
|
251
|
+
```bash [deno]
|
|
252
|
+
# Using pinned version due to https://github.com/codemod/codemod/issues/1710
|
|
253
|
+
deno x codemod@0.18.7 nuxt/4/migration-recipe
|
|
254
|
+
```
|
|
255
|
+
|
|
243
256
|
::
|
|
244
257
|
|
|
245
258
|
This command will execute all codemods in sequence, with the option to deselect any that you do not wish to run. Each codemod is also listed below alongside its respective change and can be executed independently.
|
|
@@ -463,7 +476,7 @@ export default defineNuxtConfig({
|
|
|
463
476
|
// 4. project-module-2 (can override layer modules)
|
|
464
477
|
```
|
|
465
478
|
|
|
466
|
-
If you encounter issues with module order dependencies due to needing to register a hook, consider using the [`modules:done` hook](/docs/4.x/guide/
|
|
479
|
+
If you encounter issues with module order dependencies due to needing to register a hook, consider using the [`modules:done` hook](/docs/4.x/guide/modules/recipes-advanced) 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.
|
|
467
480
|
|
|
468
481
|
👉 See [PR #31507](https://github.com/nuxt/nuxt/pull/31507) and [issue #25719](https://github.com/nuxt/nuxt/issues/25719) for more details.
|
|
469
482
|
|
|
@@ -535,6 +535,10 @@ This means:
|
|
|
535
535
|
* You can't access the 'island context' from the rest of your app and you can't access the context of the rest of your app from the island component. In other words, the server component or island is _isolated_ from the rest of your app.
|
|
536
536
|
* Your plugins will run again when rendering the island, unless they have `env: { islands: false }` set (which you can do in an object-syntax plugin).
|
|
537
537
|
|
|
538
|
+
::important
|
|
539
|
+
Route middleware does not run when rendering island components. Middleware is a routing concept that applies to pages, not components, and is not designed to control component rendering.
|
|
540
|
+
::
|
|
541
|
+
|
|
538
542
|
Within an island component, you can access its island context through `nuxtApp.ssrContext.islandContext`. Note that while island components are still marked as experimental, the format of this context may change.
|
|
539
543
|
|
|
540
544
|
::note
|
|
@@ -24,6 +24,7 @@ Layouts are enabled by adding [`<NuxtLayout>`](/docs/4.x/api/components/nuxt-lay
|
|
|
24
24
|
To use a layout:
|
|
25
25
|
- Set a `layout` property in your page with [definePageMeta](/docs/4.x/api/utils/define-page-meta).
|
|
26
26
|
- Set the `name` prop of `<NuxtLayout>`.
|
|
27
|
+
- Set the `appLayout` property in route rules.
|
|
27
28
|
|
|
28
29
|
::note
|
|
29
30
|
The layout name is normalized to kebab-case, so `someLayout` becomes `some-layout`.
|
|
@@ -68,6 +69,12 @@ Then you can use the `custom` layout in your page:
|
|
|
68
69
|
|
|
69
70
|
```vue twoslash [pages/about.vue]
|
|
70
71
|
<script setup lang="ts">
|
|
72
|
+
declare module 'nuxt/app' {
|
|
73
|
+
interface NuxtLayouts {
|
|
74
|
+
'custom': unknown
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
// ---cut---
|
|
71
78
|
definePageMeta({
|
|
72
79
|
layout: 'custom',
|
|
73
80
|
})
|
|
@@ -117,6 +124,12 @@ You can also use the [`setPageLayout`](/docs/4.x/api/utils/set-page-layout) help
|
|
|
117
124
|
|
|
118
125
|
```vue twoslash
|
|
119
126
|
<script setup lang="ts">
|
|
127
|
+
declare module 'nuxt/app' {
|
|
128
|
+
interface NuxtLayouts {
|
|
129
|
+
'custom': unknown
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// ---cut---
|
|
120
133
|
function enableCustomLayout () {
|
|
121
134
|
setPageLayout('custom')
|
|
122
135
|
}
|
|
@@ -134,6 +147,25 @@ definePageMeta({
|
|
|
134
147
|
</template>
|
|
135
148
|
```
|
|
136
149
|
|
|
150
|
+
You can also set layouts for specific routes using the `appLayout` property in route rules:
|
|
151
|
+
|
|
152
|
+
```ts [nuxt.config.ts]
|
|
153
|
+
export default defineNuxtConfig({
|
|
154
|
+
routeRules: {
|
|
155
|
+
// Set layout for specific route
|
|
156
|
+
'/admin': { appLayout: 'admin' },
|
|
157
|
+
// Set layout for multiple routes
|
|
158
|
+
'/dashboard/**': { appLayout: 'dashboard' },
|
|
159
|
+
// Disable layout for a route
|
|
160
|
+
'/landing': { appLayout: false },
|
|
161
|
+
},
|
|
162
|
+
})
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
::tip
|
|
166
|
+
This is useful when you want to manage layouts centrally in your configuration rather than in each page file, or when you need to apply layouts to routes that don't have corresponding page components (such as catchall pages which might match many paths).
|
|
167
|
+
::
|
|
168
|
+
|
|
137
169
|
:link-example{to="/docs/4.x/examples/features/layouts"}
|
|
138
170
|
|
|
139
171
|
## Overriding a Layout on a Per-page Basis
|
|
@@ -246,6 +246,27 @@ For example:
|
|
|
246
246
|
|
|
247
247
|
This will produce `/`, `/about` and `/contact` pages in your app. The `marketing` group is ignored for purposes of your URL structure.
|
|
248
248
|
|
|
249
|
+
### Accessing Route Groups
|
|
250
|
+
|
|
251
|
+
Route groups are automatically available in the route metadata as `route.meta.groups`.
|
|
252
|
+
This allows you to access the group information in your components for conditional logic, styling, or other purposes.
|
|
253
|
+
|
|
254
|
+
```vue {}[pages/(marketing)/about.vue]
|
|
255
|
+
<script setup lang="ts">
|
|
256
|
+
const route = useRoute()
|
|
257
|
+
|
|
258
|
+
console.log(route.meta.groups) // Output: ['marketing']
|
|
259
|
+
</script>
|
|
260
|
+
|
|
261
|
+
<template>
|
|
262
|
+
<div>
|
|
263
|
+
<p v-if="route.meta.groups?.includes('marketing')">
|
|
264
|
+
This is a marketing page
|
|
265
|
+
</p>
|
|
266
|
+
</div>
|
|
267
|
+
</template>
|
|
268
|
+
```
|
|
269
|
+
|
|
249
270
|
## Page Metadata
|
|
250
271
|
|
|
251
272
|
You might want to define metadata for each route in your app. You can do this using the `definePageMeta` macro, which will work both in `<script>` and in `<script setup>`:
|
|
@@ -16,7 +16,7 @@ const props = defineProps<{ error: NuxtError }>()
|
|
|
16
16
|
|
|
17
17
|
<template>
|
|
18
18
|
<div>
|
|
19
|
-
<h1>{{ error.
|
|
19
|
+
<h1>{{ error.status }}</h1>
|
|
20
20
|
<NuxtLink to="/">Go back home</NuxtLink>
|
|
21
21
|
</div>
|
|
22
22
|
</template>
|
|
@@ -31,12 +31,16 @@ The error page has a single prop - `error` which contains an error for you to ha
|
|
|
31
31
|
The `error` object provides the following fields:
|
|
32
32
|
```ts
|
|
33
33
|
interface NuxtError {
|
|
34
|
-
|
|
34
|
+
status: number
|
|
35
35
|
fatal: boolean
|
|
36
36
|
unhandled: boolean
|
|
37
|
-
|
|
37
|
+
statusText?: string
|
|
38
38
|
data?: unknown
|
|
39
39
|
cause?: unknown
|
|
40
|
+
// legacy/deprecated equivalent of `status`
|
|
41
|
+
statusCode: number
|
|
42
|
+
// legacy/deprecated equivalent of `statusText`
|
|
43
|
+
statusMessage?: string
|
|
40
44
|
}
|
|
41
45
|
```
|
|
42
46
|
|
|
@@ -44,8 +48,8 @@ If you have an error with custom fields they will be lost; you should assign the
|
|
|
44
48
|
|
|
45
49
|
```ts
|
|
46
50
|
throw createError({
|
|
47
|
-
|
|
48
|
-
|
|
51
|
+
status: 404,
|
|
52
|
+
statusText: 'Page Not Found',
|
|
49
53
|
data: {
|
|
50
54
|
myCustomField: true,
|
|
51
55
|
},
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'layers'
|
|
3
|
+
head.title: 'layers/'
|
|
4
|
+
description: Use the layers/ directory to organize and auto-register local layers within your application.
|
|
5
|
+
navigation.icon: i-vscode-icons-folder-type-nuxt
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
The `layers/` directory allows you to organize and share reusable code, components, composables, and configurations across your Nuxt application. Any layers within your project in the `layers/` directory will be automatically registered.
|
|
9
|
+
|
|
10
|
+
::note
|
|
11
|
+
The `layers/` directory auto-registration is available in Nuxt v3.12.0+.
|
|
12
|
+
::
|
|
13
|
+
|
|
14
|
+
::tip{icon="i-lucide-lightbulb"}
|
|
15
|
+
Layers are ideal for organizing large codebases with **Domain-Driven Design (DDD)**, creating reusable **UI libraries** or **themes**, sharing **configuration presets** across projects, and separating concerns like **admin panels** or **feature modules**.
|
|
16
|
+
::
|
|
17
|
+
|
|
18
|
+
## Structure
|
|
19
|
+
|
|
20
|
+
Each subdirectory within `layers/` is treated as a separate layer. A layer can contain the same structure as a standard Nuxt application.
|
|
21
|
+
|
|
22
|
+
::important
|
|
23
|
+
Every layer **must have** a `nuxt.config.ts` file to be recognized as a valid layer, even if it's empty.
|
|
24
|
+
::
|
|
25
|
+
|
|
26
|
+
```bash [Directory structure]
|
|
27
|
+
-| layers/
|
|
28
|
+
---| base/
|
|
29
|
+
-----| nuxt.config.ts
|
|
30
|
+
-----| app/
|
|
31
|
+
-------| components/
|
|
32
|
+
---------| BaseButton.vue
|
|
33
|
+
-------| composables/
|
|
34
|
+
---------| useBase.ts
|
|
35
|
+
-----| server/
|
|
36
|
+
-------| api/
|
|
37
|
+
---------| hello.ts
|
|
38
|
+
---| admin/
|
|
39
|
+
-----| nuxt.config.ts
|
|
40
|
+
-----| app/
|
|
41
|
+
-------| pages/
|
|
42
|
+
---------| admin.vue
|
|
43
|
+
-------| layouts/
|
|
44
|
+
---------| admin.vue
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Automatic Aliases
|
|
48
|
+
|
|
49
|
+
Named layer aliases to the `srcDir` of each layer are automatically created. You can access a layer using the `#layers/[name]` alias:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
// Access the base layer
|
|
53
|
+
import something from '#layers/base/path/to/file'
|
|
54
|
+
|
|
55
|
+
// Access the admin layer
|
|
56
|
+
import { useAdmin } from '#layers/admin/composables/useAdmin'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
::note
|
|
60
|
+
Named layer aliases were introduced in Nuxt v3.16.0.
|
|
61
|
+
::
|
|
62
|
+
|
|
63
|
+
## Layer Content
|
|
64
|
+
|
|
65
|
+
Each layer can include:
|
|
66
|
+
|
|
67
|
+
- [`nuxt.config.ts`](/docs/4.x/directory-structure/nuxt-config) - Layer-specific configuration that will be merged with the main config
|
|
68
|
+
- [`app.config.ts`](/docs/4.x/directory-structure/app/app-config) - Reactive application configuration
|
|
69
|
+
- [`app/components/`](/docs/4.x/directory-structure/app/components) - Vue components (auto-imported)
|
|
70
|
+
- [`app/composables/`](/docs/4.x/directory-structure/app/composables) - Vue composables (auto-imported)
|
|
71
|
+
- [`app/utils/`](/docs/4.x/directory-structure/app/utils) - Utility functions (auto-imported)
|
|
72
|
+
- [`app/pages/`](/docs/4.x/directory-structure/app/pages) - Application pages
|
|
73
|
+
- [`app/layouts/`](/docs/4.x/directory-structure/app/layouts) - Application layouts
|
|
74
|
+
- [`app/middleware/`](/docs/4.x/directory-structure/app/middleware) - Route middleware
|
|
75
|
+
- [`app/plugins/`](/docs/4.x/directory-structure/app/plugins) - Nuxt plugins
|
|
76
|
+
- [`server/`](/docs/4.x/directory-structure/server) - Server routes, middleware, and utilities
|
|
77
|
+
- [`shared/`](/docs/4.x/directory-structure/shared) - Shared code between app and server
|
|
78
|
+
|
|
79
|
+
## Priority Order
|
|
80
|
+
|
|
81
|
+
When multiple layers define the same resource (component, composable, page, etc.), the layer with **higher priority wins**. Layers are sorted alphabetically, with later letters having higher priority (Z > A).
|
|
82
|
+
|
|
83
|
+
To control the order, prefix directories with numbers: `1.base/`, `2.features/`, `3.admin/`.
|
|
84
|
+
|
|
85
|
+
:read-more{to="/docs/4.x/getting-started/layers#layer-priority"}
|
|
86
|
+
|
|
87
|
+
:video-accordion{title="Watch a video from Learn Vue about Nuxt Layers" videoId="lnFCM7c9f7I"}
|
|
@@ -18,7 +18,7 @@ You don't need to add those local modules to your [`nuxt.config.ts`](/docs/4.x/d
|
|
|
18
18
|
```ts twoslash [modules/hello/index.ts]
|
|
19
19
|
// `nuxt/kit` is a helper subpath import you can use when defining local modules
|
|
20
20
|
// that means you do not need to add `@nuxt/kit` to your project's dependencies
|
|
21
|
-
import { addServerHandler, createResolver, defineNuxtModule } from 'nuxt/kit'
|
|
21
|
+
import { addComponentsDir, addServerHandler, createResolver, defineNuxtModule } from 'nuxt/kit'
|
|
22
22
|
|
|
23
23
|
export default defineNuxtModule({
|
|
24
24
|
meta: {
|
|
@@ -32,6 +32,12 @@ export default defineNuxtModule({
|
|
|
32
32
|
route: '/api/hello',
|
|
33
33
|
handler: resolver.resolve('./runtime/api-route'),
|
|
34
34
|
})
|
|
35
|
+
|
|
36
|
+
// Add components
|
|
37
|
+
addComponentsDir({
|
|
38
|
+
path: resolver.resolve('./runtime/app/components'),
|
|
39
|
+
pathPrefix: true, // Prefix your exports to avoid conflicts with user code or other modules
|
|
40
|
+
})
|
|
35
41
|
},
|
|
36
42
|
})
|
|
37
43
|
```
|
|
@@ -46,6 +52,10 @@ export default defineEventHandler(() => {
|
|
|
46
52
|
|
|
47
53
|
When starting Nuxt, the `hello` module will be registered and the `/api/hello` route will be available.
|
|
48
54
|
|
|
55
|
+
::note
|
|
56
|
+
Note that all components, pages, composables and other files that would be normally placed in your `app/` directory need to be in `modules/your-module/runtime/app/`. This ensures they can be type-checked properly.
|
|
57
|
+
::
|
|
58
|
+
|
|
49
59
|
Modules are executed in the following sequence:
|
|
50
60
|
- First, the modules defined in [`nuxt.config.ts`](/docs/4.x/api/nuxt-config#modules-1) are loaded.
|
|
51
61
|
- Then, modules found in the `modules/` directory are executed, and they load in alphabetical order.
|
|
@@ -59,7 +69,7 @@ modules/
|
|
|
59
69
|
2.second-module.ts
|
|
60
70
|
```
|
|
61
71
|
|
|
62
|
-
:read-more{to="/docs/4.x/guide/
|
|
72
|
+
:read-more{to="/docs/4.x/guide/modules"}
|
|
63
73
|
|
|
64
74
|
::tip{icon="i-lucide-video" to="https://vueschool.io/lessons/creating-your-first-module-from-scratch?friend=nuxt" target="_blank"}
|
|
65
75
|
Watch Vue School video about Nuxt private modules.
|
|
@@ -5,7 +5,7 @@ head.title: "node_modules/"
|
|
|
5
5
|
navigation.icon: i-vscode-icons-folder-type-node
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
The package manager ([`npm`](https://docs.npmjs.com/cli/commands/npm/) or [`yarn`](https://yarnpkg.com) or [`pnpm`](https://pnpm.io/cli/install) or [`bun`](https://bun.com/package-manager)) creates this directory to store the dependencies of your project.
|
|
8
|
+
The package manager ([`npm`](https://docs.npmjs.com/cli/commands/npm/) or [`yarn`](https://yarnpkg.com) or [`pnpm`](https://pnpm.io/cli/install) or [`bun`](https://bun.com/package-manager) or [`deno`](https://docs.deno.com/runtime/getting_started/installation/)) creates this directory to store the dependencies of your project.
|
|
9
9
|
|
|
10
10
|
::important
|
|
11
11
|
This directory should be added to your [`.gitignore`](/docs/4.x/directory-structure/gitignore) file to avoid pushing the dependencies to your repository.
|