@nuxt/docs 0.0.0 → 3.17.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/.navigation.yml +2 -0
- package/1.getting-started/.navigation.yml +3 -0
- package/1.getting-started/01.introduction.md +81 -0
- package/1.getting-started/02.installation.md +109 -0
- package/1.getting-started/03.configuration.md +226 -0
- package/1.getting-started/04.views.md +163 -0
- package/1.getting-started/05.assets.md +48 -0
- package/1.getting-started/06.styling.md +565 -0
- package/1.getting-started/07.routing.md +149 -0
- package/1.getting-started/08.seo-meta.md +360 -0
- package/1.getting-started/09.transitions.md +473 -0
- package/1.getting-started/10.data-fetching.md +795 -0
- package/1.getting-started/11.state-management.md +223 -0
- package/1.getting-started/12.error-handling.md +233 -0
- package/1.getting-started/13.server.md +94 -0
- package/1.getting-started/14.layers.md +92 -0
- package/1.getting-started/15.prerendering.md +194 -0
- package/1.getting-started/16.deployment.md +130 -0
- package/1.getting-started/17.testing.md +728 -0
- package/1.getting-started/18.upgrade.md +997 -0
- package/2.guide/.navigation.yml +2 -0
- package/2.guide/0.index.md +22 -0
- package/2.guide/1.concepts/.navigation.yml +3 -0
- package/2.guide/1.concepts/1.auto-imports.md +205 -0
- package/2.guide/1.concepts/10.nuxt-lifecycle.md +141 -0
- package/2.guide/1.concepts/2.vuejs-development.md +103 -0
- package/2.guide/1.concepts/3.rendering.md +255 -0
- package/2.guide/1.concepts/4.server-engine.md +62 -0
- package/2.guide/1.concepts/5.modules.md +48 -0
- package/2.guide/1.concepts/7.esm.md +299 -0
- package/2.guide/1.concepts/8.typescript.md +97 -0
- package/2.guide/1.concepts/9.code-style.md +22 -0
- package/2.guide/2.directory-structure/.navigation.yml +3 -0
- package/2.guide/2.directory-structure/0.nuxt.md +20 -0
- package/2.guide/2.directory-structure/0.output.md +18 -0
- package/2.guide/2.directory-structure/1.assets.md +16 -0
- package/2.guide/2.directory-structure/1.components.md +608 -0
- package/2.guide/2.directory-structure/1.composables.md +121 -0
- package/2.guide/2.directory-structure/1.content.md +64 -0
- package/2.guide/2.directory-structure/1.layouts.md +180 -0
- package/2.guide/2.directory-structure/1.middleware.md +209 -0
- package/2.guide/2.directory-structure/1.modules.md +66 -0
- package/2.guide/2.directory-structure/1.node_modules.md +12 -0
- package/2.guide/2.directory-structure/1.pages.md +440 -0
- package/2.guide/2.directory-structure/1.plugins.md +299 -0
- package/2.guide/2.directory-structure/1.public.md +27 -0
- package/2.guide/2.directory-structure/1.server.md +546 -0
- package/2.guide/2.directory-structure/1.shared.md +104 -0
- package/2.guide/2.directory-structure/1.utils.md +49 -0
- package/2.guide/2.directory-structure/2.env.md +75 -0
- package/2.guide/2.directory-structure/2.gitignore.md +37 -0
- package/2.guide/2.directory-structure/2.nuxtignore.md +36 -0
- package/2.guide/2.directory-structure/2.nuxtrc.md +50 -0
- package/2.guide/2.directory-structure/3.app-config.md +177 -0
- package/2.guide/2.directory-structure/3.app.md +72 -0
- package/2.guide/2.directory-structure/3.error.md +55 -0
- package/2.guide/2.directory-structure/3.nuxt-config.md +34 -0
- package/2.guide/2.directory-structure/3.package.md +32 -0
- package/2.guide/2.directory-structure/3.tsconfig.md +24 -0
- package/2.guide/3.going-further/.navigation.yml +3 -0
- package/2.guide/3.going-further/1.experimental-features.md +689 -0
- package/2.guide/3.going-further/1.features.md +103 -0
- package/2.guide/3.going-further/1.internals.md +81 -0
- package/2.guide/3.going-further/10.runtime-config.md +174 -0
- package/2.guide/3.going-further/11.nightly-release-channel.md +68 -0
- package/2.guide/3.going-further/2.hooks.md +98 -0
- package/2.guide/3.going-further/3.modules.md +811 -0
- package/2.guide/3.going-further/4.kit.md +51 -0
- package/2.guide/3.going-further/6.nuxt-app.md +64 -0
- package/2.guide/3.going-further/7.layers.md +227 -0
- package/2.guide/3.going-further/9.debugging.md +115 -0
- package/2.guide/3.going-further/index.md +4 -0
- package/2.guide/4.recipes/.navigation.yml +3 -0
- package/2.guide/4.recipes/1.custom-routing.md +181 -0
- package/2.guide/4.recipes/2.vite-plugin.md +65 -0
- package/2.guide/4.recipes/3.custom-usefetch.md +125 -0
- package/2.guide/4.recipes/4.sessions-and-authentication.md +203 -0
- package/3.api/.navigation.yml +3 -0
- package/3.api/1.components/.navigation.yml +3 -0
- package/3.api/1.components/1.client-only.md +76 -0
- package/3.api/1.components/1.dev-only.md +51 -0
- package/3.api/1.components/1.nuxt-client-fallback.md +80 -0
- package/3.api/1.components/10.nuxt-picture.md +27 -0
- package/3.api/1.components/11.teleports.md +40 -0
- package/3.api/1.components/12.nuxt-route-announcer.md +56 -0
- package/3.api/1.components/13.nuxt-time.md +173 -0
- package/3.api/1.components/2.nuxt-page.md +154 -0
- package/3.api/1.components/3.nuxt-layout.md +156 -0
- package/3.api/1.components/4.nuxt-link.md +322 -0
- package/3.api/1.components/5.nuxt-loading-indicator.md +50 -0
- package/3.api/1.components/6.nuxt-error-boundary.md +65 -0
- package/3.api/1.components/7.nuxt-welcome.md +25 -0
- package/3.api/1.components/8.nuxt-island.md +70 -0
- package/3.api/1.components/9.nuxt-img.md +43 -0
- package/3.api/2.composables/.navigation.yml +3 -0
- package/3.api/2.composables/on-prehydrate.md +60 -0
- package/3.api/2.composables/use-app-config.md +19 -0
- package/3.api/2.composables/use-async-data.md +212 -0
- package/3.api/2.composables/use-cookie.md +233 -0
- package/3.api/2.composables/use-error.md +32 -0
- package/3.api/2.composables/use-fetch.md +217 -0
- package/3.api/2.composables/use-head-safe.md +55 -0
- package/3.api/2.composables/use-head.md +69 -0
- package/3.api/2.composables/use-hydration.md +68 -0
- package/3.api/2.composables/use-lazy-async-data.md +47 -0
- package/3.api/2.composables/use-lazy-fetch.md +55 -0
- package/3.api/2.composables/use-loading-indicator.md +77 -0
- package/3.api/2.composables/use-nuxt-app.md +294 -0
- package/3.api/2.composables/use-nuxt-data.md +112 -0
- package/3.api/2.composables/use-preview-mode.md +118 -0
- package/3.api/2.composables/use-request-event.md +23 -0
- package/3.api/2.composables/use-request-fetch.md +52 -0
- package/3.api/2.composables/use-request-header.md +34 -0
- package/3.api/2.composables/use-request-headers.md +37 -0
- package/3.api/2.composables/use-request-url.md +41 -0
- package/3.api/2.composables/use-response-header.md +48 -0
- package/3.api/2.composables/use-route-announcer.md +60 -0
- package/3.api/2.composables/use-route.md +52 -0
- package/3.api/2.composables/use-router.md +92 -0
- package/3.api/2.composables/use-runtime-config.md +142 -0
- package/3.api/2.composables/use-runtime-hook.md +43 -0
- package/3.api/2.composables/use-seo-meta.md +80 -0
- package/3.api/2.composables/use-server-seo-meta.md +27 -0
- package/3.api/2.composables/use-state.md +48 -0
- package/3.api/3.utils/$fetch.md +98 -0
- package/3.api/3.utils/.navigation.yml +3 -0
- package/3.api/3.utils/abort-navigation.md +73 -0
- package/3.api/3.utils/add-route-middleware.md +88 -0
- package/3.api/3.utils/call-once.md +92 -0
- package/3.api/3.utils/clear-error.md +29 -0
- package/3.api/3.utils/clear-nuxt-data.md +23 -0
- package/3.api/3.utils/clear-nuxt-state.md +23 -0
- package/3.api/3.utils/create-error.md +55 -0
- package/3.api/3.utils/define-nuxt-component.md +53 -0
- package/3.api/3.utils/define-nuxt-route-middleware.md +67 -0
- package/3.api/3.utils/define-page-meta.md +234 -0
- package/3.api/3.utils/define-route-rules.md +52 -0
- package/3.api/3.utils/navigate-to.md +230 -0
- package/3.api/3.utils/on-before-route-leave.md +11 -0
- package/3.api/3.utils/on-before-route-update.md +11 -0
- package/3.api/3.utils/on-nuxt-ready.md +25 -0
- package/3.api/3.utils/prefetch-components.md +28 -0
- package/3.api/3.utils/preload-components.md +23 -0
- package/3.api/3.utils/preload-route-components.md +41 -0
- package/3.api/3.utils/prerender-routes.md +46 -0
- package/3.api/3.utils/refresh-cookie.md +46 -0
- package/3.api/3.utils/refresh-nuxt-data.md +91 -0
- package/3.api/3.utils/reload-nuxt-app.md +74 -0
- package/3.api/3.utils/set-page-layout.md +24 -0
- package/3.api/3.utils/set-response-status.md +36 -0
- package/3.api/3.utils/show-error.md +31 -0
- package/3.api/3.utils/update-app-config.md +27 -0
- package/3.api/4.commands/.navigation.yml +3 -0
- package/3.api/4.commands/add.md +112 -0
- package/3.api/4.commands/analyze.md +41 -0
- package/3.api/4.commands/build-module.md +42 -0
- package/3.api/4.commands/build.md +46 -0
- package/3.api/4.commands/cleanup.md +38 -0
- package/3.api/4.commands/dev.md +59 -0
- package/3.api/4.commands/devtools.md +38 -0
- package/3.api/4.commands/generate.md +41 -0
- package/3.api/4.commands/info.md +33 -0
- package/3.api/4.commands/init.md +46 -0
- package/3.api/4.commands/module.md +84 -0
- package/3.api/4.commands/prepare.md +36 -0
- package/3.api/4.commands/preview.md +43 -0
- package/3.api/4.commands/typecheck.md +42 -0
- package/3.api/4.commands/upgrade.md +37 -0
- package/3.api/5.kit/.navigation.yml +3 -0
- package/3.api/5.kit/1.modules.md +172 -0
- package/3.api/5.kit/10.runtime-config.md +27 -0
- package/3.api/5.kit/10.templates.md +283 -0
- package/3.api/5.kit/11.nitro.md +409 -0
- package/3.api/5.kit/12.resolving.md +268 -0
- package/3.api/5.kit/13.logging.md +65 -0
- package/3.api/5.kit/14.builder.md +491 -0
- package/3.api/5.kit/15.examples.md +41 -0
- package/3.api/5.kit/2.programmatic.md +125 -0
- package/3.api/5.kit/3.compatibility.md +230 -0
- package/3.api/5.kit/4.autoimports.md +144 -0
- package/3.api/5.kit/5.components.md +127 -0
- package/3.api/5.kit/6.context.md +130 -0
- package/3.api/5.kit/7.pages.md +295 -0
- package/3.api/5.kit/8.layout.md +80 -0
- package/3.api/5.kit/9.plugins.md +263 -0
- package/3.api/6.advanced/.navigation.yml +1 -0
- package/3.api/6.advanced/1.hooks.md +105 -0
- package/3.api/6.advanced/2.import-meta.md +60 -0
- package/3.api/6.nuxt-config.md +12 -0
- package/3.api/index.md +31 -0
- package/5.community/.navigation.yml +3 -0
- package/5.community/2.getting-help.md +48 -0
- package/5.community/3.reporting-bugs.md +50 -0
- package/5.community/4.contribution.md +205 -0
- package/5.community/5.framework-contribution.md +142 -0
- package/5.community/6.roadmap.md +79 -0
- package/5.community/7.changelog.md +92 -0
- package/6.bridge/.navigation.yml +3 -0
- package/6.bridge/1.overview.md +137 -0
- package/6.bridge/10.configuration.md +96 -0
- package/6.bridge/2.typescript.md +46 -0
- package/6.bridge/3.bridge-composition-api.md +132 -0
- package/6.bridge/4.plugins-and-middleware.md +65 -0
- package/6.bridge/5.nuxt3-compatible-api.md +204 -0
- package/6.bridge/6.meta.md +117 -0
- package/6.bridge/7.runtime-config.md +38 -0
- package/6.bridge/8.nitro.md +102 -0
- package/6.bridge/9.vite.md +37 -0
- package/7.migration/.navigation.yml +3 -0
- package/7.migration/1.overview.md +24 -0
- package/7.migration/10.bundling.md +28 -0
- package/7.migration/11.server.md +17 -0
- package/7.migration/2.configuration.md +240 -0
- package/7.migration/20.module-authors.md +94 -0
- package/7.migration/3.auto-imports.md +18 -0
- package/7.migration/4.meta.md +127 -0
- package/7.migration/5.plugins-and-middleware.md +80 -0
- package/7.migration/6.pages-and-layouts.md +233 -0
- package/7.migration/7.component-options.md +156 -0
- package/7.migration/8.runtime-config.md +58 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/package.json +16 -4
- package/dist/.gitkeep +0 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'Nuxt Guide'
|
|
3
|
+
titleTemplate: '%s'
|
|
4
|
+
description: 'Learn how Nuxt works with in-depth guides.'
|
|
5
|
+
navigation: false
|
|
6
|
+
surround: false
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
::card-group{class="sm:grid-cols-1"}
|
|
10
|
+
::card{icon="i-lucide-medal" title="Key Concepts" to="/docs/guide/concepts"}
|
|
11
|
+
Discover the main concepts behind Nuxt, from auto-import, hybrid rendering to its TypeScript support.
|
|
12
|
+
::
|
|
13
|
+
::card{icon="i-lucide-folders" title="Directory Structure" to="/docs/guide/directory-structure"}
|
|
14
|
+
Learn about Nuxt directory structure and what benefits each directory or file offers.
|
|
15
|
+
::
|
|
16
|
+
::card{icon="i-lucide-star" title="Going Further" to="/docs/guide/going-further"}
|
|
17
|
+
Master Nuxt with advanced concepts like experimental features, hooks, modules, and more.
|
|
18
|
+
::
|
|
19
|
+
::card{icon="i-lucide-book-open" title="Recipes" to="/docs/guide/recipes"}
|
|
20
|
+
Find solutions to common problems and learn how to implement them in your Nuxt project.
|
|
21
|
+
::
|
|
22
|
+
::
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Auto-imports
|
|
3
|
+
description: "Nuxt auto-imports components, composables, helper functions and Vue APIs."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Nuxt auto-imports components, composables and [Vue.js APIs](https://vuejs.org/api) to use across your application without explicitly importing them.
|
|
7
|
+
|
|
8
|
+
```vue twoslash [app.vue]
|
|
9
|
+
<script setup lang="ts">
|
|
10
|
+
const count = ref(1) // ref is auto-imported
|
|
11
|
+
</script>
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Thanks to its opinionated directory structure, Nuxt can auto-import your [`components/`](/docs/guide/directory-structure/components), [`composables/`](/docs/guide/directory-structure/composables) and [`utils/`](/docs/guide/directory-structure/utils).
|
|
15
|
+
|
|
16
|
+
Contrary to a classic global declaration, Nuxt preserves typings, IDEs completions and hints, and **only includes what is used in your production code**.
|
|
17
|
+
|
|
18
|
+
::note
|
|
19
|
+
In the docs, every function that is not explicitly imported is auto-imported by Nuxt and can be used as-is in your code. You can find a reference for auto-imported components, composables and utilities in the [API section](/docs/api).
|
|
20
|
+
::
|
|
21
|
+
|
|
22
|
+
::note
|
|
23
|
+
In the [`server`](/docs/guide/directory-structure/server) directory, Nuxt auto-imports exported functions and variables from `server/utils/`.
|
|
24
|
+
::
|
|
25
|
+
|
|
26
|
+
::note
|
|
27
|
+
You can also auto-import functions exported from custom folders or third-party packages by configuring the [`imports`](/docs/api/nuxt-config#imports) section of your `nuxt.config` file.
|
|
28
|
+
::
|
|
29
|
+
|
|
30
|
+
## Built-in Auto-imports
|
|
31
|
+
|
|
32
|
+
Nuxt auto-imports functions and composables to perform [data fetching](/docs/getting-started/data-fetching), get access to the [app context](/docs/api/composables/use-nuxt-app) and [runtime config](/docs/guide/going-further/runtime-config), manage [state](/docs/getting-started/state-management) or define components and plugins.
|
|
33
|
+
|
|
34
|
+
```vue twoslash
|
|
35
|
+
<script setup lang="ts">
|
|
36
|
+
/* useFetch() is auto-imported */
|
|
37
|
+
const { data, refresh, status } = await useFetch('/api/hello')
|
|
38
|
+
</script>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Vue exposes Reactivity APIs like `ref` or `computed`, as well as lifecycle hooks and helpers that are auto-imported by Nuxt.
|
|
42
|
+
|
|
43
|
+
```vue twoslash
|
|
44
|
+
<script setup lang="ts">
|
|
45
|
+
/* ref() and computed() are auto-imported */
|
|
46
|
+
const count = ref(1)
|
|
47
|
+
const double = computed(() => count.value * 2)
|
|
48
|
+
</script>
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Vue and Nuxt Composables
|
|
52
|
+
|
|
53
|
+
<!-- TODO: move to separate page with https://github.com/nuxt/nuxt/issues/14723 and add more information -->
|
|
54
|
+
|
|
55
|
+
When you are using the built-in Composition API composables provided by Vue and Nuxt, be aware that many of them rely on being called in the right _context_.
|
|
56
|
+
|
|
57
|
+
During a component lifecycle, Vue tracks the temporary instance of the current component (and similarly, Nuxt tracks a temporary instance of `nuxtApp`) via a global variable, and then unsets it in same tick. This is essential when server rendering, both to avoid cross-request state pollution (leaking a shared reference between two users) and to avoid leakage between different components.
|
|
58
|
+
|
|
59
|
+
That means that (with very few exceptions) you cannot use them outside a Nuxt plugin, Nuxt route middleware or Vue setup function. On top of that, you must use them synchronously - that is, you cannot use `await` before calling a composable, except within `<script setup>` blocks, within the setup function of a component declared with `defineNuxtComponent`, in `defineNuxtPlugin` or in `defineNuxtRouteMiddleware`, where we perform a transform to keep the synchronous context even after the `await`.
|
|
60
|
+
|
|
61
|
+
If you get an error message like `Nuxt instance is unavailable` then it probably means you are calling a Nuxt composable in the wrong place in the Vue or Nuxt lifecycle.
|
|
62
|
+
|
|
63
|
+
:video-accordion{title="Watch a video from Alexander Lichter about avoiding the 'Nuxt instance is unavailable' error" videoId="ofuKRZLtOdY"}
|
|
64
|
+
|
|
65
|
+
::tip
|
|
66
|
+
When using a composable that requires the Nuxt context inside a non-SFC component, you need to wrap your component with `defineNuxtComponent` instead of `defineComponent`
|
|
67
|
+
::
|
|
68
|
+
|
|
69
|
+
::read-more{to="/docs/guide/going-further/experimental-features#asynccontext" icon="i-lucide-star"}
|
|
70
|
+
Checkout the `asyncContext` experimental feature to use Nuxt composables in async functions.
|
|
71
|
+
::
|
|
72
|
+
|
|
73
|
+
::read-more{to="https://github.com/nuxt/nuxt/issues/14269#issuecomment-1397352832" target="_blank"}
|
|
74
|
+
See the full explanation in this GitHub comment.
|
|
75
|
+
::
|
|
76
|
+
|
|
77
|
+
**Example of breaking code:**
|
|
78
|
+
|
|
79
|
+
```ts twoslash [composables/example.ts]
|
|
80
|
+
// trying to access runtime config outside a composable
|
|
81
|
+
const config = useRuntimeConfig()
|
|
82
|
+
|
|
83
|
+
export const useMyComposable = () => {
|
|
84
|
+
// accessing runtime config here
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Example of working code:**
|
|
89
|
+
|
|
90
|
+
```ts twoslash [composables/example.ts]
|
|
91
|
+
export const useMyComposable = () => {
|
|
92
|
+
// Because your composable is called in the right place in the lifecycle,
|
|
93
|
+
// useRuntimeConfig will work here
|
|
94
|
+
const config = useRuntimeConfig()
|
|
95
|
+
|
|
96
|
+
// ...
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Directory-based Auto-imports
|
|
101
|
+
|
|
102
|
+
Nuxt directly auto-imports files created in defined directories:
|
|
103
|
+
|
|
104
|
+
- `components/` for [Vue components](/docs/guide/directory-structure/components).
|
|
105
|
+
- `composables/` for [Vue composables](/docs/guide/directory-structure/composables).
|
|
106
|
+
- `utils/` for helper functions and other utilities.
|
|
107
|
+
|
|
108
|
+
:link-example{to="/docs/examples/features/auto-imports"}
|
|
109
|
+
|
|
110
|
+
::warning
|
|
111
|
+
**Auto-imported `ref` and `computed` won't be unwrapped in a component `<template>`.** :br
|
|
112
|
+
This is due to how Vue works with refs that aren't top-level to the template. You can read more about it [in the Vue documentation](https://vuejs.org/guide/essentials/reactivity-fundamentals.html#caveat-when-unwrapping-in-templates).
|
|
113
|
+
::
|
|
114
|
+
|
|
115
|
+
### Explicit Imports
|
|
116
|
+
|
|
117
|
+
Nuxt exposes every auto-import with the `#imports` alias that can be used to make the import explicit if needed:
|
|
118
|
+
|
|
119
|
+
<!-- TODO:twoslash: Twoslash does not support tsconfig paths yet -->
|
|
120
|
+
|
|
121
|
+
```vue
|
|
122
|
+
<script setup lang="ts">
|
|
123
|
+
import { ref, computed } from '#imports'
|
|
124
|
+
|
|
125
|
+
const count = ref(1)
|
|
126
|
+
const double = computed(() => count.value * 2)
|
|
127
|
+
</script>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Disabling Auto-imports
|
|
131
|
+
|
|
132
|
+
If you want to disable auto-importing composables and utilities, you can set `imports.autoImport` to `false` in the `nuxt.config` file.
|
|
133
|
+
|
|
134
|
+
```ts twoslash [nuxt.config.ts]
|
|
135
|
+
export default defineNuxtConfig({
|
|
136
|
+
imports: {
|
|
137
|
+
autoImport: false
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
This will disable auto-imports completely but it's still possible to use [explicit imports](#explicit-imports) from `#imports`.
|
|
143
|
+
|
|
144
|
+
### Partially Disabling Auto-imports
|
|
145
|
+
|
|
146
|
+
If you want framework-specific functions like `ref` to remain auto-imported but wish to disable auto-imports for your own code (e.g., custom composables), you can set the `imports.scan` option to `false` in your `nuxt.config.ts` file:
|
|
147
|
+
|
|
148
|
+
```ts
|
|
149
|
+
export default defineNuxtConfig({
|
|
150
|
+
imports: {
|
|
151
|
+
scan: false
|
|
152
|
+
}
|
|
153
|
+
})
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
With this configuration:
|
|
157
|
+
- Framework functions like `ref`, `computed`, or `watch` will still work without needing manual imports.
|
|
158
|
+
- Custom code, such as composables, will need to be manually imported in your files.
|
|
159
|
+
|
|
160
|
+
::warning
|
|
161
|
+
**Caution:** This setup has certain limitations:
|
|
162
|
+
- If you structure your project with layers, you will need to explicitly import the composables from each layer, rather than relying on auto-imports.
|
|
163
|
+
- This breaks the layer system’s override feature. If you use `imports.scan: false`, ensure you understand this side-effect and adjust your architecture accordingly.
|
|
164
|
+
::
|
|
165
|
+
|
|
166
|
+
## Auto-imported Components
|
|
167
|
+
|
|
168
|
+
Nuxt also automatically imports components from your `~/components` directory, although this is configured separately from auto-importing composables and utility functions.
|
|
169
|
+
|
|
170
|
+
:read-more{to="/docs/guide/directory-structure/components"}
|
|
171
|
+
|
|
172
|
+
To disable auto-importing components from your own `~/components` directory, you can set `components.dirs` to an empty array (though note that this will not affect components added by modules).
|
|
173
|
+
|
|
174
|
+
```ts twoslash [nuxt.config.ts]
|
|
175
|
+
export default defineNuxtConfig({
|
|
176
|
+
components: {
|
|
177
|
+
dirs: []
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Auto-import from Third-Party Packages
|
|
183
|
+
|
|
184
|
+
Nuxt also allows auto-importing from third-party packages.
|
|
185
|
+
|
|
186
|
+
::tip
|
|
187
|
+
If you are using the Nuxt module for that package, it is likely that the module has already configured auto-imports for that package.
|
|
188
|
+
::
|
|
189
|
+
|
|
190
|
+
For example, you could enable the auto-import of the `useI18n` composable from the `vue-i18n` package like this:
|
|
191
|
+
|
|
192
|
+
```ts twoslash [nuxt.config.ts]
|
|
193
|
+
export default defineNuxtConfig({
|
|
194
|
+
imports: {
|
|
195
|
+
presets: [
|
|
196
|
+
{
|
|
197
|
+
from: 'vue-i18n',
|
|
198
|
+
imports: ['useI18n']
|
|
199
|
+
}
|
|
200
|
+
]
|
|
201
|
+
}
|
|
202
|
+
})
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
:video-accordion{title="Watch a video from Alexander Lichter on how to easily set up custom auto imports" videoId="FT2LQJ2NvVI"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'Nuxt Lifecycle'
|
|
3
|
+
description: "Understanding the lifecycle of Nuxt applications can help you gain deeper insights into how the framework operates, especially for both server-side and client-side rendering."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
The goal of this chapter is to provide a high-level overview of the different parts of the framework, their execution order, and how they work together.
|
|
7
|
+
|
|
8
|
+
## Server
|
|
9
|
+
|
|
10
|
+
On the server, the following steps are executed for every initial request to your application:
|
|
11
|
+
|
|
12
|
+
### Step 1: Setup Nitro Server and Nitro Plugins (Once)
|
|
13
|
+
|
|
14
|
+
Nuxt is powered by [Nitro](https://nitro.build/), a modern server engine.
|
|
15
|
+
|
|
16
|
+
When Nitro starts, it initializes and executes the plugins under the `/server/plugins` directory. These plugins can:
|
|
17
|
+
- Capture and handle application-wide errors.
|
|
18
|
+
- Register hooks that execute when Nitro shuts down.
|
|
19
|
+
- Register hooks for request lifecycle events, such as modifying responses.
|
|
20
|
+
|
|
21
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
22
|
+
Nitro plugins are executed only once when the server starts. In a serverless environment, the server boots on each incoming request, and so do the Nitro plugins. However, they are not awaited.
|
|
23
|
+
::
|
|
24
|
+
|
|
25
|
+
:read-more{to="/docs/guide/directory-structure/server#server-plugins"}
|
|
26
|
+
|
|
27
|
+
### Step 2: Nitro Server Middleware
|
|
28
|
+
|
|
29
|
+
After initializing the Nitro server, middleware under `server/middleware/` is executed for every request. Middleware can be used for tasks such as authentication, logging, or request transformation.
|
|
30
|
+
|
|
31
|
+
::warning
|
|
32
|
+
Returning a value from middleware will terminate the request and send the returned value as the response. This behavior should generally be avoided to ensure proper request handling!
|
|
33
|
+
::
|
|
34
|
+
|
|
35
|
+
:read-more{to="/docs/guide/directory-structure/server#server-middleware"}
|
|
36
|
+
|
|
37
|
+
### Step 3: Initialize Nuxt and Execute Nuxt App Plugins
|
|
38
|
+
|
|
39
|
+
The Vue and Nuxt instances are created first. Afterward, Nuxt executes its server plugins. This includes:
|
|
40
|
+
- Built-in plugins, such as Vue Router and `unhead`.
|
|
41
|
+
- Custom plugins located in the `plugins/` directory, including those without a suffix (e.g., `myPlugin.ts`) and those with the `.server` suffix (e.g., `myServerPlugin.server.ts`).
|
|
42
|
+
|
|
43
|
+
Plugins execute in a specific order and may have dependencies on one another. For more details, including execution order and parallelism, refer to the [Plugins documentation](/docs/guide/directory-structure/plugins).
|
|
44
|
+
|
|
45
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
46
|
+
After this step, Nuxt calls the [`app:created`](/docs/api/advanced/hooks#app-hooks-runtime) hook, which can be used to execute additional logic.
|
|
47
|
+
::
|
|
48
|
+
|
|
49
|
+
:read-more{to="/docs/guide/directory-structure/plugins"}
|
|
50
|
+
|
|
51
|
+
### Step 4: Route Validation
|
|
52
|
+
|
|
53
|
+
After initializing plugins and before executing middleware, Nuxt calls the `validate` method if it is defined in the `definePageMeta` function. The `validate` method, which can be synchronous or asynchronous, is often used to validate dynamic route parameters.
|
|
54
|
+
|
|
55
|
+
- The `validate` function should return `true` if the parameters are valid.
|
|
56
|
+
- If validation fails, it should return `false` or an object containing a `statusCode` and/or `statusMessage` to terminate the request.
|
|
57
|
+
|
|
58
|
+
For more information, see the [Route Validation documentation](/docs/getting-started/routing#route-validation).
|
|
59
|
+
|
|
60
|
+
:read-more{to="/docs/getting-started/routing#route-validation"}
|
|
61
|
+
|
|
62
|
+
### Step 5: Execute Nuxt App Middleware
|
|
63
|
+
|
|
64
|
+
Middleware allows you to run code before navigating to a particular route. It is often used for tasks such as authentication, redirection, or logging.
|
|
65
|
+
|
|
66
|
+
In Nuxt, there are three types of middleware:
|
|
67
|
+
- **Global route middleware**
|
|
68
|
+
- **Named route middleware**
|
|
69
|
+
- **Anonymous (or inline) route middleware**
|
|
70
|
+
|
|
71
|
+
Nuxt automatically executes global middleware for first time enter to the application and every time before route navigation. Named and anonymous middleware are executed only on the routes specified in the middleware property of the page(route) meta defined in the corresponding page components.
|
|
72
|
+
|
|
73
|
+
For details about each type and examples, see the [Middleware documentation](/docs/guide/directory-structure/middleware).
|
|
74
|
+
|
|
75
|
+
Any redirection on the server will result in a `Location:` header being sent to the browser; the browser then makes a fresh request to this new location. All application state will be reset when this happens, unless persisted in a cookie.
|
|
76
|
+
|
|
77
|
+
:read-more{to="/docs/guide/directory-structure/middleware"}
|
|
78
|
+
|
|
79
|
+
### Step 6: Setup Page and Components
|
|
80
|
+
|
|
81
|
+
Nuxt initializes the page and its components during this step and fetches any required data with `useFetch` and `useAsyncData`. Since there are no dynamic updates and no DOM operations occur on the server, Vue lifecycle hooks such as `onBeforeMount`, `onMounted`, and subsequent hooks are **NOT** executed during SSR.
|
|
82
|
+
|
|
83
|
+
::important
|
|
84
|
+
You should avoid code that produces side effects that need cleanup in root scope of `<script setup>`. An example of such side effects is setting up timers with `setInterval`. In client-side only code we may setup a timer and then tear it down in `onBeforeUnmount` or `onUnmounted`. However, because the unmount hooks will never be called during SSR, the timers will stay around forever. To avoid this, move your side-effect code into `onMounted` instead.
|
|
85
|
+
::
|
|
86
|
+
|
|
87
|
+
### Step 7: Render and Generate HTML Output
|
|
88
|
+
|
|
89
|
+
After all components are initialized and data is fetched, Nuxt combines the components with settings from `unhead` to generate a complete HTML document. This HTML, along with the associated data, is sent back to the client to complete the SSR process.
|
|
90
|
+
|
|
91
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
92
|
+
After rendering the Vue application to HTML, Nuxt calls the [`app:rendered`](/docs/api/advanced/hooks#app-hooks-runtime) hook.
|
|
93
|
+
::
|
|
94
|
+
|
|
95
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
96
|
+
Before finalizing and sending the HTML, Nitro will call the [`render:html`](/docs/api/advanced/hooks#nitro-app-hooks-runtime-server-side) hook. This hook allows you to manipulate the generated HTML, such as injecting additional scripts or modifying meta tags.
|
|
97
|
+
::
|
|
98
|
+
|
|
99
|
+
## Client (browser)
|
|
100
|
+
|
|
101
|
+
This part of the lifecycle is fully executed in the browser, no matter which Nuxt mode you've chosen.
|
|
102
|
+
|
|
103
|
+
### Step 1: Initialize Nuxt and Execute Nuxt App Plugins
|
|
104
|
+
|
|
105
|
+
This step is similar to the server-side execution and includes both built-in and custom plugins.
|
|
106
|
+
|
|
107
|
+
Custom plugins in the `plugins/` directory, such as those without a suffix (e.g., `myPlugin.ts`) and with the `.client` suffix (e.g., `myClientPlugin.client.ts`), are executed on the client side.
|
|
108
|
+
|
|
109
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
110
|
+
After this step, Nuxt calls the [`app:created`](/docs/api/advanced/hooks#app-hooks-runtime) hook, which can be used to execute additional logic.
|
|
111
|
+
::
|
|
112
|
+
|
|
113
|
+
:read-more{to="/docs/guide/directory-structure/plugins"}
|
|
114
|
+
|
|
115
|
+
### Step 2: Route Validation
|
|
116
|
+
|
|
117
|
+
This step is the same as the server-side execution and includes the `validate` method if defined in the `definePageMeta` function.
|
|
118
|
+
|
|
119
|
+
### Step 3: Execute Nuxt App Middleware
|
|
120
|
+
|
|
121
|
+
Nuxt middleware runs on both the server and the client. If you want certain code to run in specific environments, consider splitting it by using `import.meta.client` for the client and `import.meta.server` for the server.
|
|
122
|
+
|
|
123
|
+
:read-more{to="/docs/guide/directory-structure/middleware#when-middleware-runs"}
|
|
124
|
+
|
|
125
|
+
### Step 4: Mount Vue Application and Hydration
|
|
126
|
+
|
|
127
|
+
Calling `app.mount('#__nuxt')` mounts the Vue application to the DOM. If the application uses SSR or SSG mode, Vue performs a hydration step to make the client-side application interactive. During hydration, Vue recreates the application (excluding [Server Components](/docs/guide/directory-structure/components#server-components)), matches each component to its corresponding DOM nodes, and attaches DOM event listeners.
|
|
128
|
+
|
|
129
|
+
To ensure proper hydration, it's important to maintain consistency between the data on the server and the client. For API requests, it is recommended to use `useAsyncData`, `useFetch`, or other SSR-friendly composables. These methods ensure that the data fetched on the server side is reused during hydration, avoiding repeated requests. Any new requests should only be triggered after hydration, preventing hydration errors.
|
|
130
|
+
|
|
131
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
132
|
+
Before mounting the Vue application, Nuxt calls the [`app:beforeMount`](/docs/api/advanced/hooks#app-hooks-runtime) hook.
|
|
133
|
+
::
|
|
134
|
+
|
|
135
|
+
::callout{icon="i-lucide-lightbulb"}
|
|
136
|
+
After mounting the Vue application, Nuxt calls the [`app:mounted`](/docs/api/advanced/hooks#app-hooks-runtime) hook.
|
|
137
|
+
::
|
|
138
|
+
|
|
139
|
+
### Step 5: Vue Lifecycle
|
|
140
|
+
|
|
141
|
+
Unlike on the server, the browser executes the full [Vue lifecycle](https://vuejs.org/guide/essentials/lifecycle).
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: 'Vue.js Development'
|
|
3
|
+
description: "Nuxt uses Vue.js and adds features such as component auto-imports, file-based routing and composables for an SSR-friendly usage."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
Nuxt integrates Vue 3, the new major release of Vue that enables new patterns for Nuxt users.
|
|
7
|
+
|
|
8
|
+
::note
|
|
9
|
+
While an in-depth knowledge of Vue is not required to use Nuxt, we recommend that you read the documentation and go through some of the examples on [vuejs.org](https://vuejs.org).
|
|
10
|
+
::
|
|
11
|
+
|
|
12
|
+
Nuxt has always used Vue as a frontend framework.
|
|
13
|
+
|
|
14
|
+
We chose to build Nuxt on top of Vue for these reasons:
|
|
15
|
+
|
|
16
|
+
- The reactivity model of Vue, where a change in data automatically triggers a change in the interface.
|
|
17
|
+
- The component-based templating, while keeping HTML as the common language of the web, enables intuitive patterns to keep your interface consistent, yet powerful.
|
|
18
|
+
- From small projects to large web applications, Vue keeps performing well at scale to ensure that your application keeps delivering value to your users.
|
|
19
|
+
|
|
20
|
+
## Vue with Nuxt
|
|
21
|
+
|
|
22
|
+
### Single File Components
|
|
23
|
+
|
|
24
|
+
[Vue’s single-file components](https://vuejs.org/guide/scaling-up/sfc.html) (SFC or `*.vue` files) encapsulate the markup (`<template>`), logic (`<script>`) and styling (`<style>`) of a Vue component. Nuxt provides a zero-config experience for SFCs with [Hot Module Replacement](https://vite.dev/guide/features.html#hot-module-replacement) that offers a seamless developer experience.
|
|
25
|
+
|
|
26
|
+
### Auto-imports
|
|
27
|
+
|
|
28
|
+
Every Vue component created in the [`components/`](/docs/guide/directory-structure/components) directory of a Nuxt project will be available in your project without having to import it. If a component is not used anywhere, your production’s code will not include it.
|
|
29
|
+
|
|
30
|
+
:read-more{to="/docs/guide/concepts/auto-imports"}
|
|
31
|
+
|
|
32
|
+
### Vue Router
|
|
33
|
+
|
|
34
|
+
Most applications need multiple pages and a way to navigate between them. This is called **routing**. Nuxt uses a [`pages/`](/docs/guide/directory-structure/pages) directory and naming conventions to directly create routes mapped to your files using the official [Vue Router library](https://router.vuejs.org).
|
|
35
|
+
|
|
36
|
+
:read-more{to="/docs/getting-started/routing"}
|
|
37
|
+
|
|
38
|
+
:link-example{to="/docs/examples/features/auto-imports"}
|
|
39
|
+
|
|
40
|
+
## Differences with Nuxt 2 / Vue 2
|
|
41
|
+
|
|
42
|
+
Nuxt 3+ is based on Vue 3. The new major Vue version introduces several changes that Nuxt takes advantage of:
|
|
43
|
+
|
|
44
|
+
- Better performance
|
|
45
|
+
- Composition API
|
|
46
|
+
- TypeScript support
|
|
47
|
+
|
|
48
|
+
### Faster Rendering
|
|
49
|
+
|
|
50
|
+
The Vue Virtual DOM (VDOM) has been rewritten from the ground up and allows for better rendering performance. On top of that, when working with compiled Single-File Components, the Vue compiler can further optimize them at build time by separating static and dynamic markup.
|
|
51
|
+
|
|
52
|
+
This results in faster first rendering (component creation) and updates, and less memory usage. In Nuxt 3, it enables faster server-side rendering as well.
|
|
53
|
+
|
|
54
|
+
### Smaller Bundle
|
|
55
|
+
|
|
56
|
+
With Vue 3 and Nuxt 3, a focus has been put on bundle size reduction. With version 3, most of Vue’s functionality, including template directives and built-in components, is tree-shakable. Your production bundle will not include them if you don’t use them.
|
|
57
|
+
|
|
58
|
+
This way, a minimal Vue 3 application can be reduced to 12 kb gzipped.
|
|
59
|
+
|
|
60
|
+
### Composition API
|
|
61
|
+
|
|
62
|
+
The only way to provide data and logic to components in Vue 2 was through the Options API, which allows you to return data and methods to a template with pre-defined properties like `data` and `methods`:
|
|
63
|
+
|
|
64
|
+
```vue twoslash
|
|
65
|
+
<script>
|
|
66
|
+
export default {
|
|
67
|
+
data() {
|
|
68
|
+
return {
|
|
69
|
+
count: 0
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
methods: {
|
|
73
|
+
increment(){
|
|
74
|
+
this.count++
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
</script>
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
The [Composition API](https://vuejs.org/guide/extras/composition-api-faq.html) introduced in Vue 3 is not a replacement of the Options API, but it enables better logic reuse throughout an application, and is a more natural way to group code by concern in complex components.
|
|
82
|
+
|
|
83
|
+
Used with the `setup` keyword in the `<script>` definition, here is the above component rewritten with Composition API and Nuxt 3’s auto-imported Reactivity APIs:
|
|
84
|
+
|
|
85
|
+
```vue twoslash [components/Counter.vue]
|
|
86
|
+
<script setup lang="ts">
|
|
87
|
+
const count = ref(0)
|
|
88
|
+
const increment = () => count.value++
|
|
89
|
+
</script>
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The goal of Nuxt is to provide a great developer experience around the Composition API.
|
|
93
|
+
|
|
94
|
+
- Use auto-imported [Reactivity functions](https://vuejs.org/api/reactivity-core.html) from Vue and Nuxt [built-in composables](/docs/api/composables/use-async-data).
|
|
95
|
+
- Write your own auto-imported reusable functions in the [`composables/` directory](/docs/guide/directory-structure/composables).
|
|
96
|
+
|
|
97
|
+
### TypeScript Support
|
|
98
|
+
|
|
99
|
+
Both Vue 3 and Nuxt 3+ are written in TypeScript. A fully typed codebase prevents mistakes and documents APIs usage. This doesn’t mean that you have to write your application in TypeScript to take advantage of it. With Nuxt 3, you can opt-in by renaming your file from `.js` to `.ts` , or add `<script setup lang="ts">` in a component.
|
|
100
|
+
|
|
101
|
+
::read-more{to="/docs/guide/concepts/typescript"}
|
|
102
|
+
Read the details about TypeScript in Nuxt
|
|
103
|
+
::
|