@nuxt/docs 3.17.6 → 3.18.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/01.introduction.md +2 -2
- package/1.getting-started/02.installation.md +7 -7
- package/1.getting-started/03.configuration.md +1 -1
- package/1.getting-started/04.views.md +1 -1
- package/1.getting-started/06.styling.md +3 -3
- package/1.getting-started/08.seo-meta.md +2 -2
- package/1.getting-started/09.transitions.md +3 -3
- package/1.getting-started/10.data-fetching.md +3 -3
- package/1.getting-started/12.error-handling.md +1 -1
- package/1.getting-started/15.prerendering.md +2 -2
- package/1.getting-started/16.deployment.md +1 -1
- package/1.getting-started/17.testing.md +2 -2
- package/1.getting-started/18.upgrade.md +222 -2
- package/2.guide/0.index.md +3 -0
- package/2.guide/1.concepts/3.rendering.md +1 -1
- package/2.guide/2.directory-structure/1.components.md +2 -2
- package/2.guide/2.directory-structure/1.pages.md +1 -1
- package/2.guide/2.directory-structure/2.env.md +4 -0
- package/2.guide/3.going-further/1.events.md +3 -22
- package/2.guide/3.going-further/2.hooks.md +21 -2
- package/2.guide/3.going-further/3.modules.md +27 -2
- package/2.guide/5.best-practices/.navigation.yml +3 -0
- package/2.guide/5.best-practices/hydration.md +188 -0
- package/2.guide/5.best-practices/performance.md +305 -0
- package/2.guide/5.best-practices/plugins.md +20 -0
- package/3.api/1.components/4.nuxt-link.md +1 -1
- package/3.api/2.composables/use-async-data.md +7 -3
- package/3.api/2.composables/use-fetch.md +13 -8
- package/3.api/2.composables/use-nuxt-data.md +1 -1
- package/3.api/2.composables/use-response-header.md +1 -1
- package/3.api/2.composables/use-route.md +1 -1
- package/3.api/2.composables/use-runtime-config.md +1 -1
- package/3.api/3.utils/$fetch.md +1 -1
- package/3.api/3.utils/define-lazy-hydration-component.md +261 -0
- package/3.api/3.utils/define-nuxt-route-middleware.md +1 -1
- package/3.api/3.utils/on-before-route-leave.md +1 -1
- package/3.api/3.utils/on-before-route-update.md +1 -1
- package/3.api/4.commands/init.md +2 -0
- package/3.api/5.kit/1.modules.md +49 -0
- package/3.api/5.kit/10.templates.md +1 -1
- package/3.api/5.kit/11.nitro.md +47 -0
- package/3.api/5.kit/4.autoimports.md +8 -8
- package/3.api/6.advanced/1.hooks.md +4 -4
- package/5.community/4.contribution.md +18 -0
- package/7.migration/6.pages-and-layouts.md +1 -1
- package/7.migration/8.runtime-config.md +1 -1
- package/package.json +1 -1
|
@@ -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 = {
|
|
@@ -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` | `
|
|
171
|
-
| `deep` | `boolean` | `
|
|
175
|
+
| `watch` | `MultiWatchSources \| false` | - | Array of reactive sources to watch and auto-refresh. `false` disables watching. |
|
|
176
|
+
| `deep` | `boolean` | `true` | Return data in a deep ref object. Set to `false` to return data in a shallow ref object, which can improve performance if your data does not need to be deeply reactive. |
|
|
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 `null`, set `status` to `idle`, and cancels any pending requests. |
|
|
198
203
|
|
|
@@ -15,7 +15,7 @@ This composable is available in Nuxt v3.14+.
|
|
|
15
15
|
You can use the built-in [`useResponseHeader`](/docs/api/composables/use-response-header) composable to set any server response header within your pages, components, and plugins.
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
|
-
// Set
|
|
18
|
+
// Set a custom response header
|
|
19
19
|
const header = useResponseHeader('X-My-Header');
|
|
20
20
|
header.value = 'my-value';
|
|
21
21
|
```
|
|
@@ -49,4 +49,4 @@ Apart from dynamic parameters and query parameters, `useRoute()` also provides t
|
|
|
49
49
|
Browsers don't send [URL fragments](https://url.spec.whatwg.org/#concept-url-fragment) (for example `#foo`) when making requests. So using `route.fullPath` in your template can trigger hydration issues because this will include the fragment on client but not the server.
|
|
50
50
|
::
|
|
51
51
|
|
|
52
|
-
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api
|
|
52
|
+
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/type-aliases/RouteLocationNormalizedLoaded.html"}
|
|
@@ -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/3.utils/$fetch.md
CHANGED
|
@@ -20,7 +20,7 @@ Using `$fetch` in components without wrapping it with [`useAsyncData`](/docs/api
|
|
|
20
20
|
|
|
21
21
|
## Usage
|
|
22
22
|
|
|
23
|
-
We recommend
|
|
23
|
+
We recommend using [`useFetch`](/docs/api/composables/use-fetch) or [`useAsyncData`](/docs/api/composables/use-async-data) + `$fetch` to prevent double data fetching when fetching the component data.
|
|
24
24
|
|
|
25
25
|
```vue [app.vue]
|
|
26
26
|
<script setup lang="ts">
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'defineLazyHydrationComponent'
|
|
3
|
+
description: 'Define a lazy hydration component with a specific strategy.'
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/components/plugins/lazy-hydration-macro-transform.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
`defineLazyHydrationComponent` is a compiler macro that helps you create a component with a specific lazy hydration strategy. Lazy hydration defers hydration until components become visible or until the browser has completed more critical tasks. This can significantly reduce the initial performance cost, especially for non-essential components.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Visibility Strategy
|
|
16
|
+
|
|
17
|
+
Hydrates the component when it becomes visible in the viewport.
|
|
18
|
+
|
|
19
|
+
```vue
|
|
20
|
+
<script setup lang="ts">
|
|
21
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
22
|
+
'visible',
|
|
23
|
+
() => import('./components/MyComponent.vue')
|
|
24
|
+
)
|
|
25
|
+
</script>
|
|
26
|
+
|
|
27
|
+
<template>
|
|
28
|
+
<div>
|
|
29
|
+
<!--
|
|
30
|
+
Hydration will be triggered when
|
|
31
|
+
the element(s) is 100px away from entering the viewport.
|
|
32
|
+
-->
|
|
33
|
+
<LazyHydrationMyComponent :hydrate-on-visible="{ rootMargin: '100px' }" />
|
|
34
|
+
</div>
|
|
35
|
+
</template>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The `hydrateOnVisible` prop is optional. You can pass an object to customize the behavior of the `IntersectionObserver` under the hood.
|
|
39
|
+
|
|
40
|
+
::read-more{to="https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver" title="IntersectionObserver options"}
|
|
41
|
+
Read more about the options for `hydrate-on-visible`.
|
|
42
|
+
::
|
|
43
|
+
|
|
44
|
+
::note
|
|
45
|
+
Under the hood, this uses Vue's built-in [`hydrateOnVisible` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-visible).
|
|
46
|
+
::
|
|
47
|
+
|
|
48
|
+
### Idle Strategy
|
|
49
|
+
|
|
50
|
+
Hydrates the component when the browser is idle. This is suitable if you need the component to load as soon as possible, but not block the critical rendering path.
|
|
51
|
+
|
|
52
|
+
```vue
|
|
53
|
+
<script setup lang="ts">
|
|
54
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
55
|
+
'idle',
|
|
56
|
+
() => import('./components/MyComponent.vue')
|
|
57
|
+
)
|
|
58
|
+
</script>
|
|
59
|
+
|
|
60
|
+
<template>
|
|
61
|
+
<div>
|
|
62
|
+
<!-- Hydration will be triggered when the browser is idle or after 2000ms. -->
|
|
63
|
+
<LazyHydrationMyComponent :hydrate-on-idle="2000" />
|
|
64
|
+
</div>
|
|
65
|
+
</template>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
The `hydrateOnIdle` prop is optional. You can pass a positive number to specify the maximum timeout.
|
|
69
|
+
|
|
70
|
+
Idle strategy is for components that can be hydrated when the browser is idle.
|
|
71
|
+
|
|
72
|
+
::note
|
|
73
|
+
Under the hood, this uses Vue's built-in [`hydrateOnIdle` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-idle).
|
|
74
|
+
::
|
|
75
|
+
|
|
76
|
+
### Interaction Strategy
|
|
77
|
+
|
|
78
|
+
Hydrates the component after a specified interaction (e.g., click, mouseover).
|
|
79
|
+
|
|
80
|
+
```vue
|
|
81
|
+
<script setup lang="ts">
|
|
82
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
83
|
+
'interaction',
|
|
84
|
+
() => import('./components/MyComponent.vue')
|
|
85
|
+
)
|
|
86
|
+
</script>
|
|
87
|
+
|
|
88
|
+
<template>
|
|
89
|
+
<div>
|
|
90
|
+
<!--
|
|
91
|
+
Hydration will be triggered when
|
|
92
|
+
the element(s) is hovered over by the pointer.
|
|
93
|
+
-->
|
|
94
|
+
<LazyHydrationMyComponent hydrate-on-interaction="mouseover" />
|
|
95
|
+
</div>
|
|
96
|
+
</template>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The `hydrateOnInteraction` prop is optional. If you do not pass an event or a list of events, it defaults to hydrating on `pointerenter`, `click`, and `focus`.
|
|
100
|
+
|
|
101
|
+
::note
|
|
102
|
+
Under the hood, this uses Vue's built-in [`hydrateOnInteraction` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-interaction).
|
|
103
|
+
::
|
|
104
|
+
|
|
105
|
+
### Media Query Strategy
|
|
106
|
+
|
|
107
|
+
Hydrates the component when the window matches a media query.
|
|
108
|
+
|
|
109
|
+
```vue
|
|
110
|
+
<script setup lang="ts">
|
|
111
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
112
|
+
'mediaQuery',
|
|
113
|
+
() => import('./components/MyComponent.vue')
|
|
114
|
+
)
|
|
115
|
+
</script>
|
|
116
|
+
|
|
117
|
+
<template>
|
|
118
|
+
<div>
|
|
119
|
+
<!--
|
|
120
|
+
Hydration will be triggered when
|
|
121
|
+
the window width is greater than or equal to 768px.
|
|
122
|
+
-->
|
|
123
|
+
<LazyHydrationMyComponent hydrate-on-media-query="(min-width: 768px)" />
|
|
124
|
+
</div>
|
|
125
|
+
</template>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
::note
|
|
129
|
+
Under the hood, this uses Vue's built-in [`hydrateOnMediaQuery` strategy](https://vuejs.org/guide/components/async.html#hydrate-on-media-query).
|
|
130
|
+
::
|
|
131
|
+
|
|
132
|
+
### Time Strategy
|
|
133
|
+
|
|
134
|
+
Hydrates the component after a specified delay (in milliseconds).
|
|
135
|
+
|
|
136
|
+
```vue
|
|
137
|
+
<script setup lang="ts">
|
|
138
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
139
|
+
'time',
|
|
140
|
+
() => import('./components/MyComponent.vue')
|
|
141
|
+
)
|
|
142
|
+
</script>
|
|
143
|
+
|
|
144
|
+
<template>
|
|
145
|
+
<div>
|
|
146
|
+
<!-- Hydration is triggered after 1000ms. -->
|
|
147
|
+
<LazyHydrationMyComponent :hydrate-after="1000" />
|
|
148
|
+
</div>
|
|
149
|
+
</template>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Time strategy is for components that can wait a specific amount of time.
|
|
153
|
+
|
|
154
|
+
### If Strategy
|
|
155
|
+
|
|
156
|
+
Hydrates the component based on a boolean condition.
|
|
157
|
+
|
|
158
|
+
```vue
|
|
159
|
+
<script setup lang="ts">
|
|
160
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
161
|
+
'if',
|
|
162
|
+
() => import('./components/MyComponent.vue')
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
const isReady = ref(false)
|
|
166
|
+
|
|
167
|
+
function myFunction() {
|
|
168
|
+
// Trigger custom hydration strategy...
|
|
169
|
+
isReady.value = true
|
|
170
|
+
}
|
|
171
|
+
</script>
|
|
172
|
+
|
|
173
|
+
<template>
|
|
174
|
+
<div>
|
|
175
|
+
<!-- Hydration is triggered when isReady becomes true. -->
|
|
176
|
+
<LazyHydrationMyComponent :hydrate-when="isReady" />
|
|
177
|
+
</div>
|
|
178
|
+
</template>
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
If strategy is best for components that might not always need to be hydrated.
|
|
182
|
+
|
|
183
|
+
### Never Hydrate
|
|
184
|
+
|
|
185
|
+
Never hydrates the component.
|
|
186
|
+
|
|
187
|
+
```vue
|
|
188
|
+
<script setup lang="ts">
|
|
189
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
190
|
+
'never',
|
|
191
|
+
() => import('./components/MyComponent.vue')
|
|
192
|
+
)
|
|
193
|
+
</script>
|
|
194
|
+
|
|
195
|
+
<template>
|
|
196
|
+
<div>
|
|
197
|
+
<!-- This component will never be hydrated by Vue. -->
|
|
198
|
+
<LazyHydrationMyComponent />
|
|
199
|
+
</div>
|
|
200
|
+
</template>
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Listening to Hydration Events
|
|
204
|
+
|
|
205
|
+
All delayed hydration components emit a `@hydrated` event when they are hydrated.
|
|
206
|
+
|
|
207
|
+
```vue
|
|
208
|
+
<script setup lang="ts">
|
|
209
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
210
|
+
'visible',
|
|
211
|
+
() => import('./components/MyComponent.vue')
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
function onHydrate() {
|
|
215
|
+
console.log("Component has been hydrated!")
|
|
216
|
+
}
|
|
217
|
+
</script>
|
|
218
|
+
|
|
219
|
+
<template>
|
|
220
|
+
<div>
|
|
221
|
+
<LazyHydrationMyComponent
|
|
222
|
+
:hydrate-on-visible="{ rootMargin: '100px' }"
|
|
223
|
+
@hydrated="onHydrated"
|
|
224
|
+
/>
|
|
225
|
+
</div>
|
|
226
|
+
</template>
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
## Parameters
|
|
230
|
+
|
|
231
|
+
::warning
|
|
232
|
+
To ensure that the compiler correctly recognizes this macro, avoid using external variables. The following approach will prevent the macro from being properly recognized:
|
|
233
|
+
|
|
234
|
+
```vue
|
|
235
|
+
<script setup lang="ts">
|
|
236
|
+
const strategy = 'visible'
|
|
237
|
+
const source = () => import('./components/MyComponent.vue')
|
|
238
|
+
const LazyHydrationMyComponent = defineLazyHydrationComponent(strategy, source)
|
|
239
|
+
</script>
|
|
240
|
+
```
|
|
241
|
+
::
|
|
242
|
+
|
|
243
|
+
### `strategy`
|
|
244
|
+
|
|
245
|
+
- **Type**: `'visible' | 'idle' | 'interaction' | 'mediaQuery' | 'if' | 'time' | 'never'`
|
|
246
|
+
- **Required**: `true`
|
|
247
|
+
|
|
248
|
+
| Strategy | Description |
|
|
249
|
+
|---------------|----------------------------------------------------------------|
|
|
250
|
+
| `visible` | Hydrates when the component becomes visible in the viewport. |
|
|
251
|
+
| `idle` | Hydrates when the browser is idle or after a delay. |
|
|
252
|
+
| `interaction` | Hydrates upon user interaction (e.g., click, hover). |
|
|
253
|
+
| `mediaQuery` | Hydrates when the specified media query condition is met. |
|
|
254
|
+
| `if` | Hydrates when a specified boolean condition is met. |
|
|
255
|
+
| `time` | Hydrates after a specified time delay. |
|
|
256
|
+
| `never` | Prevents Vue from hydrating the component. |
|
|
257
|
+
|
|
258
|
+
### `source`
|
|
259
|
+
|
|
260
|
+
- **Type**: `() => Promise<Component>`
|
|
261
|
+
- **Required**: `true`
|
|
@@ -28,7 +28,7 @@ interface RouteMiddleware {
|
|
|
28
28
|
|
|
29
29
|
A function that takes two Vue Router's route location objects as parameters: the next route `to` as the first, and the current route `from` as the second.
|
|
30
30
|
|
|
31
|
-
Learn more about available properties of `RouteLocationNormalized` in the **[Vue Router docs](https://router.vuejs.org/api
|
|
31
|
+
Learn more about available properties of `RouteLocationNormalized` in the **[Vue Router docs](https://router.vuejs.org/api/type-aliases/RouteLocationNormalized.html)**.
|
|
32
32
|
|
|
33
33
|
## Examples
|
|
34
34
|
|
|
@@ -8,4 +8,4 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api
|
|
11
|
+
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/functions/onBeforeRouteLeave.html" title="Vue Router Docs" target="_blank"}
|
|
@@ -8,4 +8,4 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api
|
|
11
|
+
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/functions/onBeforeRouteUpdate.html" title="Vue Router Docs" target="_blank"}
|
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/1.modules.md
CHANGED
|
@@ -44,6 +44,12 @@ import type { ModuleDefinition, ModuleOptions, NuxtModule } from '@nuxt/schema'
|
|
|
44
44
|
export function defineNuxtModule<TOptions extends ModuleOptions> (
|
|
45
45
|
definition?: ModuleDefinition<TOptions, Partial<TOptions>, false> | NuxtModule<TOptions, Partial<TOptions>, false>,
|
|
46
46
|
): NuxtModule<TOptions, TOptions, false>
|
|
47
|
+
|
|
48
|
+
export function defineNuxtModule<TOptions extends ModuleOptions> (): {
|
|
49
|
+
with: <TOptionsDefaults extends Partial<TOptions>> (
|
|
50
|
+
definition: ModuleDefinition<TOptions, TOptionsDefaults, true> | NuxtModule<TOptions, TOptionsDefaults, true>
|
|
51
|
+
) => NuxtModule<TOptions, TOptionsDefaults, true>
|
|
52
|
+
}
|
|
47
53
|
```
|
|
48
54
|
|
|
49
55
|
### Parameters
|
|
@@ -122,6 +128,49 @@ If the user tries to use your module with an incompatible Nuxt version, they wil
|
|
|
122
128
|
- [nuxt] Nuxt version ^3.1.0 is required but currently using 3.0.0
|
|
123
129
|
```
|
|
124
130
|
|
|
131
|
+
#### Type Safety for Resolved Options with `.with()`
|
|
132
|
+
|
|
133
|
+
When you need type safety for your resolved/merged module options, you can use the `.with()` method. This enables TypeScript to properly infer the relationship between your module's defaults and the final resolved options that your setup function receives.
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
import { defineNuxtModule } from '@nuxt/kit'
|
|
137
|
+
|
|
138
|
+
// Define your module options interface
|
|
139
|
+
interface ModuleOptions {
|
|
140
|
+
apiKey: string
|
|
141
|
+
baseURL: string
|
|
142
|
+
timeout?: number
|
|
143
|
+
retries?: number
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export default defineNuxtModule<ModuleOptions>().with({
|
|
147
|
+
meta: {
|
|
148
|
+
name: '@nuxtjs/my-api',
|
|
149
|
+
configKey: 'myApi'
|
|
150
|
+
},
|
|
151
|
+
defaults: {
|
|
152
|
+
baseURL: 'https://api.example.com',
|
|
153
|
+
timeout: 5000,
|
|
154
|
+
retries: 3
|
|
155
|
+
},
|
|
156
|
+
setup(resolvedOptions, nuxt) {
|
|
157
|
+
// resolvedOptions is properly typed as:
|
|
158
|
+
// {
|
|
159
|
+
// apiKey: string // Required, no default provided
|
|
160
|
+
// baseURL: string // Required, has default value
|
|
161
|
+
// timeout: number // Optional, has default value
|
|
162
|
+
// retries: number // Optional, has default value
|
|
163
|
+
// }
|
|
164
|
+
|
|
165
|
+
console.log(resolvedOptions.baseURL) // ✅ TypeScript knows this is always defined
|
|
166
|
+
console.log(resolvedOptions.timeout) // ✅ TypeScript knows this is always defined
|
|
167
|
+
console.log(resolvedOptions.retries) // ✅ TypeScript knows this is always defined
|
|
168
|
+
}
|
|
169
|
+
})
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Without using `.with()`, the `resolvedOptions` parameter would be typed as the raw `ModuleOptions` interface, where `timeout` and `retries` could be `undefined` even when defaults are provided. The `.with()` method enables TypeScript to understand that default values make those properties non-optional in the resolved options.
|
|
173
|
+
|
|
125
174
|
## `installModule`
|
|
126
175
|
|
|
127
176
|
Install specified Nuxt module programmatically. This is helpful when your module depends on other modules. You can pass the module options as an object to `inlineOptions` and they will be passed to the module's `setup` function.
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
Templates
|
|
11
|
+
Templates allow you to generate extra files during development and build time. These files will be available in virtual filesystem and can be used in plugins, layouts, components, etc. `addTemplate` and `addTypeTemplate` allow you to add templates to the Nuxt application. `updateTemplates` allows you to regenerate templates that match the filter.
|
|
12
12
|
|
|
13
13
|
## `addTemplate`
|
|
14
14
|
|
package/3.api/5.kit/11.nitro.md
CHANGED
|
@@ -293,6 +293,53 @@ function addPrerenderRoutes (routes: string | string[]): void
|
|
|
293
293
|
| ----------- | ------------------------------- | -------- | ---------------------------------------------- |
|
|
294
294
|
| `routes` | `string \| string[]`{lang="ts"} | `true` | A route or an array of routes to prerender. |
|
|
295
295
|
|
|
296
|
+
## `addServerImports`
|
|
297
|
+
|
|
298
|
+
Add imports to the server. It makes your imports available in Nitro without the need to import them manually.
|
|
299
|
+
|
|
300
|
+
### Usage
|
|
301
|
+
|
|
302
|
+
```ts twoslash
|
|
303
|
+
import { defineNuxtModule, createResolver, addServerImports } from '@nuxt/kit'
|
|
304
|
+
|
|
305
|
+
export default defineNuxtModule({
|
|
306
|
+
setup(options) {
|
|
307
|
+
const names = [
|
|
308
|
+
'useStoryblok',
|
|
309
|
+
'useStoryblokApi',
|
|
310
|
+
'useStoryblokBridge',
|
|
311
|
+
'renderRichText',
|
|
312
|
+
'RichTextSchema'
|
|
313
|
+
]
|
|
314
|
+
|
|
315
|
+
names.forEach((name) =>
|
|
316
|
+
addServerImports({ name, as: name, from: '@storyblok/vue' })
|
|
317
|
+
)
|
|
318
|
+
}
|
|
319
|
+
})
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### Type
|
|
323
|
+
|
|
324
|
+
```ts
|
|
325
|
+
function addServerImports (dirs: Import | Import[]): void
|
|
326
|
+
```
|
|
327
|
+
|
|
328
|
+
### Parameters
|
|
329
|
+
|
|
330
|
+
`imports`: An object or an array of objects with the following properties:
|
|
331
|
+
|
|
332
|
+
| Property | Type | Required | Description |
|
|
333
|
+
| ------------------ | ---------------------------- | -------- | --------------------------------------------------------------------------------------------------------------- |
|
|
334
|
+
| `name` | `string` | `true` | Import name to be detected. |
|
|
335
|
+
| `from` | `string` | `true` | Module specifier to import from. |
|
|
336
|
+
| `priority` | `number` | `false` | Priority of the import; if multiple imports have the same name, the one with the highest priority will be used. |
|
|
337
|
+
| `disabled` | `boolean` | `false` | If this import is disabled. |
|
|
338
|
+
| `meta` | `Record<string, any>` | `false` | Metadata of the import. |
|
|
339
|
+
| `type` | `boolean` | `false` | If this import is a pure type import. |
|
|
340
|
+
| `typeFrom` | `string` | `false` | Use this as the `from` value when generating type declarations. |
|
|
341
|
+
| `as` | `string` | `false` | Import as this name. |
|
|
342
|
+
|
|
296
343
|
## `addServerImportsDir`
|
|
297
344
|
|
|
298
345
|
Add a directory to be scanned for auto-imports by Nitro.
|
|
@@ -34,16 +34,16 @@ import { defineNuxtModule, addImports } from "@nuxt/kit";
|
|
|
34
34
|
export default defineNuxtModule({
|
|
35
35
|
setup(options, nuxt) {
|
|
36
36
|
const names = [
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
]
|
|
37
|
+
'useStoryblok',
|
|
38
|
+
'useStoryblokApi',
|
|
39
|
+
'useStoryblokBridge',
|
|
40
|
+
'renderRichText',
|
|
41
|
+
'RichTextSchema'
|
|
42
|
+
]
|
|
43
43
|
|
|
44
44
|
names.forEach((name) =>
|
|
45
|
-
addImports({ name, as: name, from:
|
|
46
|
-
)
|
|
45
|
+
addImports({ name, as: name, from: '@storyblok/vue' })
|
|
46
|
+
)
|
|
47
47
|
}
|
|
48
48
|
})
|
|
49
49
|
```
|
|
@@ -74,13 +74,13 @@ Hook | Arguments | Description
|
|
|
74
74
|
`schema:resolved` | `schema` | Allows extending resolved schema.
|
|
75
75
|
`schema:beforeWrite` | `schema` | Called before writing the given schema.
|
|
76
76
|
`schema:written` | - | Called after the schema is written.
|
|
77
|
-
`vite:extend` | `viteBuildContext` | Allows
|
|
78
|
-
`vite:extendConfig` | `viteInlineConfig, env` | Allows
|
|
79
|
-
`vite:configResolved` | `viteInlineConfig, env` | Allows
|
|
77
|
+
`vite:extend` | `viteBuildContext` | Allows extending Vite default context.
|
|
78
|
+
`vite:extendConfig` | `viteInlineConfig, env` | Allows extending Vite default config.
|
|
79
|
+
`vite:configResolved` | `viteInlineConfig, env` | Allows reading the resolved Vite config.
|
|
80
80
|
`vite:serverCreated` | `viteServer, env` | Called when the Vite server is created.
|
|
81
81
|
`vite:compiled` | - | Called after Vite server is compiled.
|
|
82
82
|
`webpack:config` | `webpackConfigs` | Called before configuring the webpack compiler.
|
|
83
|
-
`webpack:configResolved` | `webpackConfigs` | Allows
|
|
83
|
+
`webpack:configResolved` | `webpackConfigs` | Allows reading the resolved webpack config.
|
|
84
84
|
`webpack:compile` | `options` | Called right before compilation.
|
|
85
85
|
`webpack:compiled` | `options` | Called after resources are loaded.
|
|
86
86
|
`webpack:change` | `shortPath` | Called on `change` on WebpackBar.
|
|
@@ -80,6 +80,24 @@ If we mark a PR as 'pending', that means we likely have another task to do in re
|
|
|
80
80
|
|
|
81
81
|
We'll do our best to follow [our PR decision making flowchart](https://mermaid.live/view#pako:eNp9VE1v2kAQ_SsjXzBSEqlALlaUisSh0ACK2l4qcVm8Y9hi7672Iwly-O-ZtYPt5FAOCHbee_PmzdpVlCmOURLlhXrJ9sw4-JNuJNBnWs1UQafIQVjrERyWumAOv58-AJeXt29_0b7BXbWwwL0uRPa1vlZvcB_fF8oiMMmB2QM4BXkt3UoON7Lh3LWaDz2SVkK6QGt7DHvw0CKt5sxCKaQoWQEGtVHcZ04oGdw04LTVngW_LHOeFcURGGz97mw6PSv-iJdsi0UCA4nI7SfNwc3W3JZit3eQ1SZFDlKB15yswQ2MgbOjbYeatY3n8bcr-IWlekYYaJRcyB04I9gOB1CEfkF5dAVTzmFAtnqn4-bUYAiMMmHZgWhNPRhgus5mW2BATxq0NkIZ4Y4NbNjzE2ZchBzcHmGLe_ZMSKCcyRXyLrVFa_5n_PBK2xKy3kk9eOjULUdltk6C8kI-7NFDr8f4EVGDoqlp-wa4sJm3ltIMIuZ_mTQXJyTSkQZtunPqsKxShV9GKdkBYe1fHXjpbcjlvONlO9Kqx_M7YHmOmav_luxfE5zKwVs09hM5DLSupgYDlr5flDkwo7ykixKG-xDsUly1LZ-uY32dgDc7lG7YqwbNp0msJwmIUivjWFtfd-xRrEcJ7Omydz37qFplHOtxEp4GskI2qB5dRCWakglOz3oV8JuITJa4iRL6yZk5bKKNPBGOead-H2UWJc54vIiaW53SPgwrz4fIhVNm1bw76lfI6R2_MW21) when responding and reviewing to pull requests.
|
|
82
82
|
|
|
83
|
+
### AI-Assisted Contributions
|
|
84
|
+
|
|
85
|
+
We welcome the thoughtful use of AI tools when contributing to Nuxt, yet ask all contributors to follow [two core principles](https://roe.dev/blog/using-ai-in-open-source).
|
|
86
|
+
|
|
87
|
+
#### Never let an LLM speak for you
|
|
88
|
+
|
|
89
|
+
* All comments, issues, and pull request descriptions should be written in your own voice
|
|
90
|
+
* We value clear, human communication over perfect grammar or spelling
|
|
91
|
+
* Avoid copy-pasting AI-generated summaries that don't reflect your own understanding
|
|
92
|
+
|
|
93
|
+
#### Never let an LLM think for you
|
|
94
|
+
|
|
95
|
+
* Feel free to use AI tools to generate code or explore ideas
|
|
96
|
+
* Only submit contributions you fully understand and can explain
|
|
97
|
+
* Contributions should reflect your own reasoning and problem-solving
|
|
98
|
+
|
|
99
|
+
Our aim is ensuring quality and maintaining the joy of collaborating and communicating with real people. If you have ideas for improving our policy on AI in the Nuxt community, we'd love to hear them! ❤️
|
|
100
|
+
|
|
83
101
|
### Create a Module
|
|
84
102
|
|
|
85
103
|
If you've built something with Nuxt that's cool, why not [extract it into a module](/docs/guide/going-further/modules), so it can be shared with others? We have [many excellent modules already](/modules), but there's always room for more.
|
|
@@ -193,7 +193,7 @@ Most of the syntax and functionality are the same for the global [NuxtLink](/doc
|
|
|
193
193
|
When migrating from Nuxt 2 to Nuxt 3, you will have to update how you programmatically navigate your users. In Nuxt 2, you had access to the underlying Vue Router with `this.$router`. In Nuxt 3, you can use the `navigateTo()` utility method which allows you to pass a route and parameters to Vue Router.
|
|
194
194
|
|
|
195
195
|
::warning
|
|
196
|
-
|
|
196
|
+
Make sure to always `await` on [`navigateTo`](/docs/api/utils/navigate-to) or chain its result by returning from functions.
|
|
197
197
|
::
|
|
198
198
|
|
|
199
199
|
::code-group
|