@nuxt/docs 4.1.3 → 4.2.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/02.installation.md +4 -4
- package/1.getting-started/03.configuration.md +4 -4
- package/1.getting-started/04.views.md +2 -2
- package/1.getting-started/05.assets.md +1 -1
- package/1.getting-started/06.styling.md +11 -11
- package/1.getting-started/07.routing.md +4 -0
- package/1.getting-started/09.transitions.md +6 -6
- package/1.getting-started/10.data-fetching.md +14 -14
- package/1.getting-started/11.state-management.md +2 -2
- package/1.getting-started/12.error-handling.md +4 -4
- package/1.getting-started/13.server.md +4 -4
- package/1.getting-started/14.layers.md +29 -10
- package/1.getting-started/17.testing.md +3 -3
- package/1.getting-started/18.upgrade.md +205 -48
- package/2.guide/1.directory-structure/1.app/1.components.md +4 -4
- package/2.guide/1.directory-structure/1.app/1.composables.md +1 -1
- package/2.guide/1.directory-structure/1.app/1.layouts.md +2 -2
- package/2.guide/1.directory-structure/1.app/1.middleware.md +4 -4
- package/2.guide/1.directory-structure/1.app/1.pages.md +13 -13
- package/2.guide/1.directory-structure/1.app/1.plugins.md +1 -5
- package/2.guide/1.directory-structure/1.node_modules.md +1 -1
- package/2.guide/1.directory-structure/1.public.md +1 -1
- package/2.guide/1.directory-structure/1.server.md +5 -5
- package/2.guide/1.directory-structure/2.env.md +1 -1
- package/2.guide/1.directory-structure/3.tsconfig.md +38 -7
- package/2.guide/2.concepts/1.auto-imports.md +2 -2
- package/2.guide/2.concepts/10.nuxt-lifecycle.md +3 -3
- package/2.guide/2.concepts/2.vuejs-development.md +3 -3
- package/2.guide/2.concepts/3.rendering.md +4 -4
- package/2.guide/2.concepts/7.esm.md +7 -3
- package/2.guide/2.concepts/8.typescript.md +15 -38
- package/2.guide/2.concepts/9.code-style.md +1 -1
- package/2.guide/3.going-further/1.experimental-features.md +94 -6
- package/2.guide/3.going-further/1.features.md +15 -3
- package/2.guide/3.going-further/1.internals.md +2 -2
- package/2.guide/3.going-further/2.hooks.md +1 -1
- package/2.guide/3.going-further/3.modules.md +112 -29
- package/2.guide/3.going-further/6.nuxt-app.md +5 -5
- package/2.guide/3.going-further/7.layers.md +46 -21
- package/2.guide/3.going-further/9.debugging.md +1 -1
- package/2.guide/4.recipes/1.custom-routing.md +4 -4
- package/2.guide/4.recipes/2.vite-plugin.md +41 -0
- package/2.guide/4.recipes/3.custom-usefetch.md +1 -1
- package/2.guide/5.best-practices/hydration.md +1 -1
- package/3.api/1.components/10.nuxt-picture.md +1 -1
- package/3.api/1.components/11.teleports.md +1 -1
- package/3.api/1.components/12.nuxt-route-announcer.md +1 -1
- package/3.api/1.components/13.nuxt-time.md +5 -1
- package/3.api/1.components/2.nuxt-page.md +1 -1
- package/3.api/1.components/4.nuxt-link.md +11 -11
- package/3.api/1.components/5.nuxt-loading-indicator.md +1 -1
- package/3.api/1.components/6.nuxt-error-boundary.md +1 -1
- package/3.api/2.composables/use-app-config.md +1 -1
- package/3.api/2.composables/use-async-data.md +80 -13
- package/3.api/2.composables/use-cookie.md +7 -7
- package/3.api/2.composables/use-fetch.md +6 -2
- package/3.api/2.composables/use-head-safe.md +37 -20
- package/3.api/2.composables/use-head.md +136 -36
- package/3.api/2.composables/use-hydration.md +24 -18
- package/3.api/2.composables/use-lazy-async-data.md +58 -9
- package/3.api/2.composables/use-lazy-fetch.md +65 -9
- package/3.api/2.composables/use-nuxt-app.md +7 -7
- package/3.api/2.composables/use-nuxt-data.md +1 -1
- package/3.api/2.composables/use-request-fetch.md +1 -1
- package/3.api/2.composables/use-route.md +1 -1
- package/3.api/2.composables/use-router.md +15 -15
- package/3.api/2.composables/use-runtime-hook.md +1 -1
- package/3.api/2.composables/use-state.md +1 -1
- package/3.api/3.utils/abort-navigation.md +2 -2
- package/3.api/3.utils/define-lazy-hydration-component.md +4 -4
- package/3.api/3.utils/define-nuxt-component.md +1 -1
- package/3.api/3.utils/define-nuxt-plugin.md +1 -1
- package/3.api/3.utils/define-nuxt-route-middleware.md +1 -1
- package/3.api/3.utils/define-page-meta.md +8 -8
- package/3.api/3.utils/navigate-to.md +5 -5
- 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/3.utils/refresh-cookie.md +1 -1
- package/3.api/3.utils/update-app-config.md +2 -2
- package/3.api/5.kit/1.modules.md +88 -2
- package/3.api/5.kit/11.nitro.md +4 -0
- package/3.api/5.kit/14.builder.md +66 -10
- package/3.api/5.kit/15.examples.md +3 -5
- package/3.api/5.kit/2.programmatic.md +2 -2
- package/3.api/5.kit/5.components.md +1 -0
- package/3.api/5.kit/9.head.md +132 -0
- package/3.api/6.advanced/1.hooks.md +7 -7
- package/3.api/6.nuxt-config.md +100 -29
- package/5.community/3.reporting-bugs.md +3 -3
- package/5.community/4.contribution.md +1 -1
- package/5.community/5.framework-contribution.md +8 -8
- package/5.community/6.roadmap.md +4 -4
- package/6.bridge/4.plugins-and-middleware.md +1 -1
- package/7.migration/2.configuration.md +2 -2
- package/7.migration/20.module-authors.md +1 -1
- package/7.migration/5.plugins-and-middleware.md +1 -1
- package/7.migration/6.pages-and-layouts.md +2 -2
- package/README.md +1 -1
- package/package.json +1 -1
|
@@ -75,5 +75,5 @@ Note that for a purely static site, it is not possible to set runtime configurat
|
|
|
75
75
|
|
|
76
76
|
::note
|
|
77
77
|
If you want to use environment variables set at build time but do not care about updating these down the line (or only need to update them reactively _within_ your app) then `appConfig` may be a better choice. You can define `appConfig` both within your `nuxt.config` (using environment variables) and also within an `~/app.config.ts` file in your project.
|
|
78
|
-
:read-more{to="/docs/4.x/guide/directory-structure/app-config"}
|
|
78
|
+
:read-more{to="/docs/4.x/guide/directory-structure/app/app-config"}
|
|
79
79
|
::
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "tsconfig.json"
|
|
3
|
-
description: "Nuxt
|
|
3
|
+
description: "Learn how Nuxt manages TypeScript configuration across different parts of your project."
|
|
4
4
|
head.title: "tsconfig.json"
|
|
5
5
|
navigation.icon: i-vscode-icons-file-type-tsconfig
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
Nuxt [automatically generates](/docs/4.x/guide/concepts/typescript) multiple TypeScript configuration files (`.nuxt/tsconfig.app.json`, `.nuxt/tsconfig.server.json`, `.nuxt/tsconfig.node.json` and `.nuxt/tsconfig.shared.json`)
|
|
8
|
+
Nuxt [automatically generates](/docs/4.x/guide/concepts/typescript#auto-generated-types) multiple TypeScript configuration files (`.nuxt/tsconfig.app.json`, `.nuxt/tsconfig.server.json`, `.nuxt/tsconfig.node.json` and `.nuxt/tsconfig.shared.json`) that include recommended basic TypeScript configuration for your project, references to [auto-imports](/docs/4.x/guide/concepts/auto-imports), [API route types](/docs/4.x/guide/concepts/server-engine#typed-api-routes), path aliases, and more.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Your Nuxt project should include the following `tsconfig.json` file at the root of the project:
|
|
11
11
|
|
|
12
12
|
```json [tsconfig.json]
|
|
13
13
|
{
|
|
@@ -29,10 +29,41 @@ You can benefit from this by creating a `tsconfig.json` in the root of your proj
|
|
|
29
29
|
}
|
|
30
30
|
```
|
|
31
31
|
|
|
32
|
-
::
|
|
33
|
-
|
|
32
|
+
::warning
|
|
33
|
+
We do not recommend modifying the contents of this file directly, as doing so could overwrite important settings that Nuxt or other modules rely on. Instead, extend it via `nuxt.config.ts`.
|
|
34
34
|
::
|
|
35
35
|
|
|
36
|
-
::
|
|
37
|
-
|
|
36
|
+
::read-more{to="/docs/4.x/guide/concepts/typescript#project-references"}
|
|
37
|
+
Read more about the different type contexts of a Nuxt project here.
|
|
38
38
|
::
|
|
39
|
+
|
|
40
|
+
## Extending TypeScript Configuration
|
|
41
|
+
|
|
42
|
+
You can customize the TypeScript configuration of your Nuxt project for each context (`app`, `shared`, `node`, and `server`) in the `nuxt.config.ts` file.
|
|
43
|
+
<!-- @case-police-ignore tsConfig -->
|
|
44
|
+
```ts twoslash [nuxt.config.ts]
|
|
45
|
+
export default defineNuxtConfig({
|
|
46
|
+
typescript: {
|
|
47
|
+
// customize tsconfig.app.json
|
|
48
|
+
tsConfig: {
|
|
49
|
+
// ...
|
|
50
|
+
},
|
|
51
|
+
// customize tsconfig.shared.json
|
|
52
|
+
sharedTsConfig: {
|
|
53
|
+
// ...
|
|
54
|
+
},
|
|
55
|
+
// customize tsconfig.node.json
|
|
56
|
+
nodeTsConfig: {
|
|
57
|
+
// ...
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
nitro: {
|
|
61
|
+
typescript: {
|
|
62
|
+
// customize tsconfig.server.json
|
|
63
|
+
tsConfig: {
|
|
64
|
+
// ...
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
})
|
|
69
|
+
```
|
|
@@ -3,7 +3,7 @@ title: Auto-imports
|
|
|
3
3
|
description: "Nuxt auto-imports components, composables, helper functions and Vue APIs."
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
Nuxt auto-imports components, composables and [Vue.js APIs](https://vuejs.org/api) to use across your application without explicitly importing them.
|
|
6
|
+
Nuxt auto-imports components, composables and [Vue.js APIs](https://vuejs.org/api/) to use across your application without explicitly importing them.
|
|
7
7
|
|
|
8
8
|
```vue twoslash [app/app.vue]
|
|
9
9
|
<script setup lang="ts">
|
|
@@ -109,7 +109,7 @@ Nuxt directly auto-imports files created in defined directories:
|
|
|
109
109
|
|
|
110
110
|
::warning
|
|
111
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
|
|
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#caveat-when-unwrapping-in-templates).
|
|
113
113
|
::
|
|
114
114
|
|
|
115
115
|
### Explicit Imports
|
|
@@ -40,13 +40,13 @@ The Vue and Nuxt instances are created first. Afterward, Nuxt executes its serve
|
|
|
40
40
|
- Built-in plugins, such as Vue Router and `unhead`.
|
|
41
41
|
- Custom plugins located in the `app/plugins/` directory, including those without a suffix (e.g., `myPlugin.ts`) and those with the `.server` suffix (e.g., `myServerPlugin.server.ts`).
|
|
42
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/4.x/guide/directory-structure/plugins).
|
|
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/4.x/guide/directory-structure/app/plugins).
|
|
44
44
|
|
|
45
45
|
::callout{icon="i-lucide-lightbulb"}
|
|
46
46
|
After this step, Nuxt calls the [`app:created`](/docs/4.x/api/advanced/hooks#app-hooks-runtime) hook, which can be used to execute additional logic.
|
|
47
47
|
::
|
|
48
48
|
|
|
49
|
-
:read-more{to="/docs/4.x/guide/directory-structure/plugins"}
|
|
49
|
+
:read-more{to="/docs/4.x/guide/directory-structure/app/plugins"}
|
|
50
50
|
|
|
51
51
|
### Step 4: Route Validation
|
|
52
52
|
|
|
@@ -120,7 +120,7 @@ Custom plugins in the `app/plugins/` directory, such as those without a suffix (
|
|
|
120
120
|
After this step, Nuxt calls the [`app:created`](/docs/4.x/api/advanced/hooks#app-hooks-runtime) hook, which can be used to execute additional logic.
|
|
121
121
|
::
|
|
122
122
|
|
|
123
|
-
:read-more{to="/docs/4.x/guide/directory-structure/plugins"}
|
|
123
|
+
:read-more{to="/docs/4.x/guide/directory-structure/app/plugins"}
|
|
124
124
|
|
|
125
125
|
### Step 2: Route Validation
|
|
126
126
|
|
|
@@ -21,7 +21,7 @@ We chose to build Nuxt on top of Vue for these reasons:
|
|
|
21
21
|
|
|
22
22
|
### Single File Components
|
|
23
23
|
|
|
24
|
-
[Vue’s single-file components](https://vuejs.org/guide/scaling-up/sfc
|
|
24
|
+
[Vue’s single-file components](https://vuejs.org/guide/scaling-up/sfc) (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#hot-module-replacement) that offers a seamless developer experience.
|
|
25
25
|
|
|
26
26
|
### Auto-imports
|
|
27
27
|
|
|
@@ -78,7 +78,7 @@ export default {
|
|
|
78
78
|
</script>
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
The [Composition API](https://vuejs.org/guide/extras/composition-api-faq
|
|
81
|
+
The [Composition API](https://vuejs.org/guide/extras/composition-api-faq) 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
82
|
|
|
83
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
84
|
|
|
@@ -91,7 +91,7 @@ const increment = () => count.value++
|
|
|
91
91
|
|
|
92
92
|
The goal of Nuxt is to provide a great developer experience around the Composition API.
|
|
93
93
|
|
|
94
|
-
- Use auto-imported [Reactivity functions](https://vuejs.org/api/reactivity-core
|
|
94
|
+
- Use auto-imported [Reactivity functions](https://vuejs.org/api/reactivity-core) from Vue and Nuxt [built-in composables](/docs/4.x/api/composables/use-async-data).
|
|
95
95
|
- Write your own auto-imported reusable functions in the [`app/composables/` directory](/docs/4.x/guide/directory-structure/app/composables).
|
|
96
96
|
|
|
97
97
|
### TypeScript Support
|
|
@@ -44,7 +44,7 @@ const handleClick = () => {
|
|
|
44
44
|
|
|
45
45
|
On the initial request, the `counter` ref is initialized in the server since it is rendered inside the `<p>` tag. The contents of `handleClick` is never executed here. During hydration in the browser, the `counter` ref is re-initialized. The `handleClick` finally binds itself to the button; Therefore it is reasonable to deduce that the body of `handleClick` will always run in a browser environment.
|
|
46
46
|
|
|
47
|
-
[Middlewares](/docs/4.x/guide/directory-structure/app/middleware) and [pages](/docs/4.x/guide/directory-structure/app/pages) run in the server and on the client during hydration. [Plugins](/docs/4.x/guide/directory-structure/plugins) can be rendered on the server or client or both. [Components](/docs/4.x/guide/directory-structure/app/components) can be forced to run on the client only as well. [Composables](/docs/4.x/guide/directory-structure/app/composables) and [utilities](/docs/4.x/guide/directory-structure/app/utils) are rendered based on the context of their usage.
|
|
47
|
+
[Middlewares](/docs/4.x/guide/directory-structure/app/middleware) and [pages](/docs/4.x/guide/directory-structure/app/pages) run in the server and on the client during hydration. [Plugins](/docs/4.x/guide/directory-structure/app/plugins) can be rendered on the server or client or both. [Components](/docs/4.x/guide/directory-structure/app/components) can be forced to run on the client only as well. [Composables](/docs/4.x/guide/directory-structure/app/composables) and [utilities](/docs/4.x/guide/directory-structure/app/utils) are rendered based on the context of their usage.
|
|
48
48
|
|
|
49
49
|
**Benefits of server-side rendering:**
|
|
50
50
|
- **Performance**: Users can get immediate access to the page's content because browsers can display static content much faster than JavaScript-generated content. At the same time, Nuxt preserves the interactivity of a web application during the hydration process.
|
|
@@ -57,7 +57,7 @@ On the initial request, the `counter` ref is initialized in the server since it
|
|
|
57
57
|
Universal rendering is very versatile and can fit almost any use case, and is especially appropriate for any content-oriented websites: **blogs, marketing websites, portfolios, e-commerce sites, and marketplaces.**
|
|
58
58
|
|
|
59
59
|
::tip
|
|
60
|
-
For more examples about writing Vue code without hydration mismatch, see [the Vue docs](https://vuejs.org/guide/scaling-up/ssr
|
|
60
|
+
For more examples about writing Vue code without hydration mismatch, see [the Vue docs](https://vuejs.org/guide/scaling-up/ssr#hydration-mismatch).
|
|
61
61
|
::
|
|
62
62
|
|
|
63
63
|
::important
|
|
@@ -225,8 +225,8 @@ Edge-side rendering is possible thanks to [Nitro](https://nitro.build/), the [se
|
|
|
225
225
|
|
|
226
226
|
The current platforms where you can leverage ESR are:
|
|
227
227
|
- [Cloudflare Pages](https://pages.cloudflare.com) with zero configuration using the git integration and the `nuxt build` command
|
|
228
|
-
- [Vercel
|
|
229
|
-
- [Netlify Edge Functions](https://www.netlify.com/
|
|
228
|
+
- [Vercel Cloud](https://vercel.com/home) using the `nuxt build` command and `NITRO_PRESET=vercel-edge` environment variable
|
|
229
|
+
- [Netlify Edge Functions](https://www.netlify.com/platform/#netlify-edge-functions) using the `nuxt build` command and `NITRO_PRESET=netlify-edge` environment variable
|
|
230
230
|
|
|
231
231
|
Note that **Hybrid Rendering** can be used when using Edge-Side Rendering with route rules.
|
|
232
232
|
|
|
@@ -31,7 +31,7 @@ export { a }
|
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
Before ECMAScript Modules (ESM) became a standard (it took more than 10 years!), tooling like
|
|
34
|
-
[webpack](https://webpack.js.org/guides/ecma-script-modules) and even languages like TypeScript started supporting so-called **ESM syntax**.
|
|
34
|
+
[webpack](https://webpack.js.org/guides/ecma-script-modules/) and even languages like TypeScript started supporting so-called **ESM syntax**.
|
|
35
35
|
However, there are some key differences with actual spec; here's [a helpful explainer](https://hacks.mozilla.org/2018/03/es-modules-a-cartoon-deep-dive/).
|
|
36
36
|
|
|
37
37
|
### What is 'Native' ESM?
|
|
@@ -50,6 +50,10 @@ When adding modules to your package, things were a little different. A sample li
|
|
|
50
50
|
|
|
51
51
|
So in Nuxt 2, the bundler (webpack) would pull in the CJS file ('main') for the server build and use the ESM file ('module') for the client build.
|
|
52
52
|
|
|
53
|
+
::note
|
|
54
|
+
The `module` field is a convention used by bundlers like webpack and Rollup, but is not recognized by Node.js itself. Node.js only uses the [`exports`](https://nodejs.org/api/packages.html#exports) and [`main`](https://nodejs.org/api/packages.html#main) fields for module resolution.
|
|
55
|
+
::
|
|
56
|
+
|
|
53
57
|
However, in recent Node.js LTS releases, it is now possible to [use native ESM module](https://nodejs.org/api/esm.html) within Node.js. That means that Node.js itself can process JavaScript using ESM syntax, although it doesn't do it by default. The two most common ways to enable ESM syntax are:
|
|
54
58
|
|
|
55
59
|
- set `"type": "module"` within your `package.json` and keep using `.js` extension
|
|
@@ -59,7 +63,7 @@ This is what we do for Nuxt Nitro; we output a `.output/server/index.mjs` file.
|
|
|
59
63
|
|
|
60
64
|
### What Are Valid Imports in a Node.js Context?
|
|
61
65
|
|
|
62
|
-
When you `import` a module rather than `require` it, Node.js resolves it differently. For example, when you import `sample-library`, Node.js will look
|
|
66
|
+
When you `import` a module rather than `require` it, Node.js resolves it differently. For example, when you import `sample-library`, Node.js will look for the `exports` entry in that library's `package.json`, or fall back to the `main` entry if `exports` is not defined.
|
|
63
67
|
|
|
64
68
|
This is also true of dynamic imports, like `const b = await import('sample-library')`.
|
|
65
69
|
|
|
@@ -157,7 +161,7 @@ const pkg = require('cjs-pkg')
|
|
|
157
161
|
console.log(pkg) // { test: 123 }
|
|
158
162
|
```
|
|
159
163
|
|
|
160
|
-
[Node.js in native ESM mode](https://nodejs.org/api/esm.html#interoperability-with-commonjs), [typescript with `esModuleInterop` enabled](https://www.typescriptlang.org/tsconfig
|
|
164
|
+
[Node.js in native ESM mode](https://nodejs.org/api/esm.html#interoperability-with-commonjs), [typescript with `esModuleInterop` enabled](https://www.typescriptlang.org/tsconfig/#esModuleInterop) and bundlers such as webpack, provide a compatibility mechanism so that we can default import such library.
|
|
161
165
|
This mechanism is often referred to as "interop require default":
|
|
162
166
|
|
|
163
167
|
```js
|
|
@@ -47,56 +47,37 @@ export default defineNuxtConfig({
|
|
|
47
47
|
|
|
48
48
|
## Auto-generated Types
|
|
49
49
|
|
|
50
|
-
|
|
50
|
+
Nuxt projects rely on auto-generated types to work properly. These types are stored in the [`.nuxt`](/docs/4.x/guide/directory-structure/nuxt) directory and are generated when you run the dev server or build your application. You can also generate these files manually by running `nuxt prepare`.
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
The generated `tsconfig.json` files inside the [`.nuxt`](/docs/4.x/guide/directory-structure/nuxt) directory include **recommended basic TypeScript configuration** for your project, references to [auto-imports](/docs/4.x/guide/concepts/auto-imports), [API route types](/docs/4.x/guide/concepts/server-engine#typed-api-routes), path aliases like `#imports`, `~/file`, or `#build/file`, and more.
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
Some of the references in the file are to files that are only generated within your `buildDir` (`.nuxt`) and therefore for full typings, you will need to run `nuxt dev` or `nuxt build`.
|
|
57
|
-
|
|
58
|
-
### `.nuxt/tsconfig.app.json`
|
|
59
|
-
|
|
60
|
-
This file contains the recommended basic TypeScript configuration for your project, including resolved aliases injected by Nuxt or modules you are using, so you can get full type support and path auto-complete for aliases like `~/file` or `#build/file`.
|
|
61
|
-
|
|
62
|
-
::note
|
|
63
|
-
Consider using the `imports` section of [nuxt.config](/docs/4.x/api/nuxt-config#imports) to include directories beyond the default ones. This can be useful for auto-importing types which you're using across your app.
|
|
54
|
+
::warning
|
|
55
|
+
Nuxt relies on this configuration, and [Nuxt Modules](/docs/4.x/guide/going-further/modules) can extend it as well. For this reason, it is not recommended to modify your `tsconfig.json` file directly, as doing so could overwrite important settings. Instead, extend it via `nuxt.config.ts`. [Learn more about extending the configuration here](/docs/4.x/guide/directory-structure/tsconfig).
|
|
64
56
|
::
|
|
65
57
|
|
|
66
|
-
[Read more about how to extend this configuration](/docs/4.x/guide/directory-structure/tsconfig).
|
|
67
|
-
|
|
68
58
|
::tip{icon="i-lucide-video" to="https://youtu.be/umLI7SlPygY" target="_blank"}
|
|
69
59
|
Watch a video from Daniel Roe explaining built-in Nuxt aliases.
|
|
70
60
|
::
|
|
71
61
|
|
|
72
|
-
::note
|
|
73
|
-
Nitro also [auto-generates types](/docs/4.x/guide/concepts/server-engine#typed-api-routes) for API routes. Plus, Nuxt also generates types for globally available components and [auto-imports from your composables](/docs/4.x/guide/directory-structure/app/composables), plus other core functionality.
|
|
74
|
-
::
|
|
75
|
-
|
|
76
|
-
::note
|
|
77
|
-
For backward compatibility, Nuxt still generates `./.nuxt/tsconfig.json`. However, we recommend using [TypeScript project references](/docs/4.x/guide/directory-structure/tsconfig) with the new configuration files (`.nuxt/tsconfig.app.json`, `.nuxt/tsconfig.server.json`, etc.) for better type safety and performance.
|
|
78
|
-
|
|
79
|
-
If you do extend from `./.nuxt/tsconfig.json`, keep in mind that all options will be overwritten by those defined in your `tsconfig.json`. Overwriting options such as `"compilerOptions.paths"` with your own configuration will lead TypeScript to not factor in the module resolutions, which can cause module resolutions such as `#imports` to not be recognized.
|
|
80
|
-
|
|
81
|
-
In case you need to extend options further, you can use the [`alias` property](/docs/4.x/api/nuxt-config#alias) within your `nuxt.config`. Nuxt will pick them up and extend the generated TypeScript configurations accordingly.
|
|
82
|
-
::
|
|
83
|
-
|
|
84
62
|
## Project References
|
|
85
63
|
|
|
86
64
|
Nuxt uses [TypeScript project references](https://www.typescriptlang.org/docs/handbook/project-references.html) to improve type-checking performance and provide better IDE support. This feature allows TypeScript to break up your codebase into smaller, more manageable pieces.
|
|
87
65
|
|
|
88
66
|
### How Nuxt Uses Project References
|
|
89
67
|
|
|
90
|
-
When you run `nuxt dev` or `nuxt
|
|
68
|
+
When you run `nuxt dev`, `nuxt build` or `nuxt prepare`, Nuxt will generate multiple `tsconfig.json` files for different parts of your application.
|
|
91
69
|
|
|
92
|
-
- **`.nuxt/tsconfig.app.json`** - Configuration for your application code
|
|
93
|
-
- **`.nuxt/tsconfig.node.json`** - Configuration for your `nuxt.config` and
|
|
70
|
+
- **`.nuxt/tsconfig.app.json`** - Configuration for your application code within the `app/` directory
|
|
71
|
+
- **`.nuxt/tsconfig.node.json`** - Configuration for your `nuxt.config.ts` and files outside the other contexts
|
|
94
72
|
- **`.nuxt/tsconfig.server.json`** - Configuration for server-side code (when applicable)
|
|
95
73
|
- **`.nuxt/tsconfig.shared.json`** - For code shared between app and server contexts (like types and non-environment specific utilities)
|
|
96
|
-
- **`.nuxt/tsconfig.json`** - Legacy configuration for backward compatibility
|
|
97
74
|
|
|
98
75
|
Each of these files is configured to reference the appropriate dependencies and provide optimal type-checking for their specific context.
|
|
99
76
|
|
|
77
|
+
::note
|
|
78
|
+
For backward compatibility, Nuxt still generates `.nuxt/tsconfig.json`. However, we recommend using [TypeScript project references](/docs/4.x/guide/directory-structure/tsconfig) with the new configuration files (`.nuxt/tsconfig.app.json`, `.nuxt/tsconfig.server.json`, etc.) for better type safety and performance. This legacy file will be removed in a future version of Nuxt.
|
|
79
|
+
::
|
|
80
|
+
|
|
100
81
|
### Benefits of Project References
|
|
101
82
|
|
|
102
83
|
- **Faster builds**: TypeScript can skip rebuilding unchanged projects
|
|
@@ -104,13 +85,9 @@ Each of these files is configured to reference the appropriate dependencies and
|
|
|
104
85
|
- **Isolated compilation**: Errors in one part of your application don't prevent compilation of other parts
|
|
105
86
|
- **Clearer dependency management**: Each project explicitly declares its dependencies
|
|
106
87
|
|
|
107
|
-
::note
|
|
108
|
-
The project reference setup is handled automatically by Nuxt. You typically don't need to modify these configurations manually, but understanding how they work can help you troubleshoot type-checking issues.
|
|
109
|
-
::
|
|
110
|
-
|
|
111
88
|
### Augmenting Types with Project References
|
|
112
89
|
|
|
113
|
-
Since the project is divided into **multiple type contexts**, it's important to **augment types within the correct context** to ensure they
|
|
90
|
+
Since the project is divided into **multiple type contexts**, it's important to **augment types within the correct context** to ensure they're properly recognized. TypeScript will not recognize augmentations placed outside these directories unless they are explicitly included in the appropriate context.
|
|
114
91
|
|
|
115
92
|
For example, if you want to augment types for the `app` context, the augmentation file should be placed in the `app/` directory.
|
|
116
93
|
|
|
@@ -118,15 +95,15 @@ Similarly:
|
|
|
118
95
|
- For the `server` context, place the augmentation file in the `server/` directory.
|
|
119
96
|
- For types that are **shared between the app and server**, place the file in the `shared/` directory.
|
|
120
97
|
|
|
121
|
-
::
|
|
122
|
-
|
|
98
|
+
::read-more{to="/docs/4.x/guide/going-further/modules#extending-tsconfigjson"}
|
|
99
|
+
Read more about augmenting specific type contexts from **files outside those contexts** in the Module Author Guide.
|
|
123
100
|
::
|
|
124
101
|
|
|
125
102
|
## Strict Checks
|
|
126
103
|
|
|
127
104
|
TypeScript comes with certain checks to give you more safety and analysis of your program.
|
|
128
105
|
|
|
129
|
-
[Strict checks](https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#getting-stricter-checks) are enabled by default in Nuxt to give you greater type safety.
|
|
106
|
+
[Strict checks](https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html#getting-stricter-checks) are enabled by default in Nuxt when the [`typescript.typeCheck`](/docs/4.x/guide/concepts/typescript#type-checking) option is enabled to give you greater type safety.
|
|
130
107
|
|
|
131
108
|
If you are currently converting your codebase to TypeScript, you may want to temporarily disable strict checks by setting `strict` to `false` in your `nuxt.config`:
|
|
132
109
|
|
|
@@ -8,7 +8,7 @@ description: "Nuxt supports ESLint out of the box"
|
|
|
8
8
|
The recommended approach for Nuxt is to enable ESLint support using the [`@nuxt/eslint`](https://eslint.nuxt.com/packages/module) module, that will setup project-aware ESLint configuration for you.
|
|
9
9
|
|
|
10
10
|
:::callout{icon="i-lucide-lightbulb"}
|
|
11
|
-
The module is designed for the [new ESLint flat config format](https://eslint.org/docs/latest/use/configure/configuration-files
|
|
11
|
+
The module is designed for the [new ESLint flat config format](https://eslint.org/docs/latest/use/configure/configuration-files) which is the [default format since ESLint v9](https://eslint.org/blog/2024/04/eslint-v9.0.0-released/). If you are using the legacy `.eslintrc` config, you will need to [configure manually with `@nuxt/eslint-config`](https://eslint.nuxt.com/packages/config#customizing-the-config). We highly recommend you to migrate over the flat config to be future-proof.
|
|
12
12
|
:::
|
|
13
13
|
|
|
14
14
|
## Quick Setup
|
|
@@ -5,7 +5,7 @@ description: "Enable Nuxt experimental features to unlock new possibilities."
|
|
|
5
5
|
|
|
6
6
|
Nuxt includes experimental features that you can enable in your configuration file.
|
|
7
7
|
|
|
8
|
-
Internally, Nuxt uses `@nuxt/schema` to define these experimental features. You can refer to the [API documentation](/docs/4.x/
|
|
8
|
+
Internally, Nuxt uses `@nuxt/schema` to define these experimental features. You can refer to the [API documentation](/docs/4.x/guide/going-further/experimental-features) or the [source code](https://github.com/nuxt/nuxt/blob/main/packages/schema/src/config/experimental.ts) for more information.
|
|
9
9
|
|
|
10
10
|
::note
|
|
11
11
|
Note that these features are experimental and could be removed or modified in the future.
|
|
@@ -87,6 +87,44 @@ export default defineNuxtConfig({
|
|
|
87
87
|
This feature will likely be removed in a near future.
|
|
88
88
|
::
|
|
89
89
|
|
|
90
|
+
## extractAsyncDataHandlers
|
|
91
|
+
|
|
92
|
+
Extracts handler functions from `useAsyncData` and `useLazyAsyncData` calls into separate chunks for improved code splitting and caching efficiency.
|
|
93
|
+
|
|
94
|
+
```ts twoslash [nuxt.config.ts]
|
|
95
|
+
export default defineNuxtConfig({
|
|
96
|
+
experimental: {
|
|
97
|
+
extractAsyncDataHandlers: true,
|
|
98
|
+
},
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
This feature transforms inline handler functions into dynamically imported chunks:
|
|
103
|
+
|
|
104
|
+
```vue
|
|
105
|
+
<!-- Before -->
|
|
106
|
+
<script setup>
|
|
107
|
+
const { data } = await useAsyncData('user', async () => {
|
|
108
|
+
return await $fetch('/api/user')
|
|
109
|
+
})
|
|
110
|
+
</script>
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
```vue
|
|
114
|
+
<!-- After transformation -->
|
|
115
|
+
<script setup>
|
|
116
|
+
const { data } = await useAsyncData('user', () =>
|
|
117
|
+
import('/generated-chunk.js').then(r => r.default()),
|
|
118
|
+
)
|
|
119
|
+
</script>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The benefit of this transformation is that we can split out data fetching logic — while still allowing the code to be loaded if required.
|
|
123
|
+
|
|
124
|
+
::important
|
|
125
|
+
This feature is only recommended for **static builds** with payload extraction, and where data does not need to be re-fetched at runtime.
|
|
126
|
+
::
|
|
127
|
+
|
|
90
128
|
## emitRouteChunkError
|
|
91
129
|
|
|
92
130
|
Emits `app:chunkError` hook when there is an error loading vite/webpack chunks. Default behavior is to perform a reload of the new route on navigation to a new route when a chunk fails to load.
|
|
@@ -252,7 +290,7 @@ export default defineNuxtConfig({
|
|
|
252
290
|
|
|
253
291
|
:link-example{to="https://stackblitz.com/edit/nuxt-view-transitions?file=app.vue" target="_blank"}
|
|
254
292
|
|
|
255
|
-
::read-more{icon="i-simple-icons-mdnwebdocs" to="https://developer.mozilla.org/en-US/docs/Web/API/
|
|
293
|
+
::read-more{icon="i-simple-icons-mdnwebdocs" to="https://developer.mozilla.org/en-US/docs/Web/API/View_Transition_API" target="_blank"}
|
|
256
294
|
Read more about the **View Transition API**.
|
|
257
295
|
::
|
|
258
296
|
|
|
@@ -367,12 +405,12 @@ should do this automatically for you.)
|
|
|
367
405
|
// This would be unsafe in a dynamic page (e.g. `[slug].vue`) because the route slug makes a difference
|
|
368
406
|
// to the data fetched, but Nuxt can't know that because it's not reflected in the key.
|
|
369
407
|
const route = useRoute()
|
|
370
|
-
const { data } = await useAsyncData(async () => {
|
|
371
|
-
return await $fetch(`/api/my-page/${route.params.slug}
|
|
408
|
+
const { data } = await useAsyncData(async (_nuxtApp, { signal }) => {
|
|
409
|
+
return await $fetch(`/api/my-page/${route.params.slug}`, { signal })
|
|
372
410
|
})
|
|
373
411
|
// Instead, you should use a key that uniquely identifies the data fetched.
|
|
374
|
-
const { data } = await useAsyncData(route.params.slug, async () => {
|
|
375
|
-
return await $fetch(`/api/my-page/${route.params.slug}
|
|
412
|
+
const { data } = await useAsyncData(route.params.slug, async (_nuxtApp, { signal }) => {
|
|
413
|
+
return await $fetch(`/api/my-page/${route.params.slug}`, { signal })
|
|
376
414
|
})
|
|
377
415
|
```
|
|
378
416
|
|
|
@@ -831,3 +869,53 @@ export default defineNuxtConfig({
|
|
|
831
869
|
},
|
|
832
870
|
})
|
|
833
871
|
```
|
|
872
|
+
|
|
873
|
+
## typescriptPlugin
|
|
874
|
+
|
|
875
|
+
Enable enhanced TypeScript developer experience with the `@dxup/nuxt` module.
|
|
876
|
+
|
|
877
|
+
This experimental plugin provides improved TypeScript integration and development tooling for better DX when working with TypeScript in Nuxt applications.
|
|
878
|
+
|
|
879
|
+
This flag is disabled by default, but you can enable this feature:
|
|
880
|
+
|
|
881
|
+
```ts twoslash [nuxt.config.ts]
|
|
882
|
+
export default defineNuxtConfig({
|
|
883
|
+
experimental: {
|
|
884
|
+
typescriptPlugin: true,
|
|
885
|
+
},
|
|
886
|
+
})
|
|
887
|
+
```
|
|
888
|
+
|
|
889
|
+
::important
|
|
890
|
+
To use this feature, you need to:
|
|
891
|
+
- Have `typescript` installed as a dependency
|
|
892
|
+
- Configure VS Code to use your workspace TypeScript version (see [VS Code documentation](https://code.visualstudio.com/docs/typescript/typescript-compiling#_using-the-workspace-version-of-typescript))
|
|
893
|
+
::
|
|
894
|
+
|
|
895
|
+
::read-more{icon="i-simple-icons-github" to="https://github.com/KazariEX/dxup" target="_blank"}
|
|
896
|
+
Learn more about **@dxup/nuxt**.
|
|
897
|
+
::
|
|
898
|
+
|
|
899
|
+
## viteEnvironmentApi
|
|
900
|
+
|
|
901
|
+
Enable Vite 6's new [Environment API](https://vite.dev/guide/api-environment) for improved build configuration and plugin architecture.
|
|
902
|
+
|
|
903
|
+
When you set `future.compatibilityVersion` to `5`, this feature is enabled by default. You can also enable it explicitly for testing:
|
|
904
|
+
|
|
905
|
+
```ts twoslash [nuxt.config.ts]
|
|
906
|
+
export default defineNuxtConfig({
|
|
907
|
+
experimental: {
|
|
908
|
+
viteEnvironmentApi: true,
|
|
909
|
+
},
|
|
910
|
+
})
|
|
911
|
+
```
|
|
912
|
+
|
|
913
|
+
The Vite Environment API provides better consistency between development and production builds, more granular control over environment-specific configuration, and improved performance.
|
|
914
|
+
|
|
915
|
+
::important
|
|
916
|
+
Enabling this feature changes how Vite plugins are registered and configured. See the [Vite Environment API migration guide](/docs/4.x/getting-started/upgrade#migration-to-vite-environment-api) for details on updating your plugins.
|
|
917
|
+
::
|
|
918
|
+
|
|
919
|
+
::read-more{to="https://vite.dev/guide/api-environment" target="_blank"}
|
|
920
|
+
Learn more about Vite's Environment API.
|
|
921
|
+
::
|
|
@@ -61,9 +61,21 @@ There is also a `future` namespace for early opting-in to new features that will
|
|
|
61
61
|
|
|
62
62
|
### compatibilityVersion
|
|
63
63
|
|
|
64
|
-
This
|
|
64
|
+
This enables early access to Nuxt features or flags.
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
Setting `compatibilityVersion` to `5` changes defaults throughout your Nuxt configuration to opt in to Nuxt v5 behaviour, including enabling the [Vite Environment API](/docs/4.x/guide/going-further/experimental-features#viteenvironmentapi).
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
export default defineNuxtConfig({
|
|
70
|
+
future: {
|
|
71
|
+
compatibilityVersion: 5,
|
|
72
|
+
},
|
|
73
|
+
})
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
::read-more{to="/docs/4.x/getting-started/upgrade#testing-nuxt-5"}
|
|
77
|
+
Learn more about testing Nuxt 5.
|
|
78
|
+
::
|
|
67
79
|
|
|
68
80
|
### multiApp
|
|
69
81
|
|
|
@@ -79,7 +91,7 @@ export default defineNuxtConfig({
|
|
|
79
91
|
|
|
80
92
|
### typescriptBundlerResolution
|
|
81
93
|
|
|
82
|
-
This enables 'Bundler' module resolution mode for TypeScript, which is the recommended setting for frameworks like Nuxt and [Vite](https://vite.dev/guide/performance
|
|
94
|
+
This enables 'Bundler' module resolution mode for TypeScript, which is the recommended setting for frameworks like Nuxt and [Vite](https://vite.dev/guide/performance#reduce-resolve-operations).
|
|
83
95
|
|
|
84
96
|
It improves type support when using modern libraries with `exports`.
|
|
85
97
|
|
|
@@ -30,7 +30,7 @@ Global usage is possible for the browser but not on the server, to avoid sharing
|
|
|
30
30
|
|
|
31
31
|
Since [`useNuxtApp`](/docs/4.x/api/composables/use-nuxt-app) throws an exception if context is currently unavailable, if your composable does not always require `nuxtApp`, you can use [`tryUseNuxtApp`](/docs/4.x/api/composables/use-nuxt-app#tryusenuxtapp) instead, which will return `null` instead of throwing an exception.
|
|
32
32
|
|
|
33
|
-
To extend the `nuxtApp` interface and hook into different stages or access contexts, we can use [Nuxt Plugins](/docs/4.x/guide/directory-structure/plugins).
|
|
33
|
+
To extend the `nuxtApp` interface and hook into different stages or access contexts, we can use [Nuxt Plugins](/docs/4.x/guide/directory-structure/app/plugins).
|
|
34
34
|
|
|
35
35
|
Check [Nuxt App](/docs/4.x/api/composables/use-nuxt-app) for more information about this interface.
|
|
36
36
|
|
|
@@ -76,6 +76,6 @@ Nuxt builds and bundles project using Node.js but also has a runtime side.
|
|
|
76
76
|
|
|
77
77
|
While both areas can be extended, that runtime context is isolated from build-time. Therefore, they are not supposed to share state, code, or context other than runtime configuration!
|
|
78
78
|
|
|
79
|
-
`nuxt.config` and [Nuxt Modules](/docs/4.x/guide/going-further/modules) can be used to extend the build context, and [Nuxt Plugins](/docs/4.x/guide/directory-structure/plugins) can be used to extend runtime.
|
|
79
|
+
`nuxt.config` and [Nuxt Modules](/docs/4.x/guide/going-further/modules) can be used to extend the build context, and [Nuxt Plugins](/docs/4.x/guide/directory-structure/app/plugins) can be used to extend runtime.
|
|
80
80
|
|
|
81
81
|
When building an application for production, `nuxt build` will generate a standalone build in the `.output` directory, independent of `nuxt.config` and [Nuxt modules](/docs/4.x/guide/going-further/modules).
|
|
@@ -39,7 +39,7 @@ Explore all available Nuxt hooks.
|
|
|
39
39
|
|
|
40
40
|
## App Hooks (Runtime)
|
|
41
41
|
|
|
42
|
-
App hooks can be mainly used by [Nuxt Plugins](/docs/4.x/guide/directory-structure/plugins) to hook into rendering lifecycle but could also be used in Vue composables.
|
|
42
|
+
App hooks can be mainly used by [Nuxt Plugins](/docs/4.x/guide/directory-structure/app/plugins) to hook into rendering lifecycle but could also be used in Vue composables.
|
|
43
43
|
|
|
44
44
|
```ts [app/plugins/test.ts]
|
|
45
45
|
export default defineNuxtPlugin((nuxtApp) => {
|