@nuxt/docs 3.20.2 → 3.21.1
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 +4 -4
- package/1.getting-started/02.installation.md +4 -7
- package/1.getting-started/03.configuration.md +29 -29
- package/1.getting-started/04.views.md +6 -4
- package/1.getting-started/05.assets.md +2 -2
- package/1.getting-started/06.styling.md +22 -16
- package/1.getting-started/07.routing.md +6 -6
- package/1.getting-started/08.seo-meta.md +8 -4
- package/1.getting-started/09.transitions.md +6 -6
- package/1.getting-started/10.data-fetching.md +18 -18
- package/1.getting-started/11.state-management.md +5 -5
- package/1.getting-started/12.error-handling.md +25 -19
- package/1.getting-started/13.server.md +9 -9
- package/1.getting-started/14.layers.md +49 -15
- package/1.getting-started/15.prerendering.md +18 -4
- package/1.getting-started/16.deployment.md +2 -1
- package/1.getting-started/17.testing.md +17 -7
- package/1.getting-started/18.upgrade.md +111 -60
- package/2.directory-structure/0.output.md +1 -1
- package/2.directory-structure/1.assets.md +1 -1
- package/2.directory-structure/1.components.md +12 -8
- package/2.directory-structure/1.composables.md +2 -2
- package/2.directory-structure/1.content.md +1 -1
- package/2.directory-structure/1.layers.md +87 -0
- package/2.directory-structure/1.layouts.md +35 -3
- package/2.directory-structure/1.middleware.md +7 -7
- package/2.directory-structure/1.modules.md +8 -2
- package/2.directory-structure/1.node_modules.md +1 -1
- package/2.directory-structure/1.pages.md +39 -18
- package/2.directory-structure/1.plugins.md +4 -1
- package/2.directory-structure/1.public.md +1 -1
- package/2.directory-structure/1.server.md +28 -8
- package/2.directory-structure/1.shared.md +3 -3
- package/2.directory-structure/1.utils.md +2 -2
- package/2.directory-structure/2.env.md +3 -3
- package/2.directory-structure/2.nuxtignore.md +1 -0
- package/2.directory-structure/2.nuxtrc.md +5 -2
- package/2.directory-structure/3.app-config.md +2 -2
- package/2.directory-structure/3.app.md +3 -3
- package/2.directory-structure/3.error.md +9 -5
- package/2.directory-structure/3.nuxt-config.md +1 -1
- package/2.directory-structure/3.package.md +1 -1
- package/2.directory-structure/3.tsconfig.md +3 -2
- package/2.directory-structure/index.md +12 -8
- package/3.guide/0.index.md +5 -2
- package/3.guide/1.concepts/{3.rendering.md → 1.rendering.md} +11 -32
- package/3.guide/1.concepts/{2.vuejs-development.md → 10.vuejs-development.md} +9 -8
- package/3.guide/1.concepts/{10.nuxt-lifecycle.md → 2.nuxt-lifecycle.md} +31 -24
- package/3.guide/1.concepts/{1.auto-imports.md → 3.auto-imports.md} +6 -6
- package/3.guide/1.concepts/4.server-engine.md +2 -2
- package/3.guide/1.concepts/5.modules.md +14 -1
- package/3.guide/1.concepts/7.esm.md +5 -4
- package/3.guide/1.concepts/8.typescript.md +9 -15
- package/3.guide/1.concepts/9.code-style.md +1 -1
- package/3.guide/2.best-practices/hydration.md +1 -1
- package/3.guide/2.best-practices/performance.md +5 -5
- package/3.guide/3.ai/.navigation.yml +3 -0
- package/3.guide/3.ai/1.mcp.md +277 -0
- package/3.guide/3.ai/2.llms-txt.md +65 -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 +420 -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/{3.recipes → 5.recipes}/1.custom-routing.md +3 -3
- package/3.guide/{3.recipes → 5.recipes}/2.vite-plugin.md +4 -0
- package/3.guide/{3.recipes → 5.recipes}/3.custom-usefetch.md +2 -2
- package/3.guide/{3.recipes → 5.recipes}/4.sessions-and-authentication.md +3 -3
- package/3.guide/{4.going-further → 6.going-further}/1.events.md +3 -4
- package/3.guide/{4.going-further → 6.going-further}/1.experimental-features.md +211 -86
- package/3.guide/6.going-further/1.features.md +108 -0
- package/3.guide/{4.going-further → 6.going-further}/1.internals.md +4 -3
- package/3.guide/{4.going-further → 6.going-further}/10.runtime-config.md +2 -2
- package/3.guide/{4.going-further → 6.going-further}/11.nightly-release-channel.md +1 -1
- package/3.guide/{4.going-further → 6.going-further}/2.hooks.md +5 -5
- package/3.guide/{4.going-further → 6.going-further}/4.kit.md +3 -3
- package/3.guide/{4.going-further → 6.going-further}/6.nuxt-app.md +6 -6
- package/3.guide/{4.going-further → 6.going-further}/7.layers.md +31 -5
- package/3.guide/{4.going-further → 6.going-further}/9.debugging.md +3 -2
- package/4.api/1.components/1.nuxt-client-fallback.md +5 -1
- package/4.api/1.components/10.nuxt-picture.md +1 -1
- package/4.api/1.components/11.teleports.md +2 -2
- package/4.api/1.components/12.nuxt-route-announcer.md +0 -2
- package/4.api/1.components/13.nuxt-time.md +0 -2
- package/4.api/1.components/2.nuxt-page.md +3 -3
- package/4.api/1.components/3.nuxt-layout.md +6 -6
- package/4.api/1.components/4.nuxt-link.md +13 -13
- package/4.api/1.components/5.nuxt-loading-indicator.md +1 -1
- package/4.api/1.components/6.nuxt-error-boundary.md +2 -2
- package/4.api/1.components/7.nuxt-welcome.md +2 -2
- package/4.api/1.components/8.nuxt-island.md +9 -2
- package/4.api/2.composables/use-app-config.md +1 -1
- package/4.api/2.composables/use-async-data.md +5 -5
- package/4.api/2.composables/use-cookie.md +16 -16
- package/4.api/2.composables/use-error.md +3 -3
- package/4.api/2.composables/use-fetch.md +37 -37
- package/4.api/2.composables/use-head-safe.md +1 -1
- package/4.api/2.composables/use-head.md +22 -7
- package/4.api/2.composables/use-lazy-async-data.md +1 -1
- package/4.api/2.composables/use-lazy-fetch.md +9 -9
- package/4.api/2.composables/use-nuxt-app.md +9 -7
- package/4.api/2.composables/use-route-announcer.md +1 -3
- package/4.api/2.composables/use-route.md +2 -2
- package/4.api/2.composables/use-router.md +15 -15
- package/4.api/2.composables/use-runtime-config.md +5 -5
- package/4.api/2.composables/use-seo-meta.md +2 -2
- package/4.api/2.composables/use-server-seo-meta.md +2 -2
- package/4.api/2.composables/use-state.md +12 -2
- package/4.api/3.utils/$fetch.md +1 -1
- package/4.api/3.utils/abort-navigation.md +2 -2
- package/4.api/3.utils/call-once.md +2 -4
- package/4.api/3.utils/clear-error.md +1 -1
- package/4.api/3.utils/create-error.md +7 -7
- package/4.api/3.utils/define-lazy-hydration-component.md +5 -5
- package/4.api/3.utils/define-nuxt-component.md +1 -1
- package/4.api/3.utils/define-nuxt-plugin.md +12 -12
- package/4.api/3.utils/define-nuxt-route-middleware.md +2 -2
- package/4.api/3.utils/define-page-meta.md +18 -11
- package/4.api/3.utils/define-route-rules.md +2 -2
- package/4.api/3.utils/navigate-to.md +14 -14
- package/4.api/3.utils/on-before-route-leave.md +1 -1
- package/4.api/3.utils/on-before-route-update.md +1 -1
- package/4.api/3.utils/preload-route-components.md +2 -2
- package/4.api/3.utils/refresh-cookie.md +0 -2
- package/4.api/3.utils/refresh-nuxt-data.md +1 -1
- package/4.api/3.utils/reload-nuxt-app.md +1 -1
- package/4.api/3.utils/set-page-layout.md +36 -0
- package/4.api/3.utils/set-response-status.md +3 -3
- package/4.api/3.utils/show-error.md +4 -4
- package/4.api/3.utils/update-app-config.md +1 -1
- package/4.api/4.commands/add.md +12 -12
- package/4.api/4.commands/analyze.md +11 -11
- package/4.api/4.commands/build-module.md +11 -11
- package/4.api/4.commands/build.md +12 -12
- package/4.api/4.commands/cleanup.md +6 -6
- package/4.api/4.commands/dev.md +23 -23
- package/4.api/4.commands/devtools.md +7 -7
- package/4.api/4.commands/generate.md +12 -12
- package/4.api/4.commands/info.md +6 -6
- package/4.api/4.commands/init.md +18 -18
- package/4.api/4.commands/module.md +18 -18
- package/4.api/4.commands/prepare.md +10 -10
- package/4.api/4.commands/preview.md +11 -11
- package/4.api/4.commands/test.md +9 -9
- package/4.api/4.commands/typecheck.md +10 -10
- package/4.api/4.commands/upgrade.md +10 -10
- package/4.api/5.kit/1.modules.md +31 -18
- package/4.api/5.kit/10.templates.md +23 -23
- package/4.api/5.kit/11.nitro.md +40 -36
- package/4.api/5.kit/12.resolving.md +2 -2
- package/4.api/5.kit/14.builder.md +35 -23
- package/4.api/5.kit/16.layers.md +16 -16
- package/4.api/5.kit/2.programmatic.md +2 -2
- package/4.api/5.kit/3.compatibility.md +2 -2
- package/4.api/5.kit/4.autoimports.md +23 -19
- package/4.api/5.kit/5.components.md +35 -35
- package/4.api/5.kit/6.context.md +1 -1
- package/4.api/5.kit/8.layout.md +1 -1
- package/4.api/6.advanced/1.hooks.md +85 -85
- package/4.api/index.md +7 -7
- package/5.community/4.contribution.md +10 -10
- package/5.community/5.framework-contribution.md +9 -9
- package/5.community/6.roadmap.md +26 -26
- package/5.community/7.changelog.md +10 -0
- package/6.bridge/1.overview.md +8 -0
- package/6.bridge/3.bridge-composition-api.md +2 -2
- package/6.bridge/4.plugins-and-middleware.md +2 -2
- package/6.bridge/5.nuxt3-compatible-api.md +1 -1
- package/6.bridge/8.nitro.md +4 -0
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +5 -5
- package/7.migration/20.module-authors.md +3 -3
- package/7.migration/4.meta.md +1 -1
- package/7.migration/5.plugins-and-middleware.md +3 -3
- package/7.migration/6.pages-and-layouts.md +5 -5
- package/7.migration/7.component-options.md +6 -6
- package/7.migration/8.runtime-config.md +1 -1
- package/package.json +1 -1
- package/3.guide/4.going-further/1.features.md +0 -103
- package/3.guide/4.going-further/3.modules.md +0 -901
- /package/3.guide/{3.recipes → 5.recipes}/.navigation.yml +0 -0
- /package/3.guide/{4.going-further → 6.going-further}/.navigation.yml +0 -0
- /package/3.guide/{4.going-further → 6.going-further}/index.md +0 -0
|
@@ -80,11 +80,11 @@ async function addTodo () {
|
|
|
80
80
|
```
|
|
81
81
|
|
|
82
82
|
::warning
|
|
83
|
-
Beware that using only `$fetch` will not provide [network calls de-duplication and navigation prevention](#the-need-for-usefetch-and-useasyncdata). :br
|
|
84
|
-
It is recommended to use `$fetch` for client-side interactions (event-based) or combined with [`useAsyncData`](#useasyncdata) when fetching the initial component data.
|
|
83
|
+
Beware that using only `$fetch` will not provide [network calls de-duplication and navigation prevention](/docs/3.x/getting-started/data-fetching#the-need-for-usefetch-and-useasyncdata). :br
|
|
84
|
+
It is recommended to use `$fetch` for client-side interactions (event-based) or combined with [`useAsyncData`](/docs/3.x/getting-started/data-fetching#useasyncdata) when fetching the initial component data.
|
|
85
85
|
::
|
|
86
86
|
|
|
87
|
-
::read-more{to="/docs/api/utils/dollarfetch"}
|
|
87
|
+
::read-more{to="/docs/3.x/api/utils/dollarfetch"}
|
|
88
88
|
Read more about `$fetch`.
|
|
89
89
|
::
|
|
90
90
|
|
|
@@ -146,9 +146,9 @@ This composable is a wrapper around the [`useAsyncData`](/docs/3.x/api/composabl
|
|
|
146
146
|
|
|
147
147
|
:video-accordion{title="Watch a video from Alexander Lichter to avoid using useFetch the wrong way" videoId="njsGVmcWviY"}
|
|
148
148
|
|
|
149
|
-
:read-more{to="/docs/api/composables/use-fetch"}
|
|
149
|
+
:read-more{to="/docs/3.x/api/composables/use-fetch"}
|
|
150
150
|
|
|
151
|
-
:link-example{to="/docs/examples/features/data-fetching"}
|
|
151
|
+
:link-example{to="/docs/3.x/examples/features/data-fetching"}
|
|
152
152
|
|
|
153
153
|
## `useAsyncData`
|
|
154
154
|
|
|
@@ -220,7 +220,7 @@ await useAsyncData(() => offersStore.getOffer(route.params.slug))
|
|
|
220
220
|
```
|
|
221
221
|
::
|
|
222
222
|
|
|
223
|
-
::read-more{to="/docs/api/composables/use-async-data"}
|
|
223
|
+
::read-more{to="/docs/3.x/api/composables/use-async-data"}
|
|
224
224
|
Read more about `useAsyncData`.
|
|
225
225
|
::
|
|
226
226
|
|
|
@@ -280,11 +280,11 @@ const { status, data: posts } = useLazyFetch('/api/posts')
|
|
|
280
280
|
</script>
|
|
281
281
|
```
|
|
282
282
|
|
|
283
|
-
::read-more{to="/docs/api/composables/use-lazy-fetch"}
|
|
283
|
+
::read-more{to="/docs/3.x/api/composables/use-lazy-fetch"}
|
|
284
284
|
Read more about `useLazyFetch`.
|
|
285
285
|
::
|
|
286
286
|
|
|
287
|
-
::read-more{to="/docs/api/composables/use-lazy-async-data"}
|
|
287
|
+
::read-more{to="/docs/3.x/api/composables/use-lazy-async-data"}
|
|
288
288
|
Read more about `useLazyAsyncData`.
|
|
289
289
|
::
|
|
290
290
|
|
|
@@ -307,7 +307,7 @@ const { status, data: comments } = useFetch('/api/comments', {
|
|
|
307
307
|
})
|
|
308
308
|
```
|
|
309
309
|
|
|
310
|
-
The `useFetch` composable is meant to be invoked in setup method or called directly at the top level of a function in lifecycle hooks, otherwise you should use [`$fetch` method](#fetch).
|
|
310
|
+
The `useFetch` composable is meant to be invoked in setup method or called directly at the top level of a function in lifecycle hooks, otherwise you should use [`$fetch` method](/docs/3.x/getting-started/data-fetching#fetch).
|
|
311
311
|
|
|
312
312
|
### Minimize payload size
|
|
313
313
|
|
|
@@ -433,7 +433,7 @@ const { data, error, execute, refresh } = await useFetch('/api/users')
|
|
|
433
433
|
</template>
|
|
434
434
|
```
|
|
435
435
|
|
|
436
|
-
The `execute` function is an alias for `refresh` that works in exactly the same way but is more semantic for cases when the fetch is [not immediate](#not-immediate).
|
|
436
|
+
The `execute` function is an alias for `refresh` that works in exactly the same way but is more semantic for cases when the fetch is [not immediate](/docs/3.x/getting-started/data-fetching#not-immediate).
|
|
437
437
|
|
|
438
438
|
::tip
|
|
439
439
|
To globally refetch or invalidate cached data, see [`clearNuxtData`](/docs/3.x/api/utils/clear-nuxt-data) and [`refreshNuxtData`](/docs/3.x/api/utils/refresh-nuxt-data).
|
|
@@ -483,7 +483,7 @@ const { data, error, refresh } = await useFetch(`/api/users/${id.value}`, {
|
|
|
483
483
|
</script>
|
|
484
484
|
```
|
|
485
485
|
|
|
486
|
-
If you need to change the URL based on a reactive value, you may want to use a [computed URL](#computed-url) instead.
|
|
486
|
+
If you need to change the URL based on a reactive value, you may want to use a [computed URL](/docs/3.x/getting-started/data-fetching#computed-url) instead.
|
|
487
487
|
|
|
488
488
|
When reactive fetch options are provided, they'll be automatically watched and trigger refetches. In some cases, it can be useful to opt-out of this behavior by specifying `watch: false`.
|
|
489
489
|
|
|
@@ -516,9 +516,9 @@ const { data, status } = useLazyFetch('/api/user', {
|
|
|
516
516
|
</script>
|
|
517
517
|
```
|
|
518
518
|
|
|
519
|
-
In the case of more complex URL construction, you may use a callback as a [computed getter](https://vuejs.org/guide/essentials/computed
|
|
519
|
+
In the case of more complex URL construction, you may use a callback as a [computed getter](https://vuejs.org/guide/essentials/computed) that returns the URL string.
|
|
520
520
|
|
|
521
|
-
Every time a dependency changes, the data will be fetched using the newly constructed URL. Combine this with [not-immediate](#not-immediate), and you can wait until the reactive element changes before fetching.
|
|
521
|
+
Every time a dependency changes, the data will be fetched using the newly constructed URL. Combine this with [not-immediate](/docs/3.x/getting-started/data-fetching#not-immediate), and you can wait until the reactive element changes before fetching.
|
|
522
522
|
|
|
523
523
|
```vue
|
|
524
524
|
<script setup lang="ts">
|
|
@@ -555,7 +555,7 @@ const pending = computed(() => status.value === 'pending')
|
|
|
555
555
|
</template>
|
|
556
556
|
```
|
|
557
557
|
|
|
558
|
-
If you need to force a refresh when other reactive values change, you can also [watch other values](#watch).
|
|
558
|
+
If you need to force a refresh when other reactive values change, you can also [watch other values](/docs/3.x/getting-started/data-fetching#watch).
|
|
559
559
|
|
|
560
560
|
### Not immediate
|
|
561
561
|
|
|
@@ -657,11 +657,11 @@ export default defineNuxtComponent({
|
|
|
657
657
|
Using `<script setup>` or `<script setup lang="ts">` are the recommended way of declaring Vue components in Nuxt.
|
|
658
658
|
::
|
|
659
659
|
|
|
660
|
-
:read-more{to="/docs/api/utils/define-nuxt-component"}
|
|
660
|
+
:read-more{to="/docs/3.x/api/utils/define-nuxt-component"}
|
|
661
661
|
|
|
662
662
|
## Serializing Data From Server to Client
|
|
663
663
|
|
|
664
|
-
When using `useAsyncData` and `useLazyAsyncData` to transfer data fetched on server to the client (as well as anything else that utilizes [the Nuxt payload](/docs/3.x/api/composables/use-nuxt-app#payload)), the payload is serialized with [`devalue`](https://github.com/
|
|
664
|
+
When using `useAsyncData` and `useLazyAsyncData` to transfer data fetched on server to the client (as well as anything else that utilizes [the Nuxt payload](/docs/3.x/api/composables/use-nuxt-app#payload)), the payload is serialized with [`devalue`](https://github.com/sveltejs/devalue). This allows us to transfer not just basic JSON but also to serialize and revive/deserialize more advanced kinds of data, such as regular expressions, Dates, Map and Set, `ref`, `reactive`, `shallowRef`, `shallowReactive` and `NuxtError` - and more.
|
|
665
665
|
|
|
666
666
|
It is also possible to define your own serializer/deserializer for types that are not supported by Nuxt. You can read more in the [`useNuxtApp`](/docs/3.x/api/composables/use-nuxt-app#payload) docs.
|
|
667
667
|
|
|
@@ -733,7 +733,7 @@ const { data } = await useFetch('/api/bar')
|
|
|
733
733
|
|
|
734
734
|
Nuxt does not currently support an alternative serializer to `JSON.stringify`. However, you can return your payload as a normal string and utilize the `toJSON` method to maintain type safety.
|
|
735
735
|
|
|
736
|
-
In the example below, we use [superjson](https://github.com/
|
|
736
|
+
In the example below, we use [superjson](https://github.com/flightcontrolhq/superjson) as our serializer.
|
|
737
737
|
|
|
738
738
|
```ts [server/api/superjson.ts]
|
|
739
739
|
import superjson from 'superjson'
|
|
@@ -771,7 +771,7 @@ const { data } = await useFetch('/api/superjson', {
|
|
|
771
771
|
### Consuming SSE (Server-Sent Events) via POST request
|
|
772
772
|
|
|
773
773
|
::tip
|
|
774
|
-
If you're consuming SSE via GET request, you can use [`EventSource`](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) or VueUse composable [`useEventSource`](https://vueuse.org/core/
|
|
774
|
+
If you're consuming SSE via GET request, you can use [`EventSource`](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) or VueUse composable [`useEventSource`](https://vueuse.org/core/useeventsource/).
|
|
775
775
|
::
|
|
776
776
|
|
|
777
777
|
When consuming SSE via POST request, you need to handle the connection manually. Here's how you can do it:
|
|
@@ -6,7 +6,7 @@ navigation.icon: i-lucide-database
|
|
|
6
6
|
|
|
7
7
|
Nuxt provides the [`useState`](/docs/3.x/api/composables/use-state) composable to create a reactive and SSR-friendly shared state across components.
|
|
8
8
|
|
|
9
|
-
[`useState`](/docs/3.x/api/composables/use-state) is an SSR-friendly [`ref`](https://vuejs.org/api/reactivity-core
|
|
9
|
+
[`useState`](/docs/3.x/api/composables/use-state) is an SSR-friendly [`ref`](https://vuejs.org/api/reactivity-core#ref) replacement. Its value will be preserved after server-side rendering (during client-side hydration) and shared across all components using a unique key.
|
|
10
10
|
|
|
11
11
|
:video-accordion{title="Watch a video from Alexander Lichter about why and when to use useState" videoId="mv0WcBABcIk"}
|
|
12
12
|
|
|
@@ -14,7 +14,7 @@ Nuxt provides the [`useState`](/docs/3.x/api/composables/use-state) composable t
|
|
|
14
14
|
Because the data inside [`useState`](/docs/3.x/api/composables/use-state) will be serialized to JSON, it is important that it does not contain anything that cannot be serialized, such as classes, functions or symbols.
|
|
15
15
|
::
|
|
16
16
|
|
|
17
|
-
::read-more{to="/docs/api/composables/use-state"}
|
|
17
|
+
::read-more{to="/docs/3.x/api/composables/use-state"}
|
|
18
18
|
Read more about `useState` composable.
|
|
19
19
|
::
|
|
20
20
|
|
|
@@ -53,7 +53,7 @@ const counter = useState('counter', () => Math.round(Math.random() * 1000))
|
|
|
53
53
|
</template>
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
-
:link-example{to="/docs/examples/features/state-management"}
|
|
56
|
+
:link-example{to="/docs/3.x/examples/features/state-management"}
|
|
57
57
|
|
|
58
58
|
::note
|
|
59
59
|
To globally invalidate cached state, see [`clearNuxtState`](/docs/3.x/api/utils/clear-nuxt-state) util.
|
|
@@ -77,7 +77,7 @@ await callOnce(async () => {
|
|
|
77
77
|
This is similar to the [`nuxtServerInit` action](https://v2.nuxt.com/docs/directory-structure/store/#the-nuxtserverinit-action) in Nuxt 2, which allows filling the initial state of your store server-side before rendering the page.
|
|
78
78
|
::
|
|
79
79
|
|
|
80
|
-
:read-more{to="/docs/api/utils/call-once"}
|
|
80
|
+
:read-more{to="/docs/3.x/api/utils/call-once"}
|
|
81
81
|
|
|
82
82
|
### Usage with Pinia
|
|
83
83
|
|
|
@@ -194,7 +194,7 @@ const date = useLocaleDate(new Date('2016-10-26'))
|
|
|
194
194
|
```
|
|
195
195
|
::
|
|
196
196
|
|
|
197
|
-
:link-example{to="/docs/examples/advanced/locale"}
|
|
197
|
+
:link-example{to="/docs/3.x/examples/advanced/locale"}
|
|
198
198
|
|
|
199
199
|
## Shared State
|
|
200
200
|
|
|
@@ -17,11 +17,11 @@ Nuxt is a full-stack framework, which means there are several sources of unpreve
|
|
|
17
17
|
|
|
18
18
|
## Vue Errors
|
|
19
19
|
|
|
20
|
-
You can hook into Vue errors using [`onErrorCaptured`](https://vuejs.org/api/composition-api-lifecycle
|
|
20
|
+
You can hook into Vue errors using [`onErrorCaptured`](https://vuejs.org/api/composition-api-lifecycle#onerrorcaptured).
|
|
21
21
|
|
|
22
22
|
In addition, Nuxt provides a [`vue:error`](/docs/3.x/api/advanced/hooks#app-hooks-runtime) hook that will be called if any errors propagate up to the top level.
|
|
23
23
|
|
|
24
|
-
If you are using an error reporting framework, you can provide a global handler through [`vueApp.config.errorHandler`](https://vuejs.org/api/application
|
|
24
|
+
If you are using an error reporting framework, you can provide a global handler through [`vueApp.config.errorHandler`](https://vuejs.org/api/application#app-config-errorhandler). It will receive all Vue errors, even if they are handled.
|
|
25
25
|
|
|
26
26
|
```ts twoslash [plugins/error-handler.ts]
|
|
27
27
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
@@ -37,7 +37,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
::note
|
|
40
|
-
Note that the `vue:error` hook is based on [`onErrorCaptured`](https://vuejs.org/api/composition-api-lifecycle
|
|
40
|
+
Note that the `vue:error` hook is based on [`onErrorCaptured`](https://vuejs.org/api/composition-api-lifecycle#onerrorcaptured) lifecycle hook.
|
|
41
41
|
::
|
|
42
42
|
|
|
43
43
|
## Startup Errors
|
|
@@ -53,7 +53,7 @@ This includes:
|
|
|
53
53
|
|
|
54
54
|
## Nitro Server Errors
|
|
55
55
|
|
|
56
|
-
You cannot currently define a server-side handler for these errors, but can render an error page, see the [Render an Error Page](#error-page) section.
|
|
56
|
+
You cannot currently define a server-side handler for these errors, but can render an error page, see the [Render an Error Page](/docs/3.x/getting-started/error-handling#error-page) section.
|
|
57
57
|
|
|
58
58
|
## Errors with JS Chunks
|
|
59
59
|
|
|
@@ -78,7 +78,7 @@ It can also occur on the client side when:
|
|
|
78
78
|
- mounting your app if the error was not handled with `onErrorCaptured` or `vue:error` hook
|
|
79
79
|
- the Vue app is initialized and mounted in browser (`app:mounted`).
|
|
80
80
|
|
|
81
|
-
::read-more{to="/docs/api/advanced/hooks"}
|
|
81
|
+
::read-more{to="/docs/3.x/api/advanced/hooks"}
|
|
82
82
|
Discover all the Nuxt lifecycle hooks.
|
|
83
83
|
::
|
|
84
84
|
|
|
@@ -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>
|
|
@@ -128,7 +128,7 @@ Make sure to check before using anything dependent on Nuxt plugins, such as `$ro
|
|
|
128
128
|
::
|
|
129
129
|
|
|
130
130
|
::note
|
|
131
|
-
Rendering an error page is an entirely separate page load, meaning any registered middleware will run again. You can use [`useError`](#useerror) in middleware to check if an error is being handled.
|
|
131
|
+
Rendering an error page is an entirely separate page load, meaning any registered middleware will run again. You can use [`useError`](/docs/3.x/getting-started/error-handling#useerror) in middleware to check if an error is being handled.
|
|
132
132
|
::
|
|
133
133
|
|
|
134
134
|
::note
|
|
@@ -140,25 +140,25 @@ 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.
|
|
147
147
|
|
|
148
|
-
::read-more{to="/docs/api/composables/use-error"}
|
|
148
|
+
::read-more{to="/docs/3.x/api/composables/use-error"}
|
|
149
149
|
Read more about `useError` composable.
|
|
150
150
|
::
|
|
151
151
|
|
|
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.
|
|
159
159
|
|
|
160
160
|
If you throw an error created with `createError`:
|
|
161
|
-
- on server-side, it will trigger a full-screen error page which you can clear with [`clearError`](#clearerror).
|
|
161
|
+
- on server-side, it will trigger a full-screen error page which you can clear with [`clearError`](/docs/3.x/getting-started/error-handling#clearerror).
|
|
162
162
|
- on client-side, it will throw a non-fatal error for you to handle. If you need to trigger a full-screen error page, then you can do this by setting `fatal: true`.
|
|
163
163
|
|
|
164
164
|
```vue twoslash [pages/movies/[slug\\].vue]
|
|
@@ -168,28 +168,34 @@ 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
|
-
::
|
|
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
|
+
|
|
184
|
+
::read-more{to="/docs/3.x/api/utils/create-error"}
|
|
179
185
|
Read more about `createError` util.
|
|
180
186
|
::
|
|
181
187
|
|
|
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
|
-
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`](#clearerror).
|
|
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/3.x/getting-started/error-handling#clearerror).
|
|
189
195
|
|
|
190
196
|
It is recommended instead to use `throw createError()`.
|
|
191
197
|
|
|
192
|
-
::read-more{to="/docs/api/utils/show-error"}
|
|
198
|
+
::read-more{to="/docs/3.x/api/utils/show-error"}
|
|
193
199
|
Read more about `showError` util.
|
|
194
200
|
::
|
|
195
201
|
|
|
@@ -201,7 +207,7 @@ function clearError (options?: { redirect?: string }): Promise<void>
|
|
|
201
207
|
|
|
202
208
|
This function will clear the currently handled Nuxt error. It also takes an optional path to redirect to (for example, if you want to navigate to a 'safe' page).
|
|
203
209
|
|
|
204
|
-
::read-more{to="/docs/api/utils/clear-error"}
|
|
210
|
+
::read-more{to="/docs/3.x/api/utils/clear-error"}
|
|
205
211
|
Read more about `clearError` util.
|
|
206
212
|
::
|
|
207
213
|
|
|
@@ -232,4 +238,4 @@ If you navigate to another route, the error will be cleared automatically.
|
|
|
232
238
|
</template>
|
|
233
239
|
```
|
|
234
240
|
|
|
235
|
-
:link-example{to="/docs/examples/advanced/error-handling"}
|
|
241
|
+
:link-example{to="/docs/3.x/examples/advanced/error-handling"}
|
|
@@ -4,7 +4,7 @@ description: Build full-stack applications with Nuxt's server framework. You can
|
|
|
4
4
|
navigation.icon: i-lucide-pc-case
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
:read-more{to="/docs/
|
|
7
|
+
:read-more{to="/docs/3.x/directory-structure/server"}
|
|
8
8
|
|
|
9
9
|
## Powered by Nitro
|
|
10
10
|
|
|
@@ -38,7 +38,7 @@ And you can directly return `text`, `json`, `html` or even a `stream`.
|
|
|
38
38
|
|
|
39
39
|
Out-of-the-box, it supports **hot module replacement** and **auto-import** like the other parts of your Nuxt application.
|
|
40
40
|
|
|
41
|
-
:read-more{to="/docs/
|
|
41
|
+
:read-more{to="/docs/3.x/directory-structure/server"}
|
|
42
42
|
|
|
43
43
|
## Universal Deployment
|
|
44
44
|
|
|
@@ -49,17 +49,17 @@ Nitro offers the ability to deploy your Nuxt app anywhere, from a bare metal ser
|
|
|
49
49
|
There are more than 15 presets to build your Nuxt app for different cloud providers and servers, including:
|
|
50
50
|
|
|
51
51
|
- [Cloudflare Workers](https://workers.cloudflare.com)
|
|
52
|
-
- [Netlify Functions](https://www.netlify.com/
|
|
53
|
-
- [Vercel
|
|
52
|
+
- [Netlify Functions](https://www.netlify.com/platform/core/functions/)
|
|
53
|
+
- [Vercel Cloud](https://vercel.com/home)
|
|
54
54
|
|
|
55
55
|
Or for other runtimes:
|
|
56
56
|
|
|
57
57
|
::card-group
|
|
58
|
-
:card{icon="i-logos-deno" title="Deno" to="https://deno.
|
|
59
|
-
:card{icon="i-logos-bun" title="Bun" to="https://bun.
|
|
58
|
+
:card{icon="i-logos-deno" title="Deno" to="https://deno.com" target="_blank"}
|
|
59
|
+
:card{icon="i-logos-bun" title="Bun" to="https://bun.com" target="_blank"}
|
|
60
60
|
::
|
|
61
61
|
|
|
62
|
-
:read-more{to="/docs/getting-started/deployment"}
|
|
62
|
+
:read-more{to="/docs/3.x/getting-started/deployment"}
|
|
63
63
|
|
|
64
64
|
## Hybrid Rendering
|
|
65
65
|
|
|
@@ -81,7 +81,7 @@ export default defineNuxtConfig({
|
|
|
81
81
|
})
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
::read-more{to="/docs/guide/concepts/rendering#hybrid-rendering"}
|
|
84
|
+
::read-more{to="/docs/3.x/guide/concepts/rendering#hybrid-rendering"}
|
|
85
85
|
Learn about all available route rules are available to customize the rendering mode of your routes.
|
|
86
86
|
::
|
|
87
87
|
|
|
@@ -91,4 +91,4 @@ Some route rules (`appMiddleware`, `redirect` and `prerender`) also affect clien
|
|
|
91
91
|
|
|
92
92
|
Nitro is used to build the app for server side rendering, as well as pre-rendering.
|
|
93
93
|
|
|
94
|
-
:read-more{to="/docs/guide/concepts/rendering"}
|
|
94
|
+
:read-more{to="/docs/3.x/guide/concepts/rendering"}
|
|
@@ -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
|
|
|
@@ -76,38 +80,66 @@ export default defineNuxtConfig({
|
|
|
76
80
|
|
|
77
81
|
::
|
|
78
82
|
|
|
79
|
-
Nuxt uses [unjs/c12](https://
|
|
83
|
+
Nuxt uses [unjs/c12](https://github.com/unjs/c12) and [unjs/giget](https://github.com/unjs/giget) for extending remote layers. Check the documentation for more information and all available options.
|
|
80
84
|
|
|
81
85
|
## Layer Priority
|
|
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
|
+
components/Button.vue # Base button style
|
|
105
|
+
2.theme/
|
|
106
|
+
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 `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,9 +151,11 @@ 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/3.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
|
-
::read-more{to="/docs/guide/going-further/layers"}
|
|
158
|
+
::read-more{to="/docs/3.x/guide/going-further/layers"}
|
|
125
159
|
Read more about layers in the **Layer Author Guide**.
|
|
126
160
|
::
|
|
127
161
|
|
|
@@ -6,7 +6,7 @@ navigation.icon: i-lucide-file-code-2
|
|
|
6
6
|
|
|
7
7
|
Nuxt allows for select pages from your application to be rendered at build time. Nuxt will serve the prebuilt pages when requested instead of generating them on the fly.
|
|
8
8
|
|
|
9
|
-
:read-more{title="Nuxt rendering modes" to="/docs/guide/concepts/rendering"}
|
|
9
|
+
:read-more{title="Nuxt rendering modes" to="/docs/3.x/guide/concepts/rendering"}
|
|
10
10
|
|
|
11
11
|
## Crawl-based Pre-rendering
|
|
12
12
|
|
|
@@ -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`.
|
|
@@ -45,7 +49,15 @@ Working of the Nitro crawler:
|
|
|
45
49
|
|
|
46
50
|
This is important to understand since pages that are not linked to a discoverable page can't be pre-rendered automatically.
|
|
47
51
|
|
|
48
|
-
|
|
52
|
+
### Payload Extraction
|
|
53
|
+
|
|
54
|
+
Nuxt generates `_payload.json` alongside HTML for:
|
|
55
|
+
- Prerendered routes (at build time)
|
|
56
|
+
- ISR/SWR routes (on first request)
|
|
57
|
+
|
|
58
|
+
Payloads contain serialized data from `useAsyncData` and `useFetch`. Client-side navigation loads these cached payloads instead of re-fetching data. Configure dynamic routes like `pages/[...slug].vue` with route rules: `'/**': { isr: true }`.
|
|
59
|
+
|
|
60
|
+
::read-more{to="/docs/3.x/api/commands/generate#nuxt-generate"}
|
|
49
61
|
Read more about the `nuxt generate` command.
|
|
50
62
|
::
|
|
51
63
|
|
|
@@ -54,6 +66,7 @@ Read more about the `nuxt generate` command.
|
|
|
54
66
|
You can manually specify routes that [Nitro](/docs/3.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
67
|
|
|
56
68
|
```ts twoslash [nuxt.config.ts]
|
|
69
|
+
// @errors: 2353
|
|
57
70
|
export default defineNuxtConfig({
|
|
58
71
|
nitro: {
|
|
59
72
|
prerender: {
|
|
@@ -67,6 +80,7 @@ export default defineNuxtConfig({
|
|
|
67
80
|
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
81
|
|
|
69
82
|
```ts twoslash [nuxt.config.ts]
|
|
83
|
+
// @errors: 2353
|
|
70
84
|
export default defineNuxtConfig({
|
|
71
85
|
nitro: {
|
|
72
86
|
prerender: {
|
|
@@ -105,7 +119,7 @@ Read more about Nitro's `routeRules` configuration.
|
|
|
105
119
|
|
|
106
120
|
As a shorthand, you can also configure this in a page file using [`defineRouteRules`](/docs/3.x/api/utils/define-route-rules).
|
|
107
121
|
|
|
108
|
-
::read-more{to="/docs/guide/going-further/experimental-features#inlinerouterules" icon="i-lucide-star"}
|
|
122
|
+
::read-more{to="/docs/3.x/guide/going-further/experimental-features#inlinerouterules" icon="i-lucide-star"}
|
|
109
123
|
This feature is experimental and in order to use it you must enable the `experimental.inlineRouteRules` option in your `nuxt.config`.
|
|
110
124
|
::
|
|
111
125
|
|
|
@@ -154,7 +168,7 @@ prerenderRoutes('/api/content/article/my-article')
|
|
|
154
168
|
</template>
|
|
155
169
|
```
|
|
156
170
|
|
|
157
|
-
:read-more{title="prerenderRoutes" to="/docs/api/utils/prerender-routes"}
|
|
171
|
+
:read-more{title="prerenderRoutes" to="/docs/3.x/api/utils/prerender-routes"}
|
|
158
172
|
|
|
159
173
|
### `prerender:routes` Nuxt hook
|
|
160
174
|
|
|
@@ -73,7 +73,7 @@ There are two ways to deploy a Nuxt application to any static hosting services:
|
|
|
73
73
|
- Static site generation (SSG) with `ssr: true` pre-renders routes of your application at build time. (This is the default behavior when running `nuxt generate`.) It will also generate `/200.html` and `/404.html` single-page app fallback pages, which can render dynamic routes or 404 errors on the client (though you may need to configure this on your static host).
|
|
74
74
|
- Alternatively, you can prerender your site with `ssr: false` (static single-page app). This will produce HTML pages with an empty `<div id="__nuxt"></div>` where your Vue app would normally be rendered. You will lose many SEO benefits of prerendering your site, so it is suggested instead to use [`<ClientOnly>`](/docs/3.x/api/components/client-only) to wrap the portions of your site that cannot be server rendered (if any).
|
|
75
75
|
|
|
76
|
-
:read-more{title="Nuxt prerendering" to="/docs/getting-started/prerendering"}
|
|
76
|
+
:read-more{title="Nuxt prerendering" to="/docs/3.x/getting-started/prerendering"}
|
|
77
77
|
|
|
78
78
|
### Client-side Only Rendering
|
|
79
79
|
|
|
@@ -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/3.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/3.x/guide/
|
|
8
|
+
If you are a module author, you can find more specific information in the [Module Author's guide](/docs/3.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
|
},
|
|
@@ -265,7 +272,7 @@ it('can also mount an app', async () => {
|
|
|
265
272
|
|
|
266
273
|
`renderSuspended` allows you to render any Vue component within the Nuxt environment using `@testing-library/vue`, allowing async setup and access to injections from your Nuxt plugins.
|
|
267
274
|
|
|
268
|
-
This should be used together with utilities from Testing Library, e.g. `screen` and `fireEvent`. Install [@testing-library/vue](https://testing-library.com/docs/vue-testing-library/intro) in your project to use these.
|
|
275
|
+
This should be used together with utilities from Testing Library, e.g. `screen` and `fireEvent`. Install [@testing-library/vue](https://testing-library.com/docs/vue-testing-library/intro/) in your project to use these.
|
|
269
276
|
|
|
270
277
|
Additionally, Testing Library also relies on testing globals for cleanup. You should turn these on in your [Vitest config](https://vitest.dev/config/globals).
|
|
271
278
|
|
|
@@ -330,10 +337,10 @@ mockNuxtImport('useStorage', () => {
|
|
|
330
337
|
```
|
|
331
338
|
|
|
332
339
|
::note
|
|
333
|
-
`mockNuxtImport` can only be used once per mocked import per test file. It is actually a macro that gets transformed to `vi.mock` and `vi.mock` is hoisted, as described [in the Vitest docs](https://vitest.dev/api/vi
|
|
340
|
+
`mockNuxtImport` can only be used once per mocked import per test file. It is actually a macro that gets transformed to `vi.mock` and `vi.mock` is hoisted, as described [in the Vitest docs](https://vitest.dev/api/vi#vi-mock).
|
|
334
341
|
::
|
|
335
342
|
|
|
336
|
-
If you need to mock a Nuxt import and provide different implementations between tests, you can do it by creating and exposing your mocks using [`vi.hoisted`](https://vitest.dev/api/vi
|
|
343
|
+
If you need to mock a Nuxt import and provide different implementations between tests, you can do it by creating and exposing your mocks using [`vi.hoisted`](https://vitest.dev/api/vi#vi-hoisted), and then use those mocks in `mockNuxtImport`. You then have access to the mocked imports, and can change the implementation between tests. Be careful to [restore mocks](https://vitest.dev/api/mock#mockrestore) before or after each test to undo mock state changes between runs.
|
|
337
344
|
|
|
338
345
|
```ts twoslash
|
|
339
346
|
import { vi } from 'vitest'
|
|
@@ -624,7 +631,7 @@ Please use the options below for the `setup` method.
|
|
|
624
631
|
- Type: `number | undefined`
|
|
625
632
|
- Default: `undefined`
|
|
626
633
|
|
|
627
|
-
- `host`: If provided, a URL to use as the test target instead of building and running a new server. Useful for running "real" end-to-end tests against a deployed version of your application, or against an already running local server (which may provide a significant reduction in test execution timings). See the [target host end-to-end example below](#target-host-end-to-end-example).
|
|
634
|
+
- `host`: If provided, a URL to use as the test target instead of building and running a new server. Useful for running "real" end-to-end tests against a deployed version of your application, or against an already running local server (which may provide a significant reduction in test execution timings). See the [target host end-to-end example below](/docs/3.x/getting-started/testing#target-host-end-to-end-example).
|
|
628
635
|
- Type: `string`
|
|
629
636
|
- Default: `undefined`
|
|
630
637
|
|
|
@@ -647,7 +654,7 @@ For local development or automated deploy pipelines, testing against a separate
|
|
|
647
654
|
|
|
648
655
|
To utilize a separate target host for end-to-end tests, simply provide the `host` property of the `setup` function with the desired URL.
|
|
649
656
|
|
|
650
|
-
```ts
|
|
657
|
+
```ts
|
|
651
658
|
import { createPage, setup } from '@nuxt/test-utils/e2e'
|
|
652
659
|
import { describe, expect, it } from 'vitest'
|
|
653
660
|
|
|
@@ -730,6 +737,9 @@ pnpm add -D @playwright/test @nuxt/test-utils
|
|
|
730
737
|
```bash [bun]
|
|
731
738
|
bun add --dev @playwright/test @nuxt/test-utils
|
|
732
739
|
```
|
|
740
|
+
```bash [deno]
|
|
741
|
+
deno add --dev npm:@playwright/test npm:@nuxt/test-utils
|
|
742
|
+
```
|
|
733
743
|
::
|
|
734
744
|
|
|
735
745
|
You can provide global Nuxt configuration, with the same configuration details as the `setup()` function mentioned earlier in this section.
|