@nuxt/docs-nightly 4.3.0-29481280.f3d5bc5f → 4.3.0-29481694.51da8e68
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.
|
@@ -86,32 +86,60 @@ Nuxt uses [unjs/c12](https://github.com/unjs/c12) and [unjs/giget](https://githu
|
|
|
86
86
|
|
|
87
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.
|
|
88
88
|
|
|
89
|
-
|
|
89
|
+
### Priority Order
|
|
90
|
+
|
|
91
|
+
From highest to lowest priority:
|
|
90
92
|
|
|
91
93
|
1. **Your project files** - always have the highest priority
|
|
92
94
|
2. **Auto-scanned layers** from `~~/layers` directory - sorted alphabetically (Z has higher priority than A)
|
|
93
95
|
3. **Layers in `extends`** config - first entry has higher priority than second
|
|
94
96
|
|
|
95
|
-
###
|
|
97
|
+
### Practical Example
|
|
96
98
|
|
|
97
|
-
|
|
98
|
-
|
|
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
|
+
```
|
|
99
125
|
|
|
100
126
|
::tip
|
|
101
|
-
|
|
127
|
+
This pattern is useful for creating base layers with defaults that can be progressively overridden by more specific layers.
|
|
102
128
|
::
|
|
103
129
|
|
|
104
|
-
###
|
|
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
|
|
105
136
|
|
|
106
137
|
```ts [nuxt.config.ts]
|
|
107
138
|
export default defineNuxtConfig({
|
|
108
139
|
extends: [
|
|
109
|
-
// Local layer outside
|
|
110
|
-
'
|
|
111
|
-
//
|
|
112
|
-
'@my-themes/awesome',
|
|
113
|
-
// Remote repository
|
|
114
|
-
'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
|
|
115
143
|
],
|
|
116
144
|
})
|
|
117
145
|
```
|
|
@@ -123,7 +151,9 @@ If you also have `~~/layers/custom`, the priority order is:
|
|
|
123
151
|
- `@my-themes/awesome`
|
|
124
152
|
- `github:my-themes/awesome#v1` (lowest)
|
|
125
153
|
|
|
126
|
-
|
|
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
|
+
::
|
|
127
157
|
|
|
128
158
|
::read-more{to="/docs/4.x/guide/going-further/layers"}
|
|
129
159
|
Read more about layers in the **Layer Author Guide**.
|
|
@@ -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
|
|
21
|
+
import { addComponentsDir, addServerHandler, createResolver, defineNuxtModule } from 'nuxt/kit'
|
|
22
22
|
|
|
23
23
|
export default defineNuxtModule({
|
|
24
24
|
meta: {
|
|
@@ -36,7 +36,7 @@ export default defineNuxtModule({
|
|
|
36
36
|
// Add components
|
|
37
37
|
addComponentsDir({
|
|
38
38
|
path: resolver.resolve('./runtime/app/components'),
|
|
39
|
-
pathPrefix: true // Prefix your exports to avoid conflicts with user code or other modules
|
|
39
|
+
pathPrefix: true, // Prefix your exports to avoid conflicts with user code or other modules
|
|
40
40
|
})
|
|
41
41
|
},
|
|
42
42
|
})
|
|
@@ -54,6 +54,10 @@ The [`content/`](/docs/4.x/directory-structure/content) directory is enabled by
|
|
|
54
54
|
|
|
55
55
|
The [`modules/`](/docs/4.x/directory-structure/modules) directory is the directory that contains the local modules of the Nuxt application. Modules are used to extend the functionality of the Nuxt application.
|
|
56
56
|
|
|
57
|
+
## Layers Directory
|
|
58
|
+
|
|
59
|
+
The [`layers/`](/docs/4.x/directory-structure/layers) directory allows you to organize and share reusable code, components, composables, and configurations. Layers within this directory are automatically registered in your project.
|
|
60
|
+
|
|
57
61
|
## Nuxt Files
|
|
58
62
|
|
|
59
63
|
- [`nuxt.config.ts`](/docs/4.x/directory-structure/nuxt-config) file is the main configuration file for the Nuxt application.
|
|
@@ -229,3 +229,15 @@ Nuxt automatically includes your module's directories in the appropriate type co
|
|
|
229
229
|
---| module.ts
|
|
230
230
|
---| augments.node.d.ts
|
|
231
231
|
```
|
|
232
|
+
|
|
233
|
+
### Known Limitations
|
|
234
|
+
|
|
235
|
+
#### Type-Checking Server Routes in App Context
|
|
236
|
+
|
|
237
|
+
Server routes are also type-checked using `tsconfig.app.json` in addition to `tsconfig.server.json`.
|
|
238
|
+
|
|
239
|
+
This is required because Nuxt infers the return types of your server endpoints to provide response types in [`$fetch`](/docs/4.x/api/utils/dollarfetch) and [`useFetch`](/docs/4.x/api/composables/use-fetch).
|
|
240
|
+
|
|
241
|
+
::warning
|
|
242
|
+
This can cause issues when using **server-only types** in your route files. For example, if a module creates a server-only virtual file using [`addServerTemplate`](/docs/api/kit/templates#addservertemplate) and you declare types for it in `tsconfig.server.json`, those type declarations will only be available in the server context. When the app context type-checks your server routes, it won't recognize these server-only types and will report errors. To resolve this, you unfortunately need to declare such types in the app context as well.
|
|
243
|
+
::
|