@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
|
@@ -224,7 +224,7 @@ Modules, like everything in a Nuxt configuration, aren't included in your applic
|
|
|
224
224
|
Inside the runtime directory, you can provide any kind of assets related to the Nuxt App:
|
|
225
225
|
- Vue components
|
|
226
226
|
- Composables
|
|
227
|
-
- [Nuxt plugins](/docs/4.x/guide/directory-structure/plugins)
|
|
227
|
+
- [Nuxt plugins](/docs/4.x/guide/directory-structure/app/plugins)
|
|
228
228
|
|
|
229
229
|
To the [server engine](/docs/4.x/guide/concepts/server-engine), Nitro:
|
|
230
230
|
- API routes
|
|
@@ -501,35 +501,51 @@ export default defineNuxtModule({
|
|
|
501
501
|
|
|
502
502
|
#### Using Other Modules in Your Module
|
|
503
503
|
|
|
504
|
-
If your module depends on other modules, you can
|
|
504
|
+
If your module depends on other modules, you can specify them using the `moduleDependencies` option. This provides a more robust way to handle module dependencies with version constraints and configuration merging:
|
|
505
505
|
|
|
506
506
|
```ts
|
|
507
|
-
import { createResolver, defineNuxtModule
|
|
507
|
+
import { createResolver, defineNuxtModule } from '@nuxt/kit'
|
|
508
508
|
|
|
509
|
-
|
|
510
|
-
async setup (options, nuxt) {
|
|
511
|
-
const resolver = createResolver(import.meta.url)
|
|
509
|
+
const resolver = createResolver(import.meta.url)
|
|
512
510
|
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
511
|
+
export default defineNuxtModule<ModuleOptions>({
|
|
512
|
+
meta: {
|
|
513
|
+
name: 'my-module',
|
|
514
|
+
},
|
|
515
|
+
moduleDependencies: {
|
|
516
|
+
'@nuxtjs/tailwindcss': {
|
|
517
|
+
// You can specify a version constraint for the module
|
|
518
|
+
version: '>=6',
|
|
519
|
+
// Any configuration that should override `nuxt.options`
|
|
520
|
+
overrides: {
|
|
521
|
+
exposeConfig: true,
|
|
522
|
+
},
|
|
523
|
+
// Any configuration that should be set. It will override module defaults but
|
|
524
|
+
// will not override any configuration set in `nuxt.options`
|
|
525
|
+
defaults: {
|
|
526
|
+
config: {
|
|
527
|
+
darkMode: 'class',
|
|
528
|
+
content: {
|
|
529
|
+
files: [
|
|
530
|
+
resolver.resolve('./runtime/components/**/*.{vue,mjs,ts}'),
|
|
531
|
+
resolver.resolve('./runtime/*.{mjs,js,ts}'),
|
|
532
|
+
],
|
|
533
|
+
},
|
|
526
534
|
},
|
|
527
535
|
},
|
|
528
|
-
}
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
setup (options, nuxt) {
|
|
539
|
+
// We can inject our CSS file which includes Tailwind's directives
|
|
540
|
+
nuxt.options.css.push(resolver.resolve('./runtime/assets/styles.css'))
|
|
529
541
|
},
|
|
530
542
|
})
|
|
531
543
|
```
|
|
532
544
|
|
|
545
|
+
::callout{type="info"}
|
|
546
|
+
The `moduleDependencies` option replaces the deprecated `installModule` function and ensures proper setup order and configuration merging.
|
|
547
|
+
::
|
|
548
|
+
|
|
533
549
|
#### Using Hooks
|
|
534
550
|
|
|
535
551
|
[Lifecycle hooks](/docs/4.x/guide/going-further/hooks) allow you to expand almost every aspect of Nuxt. Modules can hook to them programmatically or through the `hooks` map in their definition.
|
|
@@ -637,6 +653,19 @@ export default defineNuxtModule({
|
|
|
637
653
|
})
|
|
638
654
|
```
|
|
639
655
|
|
|
656
|
+
#### Updating Templates
|
|
657
|
+
|
|
658
|
+
If you need to update your templates/virtual files, you can leverage the `updateTemplates` utility like this:
|
|
659
|
+
|
|
660
|
+
```ts
|
|
661
|
+
nuxt.hook('builder:watch', (event, path) => {
|
|
662
|
+
if (path.includes('my-module-feature.config')) {
|
|
663
|
+
// This will reload the template that you registered
|
|
664
|
+
updateTemplates({ filter: t => t.filename === 'my-module-feature.mjs' })
|
|
665
|
+
}
|
|
666
|
+
})
|
|
667
|
+
```
|
|
668
|
+
|
|
640
669
|
#### Adding Type Declarations
|
|
641
670
|
|
|
642
671
|
You might also want to add a type declaration to the user's project (for example, to augment a Nuxt interface
|
|
@@ -675,19 +704,73 @@ nuxt.hook('prepare:types', ({ references }) => {
|
|
|
675
704
|
})
|
|
676
705
|
```
|
|
677
706
|
|
|
678
|
-
|
|
707
|
+
#### Extending `tsconfig.json`
|
|
708
|
+
|
|
709
|
+
There are multiple ways to extend the TypeScript configuration of the user's project from your module.
|
|
679
710
|
|
|
680
|
-
|
|
711
|
+
The simplest way is to modify the Nuxt configuration directly like this:
|
|
681
712
|
|
|
713
|
+
<!-- @case-police-ignore tsConfig -->
|
|
682
714
|
```ts
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
715
|
+
// extend tsconfig.app.json
|
|
716
|
+
nuxt.options.typescript.tsConfig.include ??= []
|
|
717
|
+
nuxt.options.typescript.tsConfig.include.push(resolve('./augments.d.ts'))
|
|
718
|
+
|
|
719
|
+
// extend tsconfig.shared.json
|
|
720
|
+
nuxt.options.typescript.sharedTsConfig.include ??= []
|
|
721
|
+
nuxt.options.typescript.sharedTsConfig.include.push(resolve('./augments.d.ts'))
|
|
722
|
+
|
|
723
|
+
// extend tsconfig.node.json
|
|
724
|
+
nuxt.options.typescript.nodeTsConfig.include ??= []
|
|
725
|
+
nuxt.options.typescript.nodeTsConfig.include.push(resolve('./augments.d.ts'))
|
|
726
|
+
|
|
727
|
+
// extend tsconfig.server.json
|
|
728
|
+
nuxt.options.nitro.typescript ??= {}
|
|
729
|
+
nuxt.options.nitro.typescript.tsConfig ??= {}
|
|
730
|
+
nuxt.options.nitro.typescript.tsConfig.include ??= []
|
|
731
|
+
nuxt.options.nitro.typescript.tsConfig.include.push(resolve('./augments.d.ts'))
|
|
732
|
+
```
|
|
733
|
+
|
|
734
|
+
Alternatively, you can use the `prepare:types` and `nitro:prepare:types` hooks to extend the TypeScript references for specific type contexts, or modify the TypeScript configuration similar to the example above.
|
|
735
|
+
|
|
736
|
+
```ts
|
|
737
|
+
nuxt.hook('prepare:types', ({ references, sharedReferences, nodeReferences }) => {
|
|
738
|
+
// extend app context
|
|
739
|
+
references.push({ path: resolve('./augments.d.ts') })
|
|
740
|
+
// extend shared context
|
|
741
|
+
sharedReferences.push({ path: resolve('./augments.d.ts') })
|
|
742
|
+
// extend node context
|
|
743
|
+
nodeReferences.push({ path: resolve('./augments.d.ts') })
|
|
744
|
+
})
|
|
745
|
+
|
|
746
|
+
nuxt.hook('nitro:prepare:types', ({ references }) => {
|
|
747
|
+
// extend server context
|
|
748
|
+
references.push({ path: resolve('./augments.d.ts') })
|
|
688
749
|
})
|
|
689
750
|
```
|
|
690
751
|
|
|
752
|
+
::note
|
|
753
|
+
TypeScript references add files to the type context [without being affected by the `exclude` option in `tsconfig.json`](https://www.typescriptlang.org/tsconfig/#exclude).
|
|
754
|
+
::
|
|
755
|
+
|
|
756
|
+
#### Augmenting Types From Modules
|
|
757
|
+
|
|
758
|
+
Nuxt automatically includes your module's directories in the appropriate type contexts. To augment types from your module, all you need to do is place the type declaration file in the appropriate directory based on the augmented type context. Alternatively, you can [extend the TypeScript configuration](/docs/4.x/guide/going-further/modules#extending-tsconfigjson) to augment from an arbitrary location.
|
|
759
|
+
|
|
760
|
+
- `my-module/runtime/` - app type context (except for the `runtime/server` directory)
|
|
761
|
+
- `my-module/runtime/server/` - server type context
|
|
762
|
+
- `my-module/` - node type context (except for the `runtime/` and `runtime/server` directories)
|
|
763
|
+
|
|
764
|
+
```bash [Directory Structure]
|
|
765
|
+
-| my-module/ # node type context
|
|
766
|
+
---| runtime/ # app type context
|
|
767
|
+
------| augments.app.d.ts
|
|
768
|
+
------| server/ # server type context
|
|
769
|
+
---------| augments.server.d.ts
|
|
770
|
+
---| module.ts
|
|
771
|
+
---| augments.node.d.ts
|
|
772
|
+
```
|
|
773
|
+
|
|
691
774
|
### Testing
|
|
692
775
|
|
|
693
776
|
Testing helps ensuring your module works as expected given various setup. Find in this section how to perform various kinds of tests against your module.
|
|
@@ -758,7 +841,7 @@ An example of such a workflow is available on [the module starter](https://githu
|
|
|
758
841
|
|
|
759
842
|
Having a playground Nuxt application to test your module when developing it is really useful. [The module starter integrates one for that purpose](/docs/4.x/guide/going-further/modules#how-to-develop).
|
|
760
843
|
|
|
761
|
-
You can test your module with other Nuxt applications (applications that are not part of your module repository) locally. To do so, you can use [`npm pack`](https://docs.npmjs.com/cli/commands/npm-pack) command, or your package manager equivalent, to create a tarball from your module. Then in your test project, you can add your module to `package.json` packages as: `"my-module": "file:/path/to/tarball.tgz"`.
|
|
844
|
+
You can test your module with other Nuxt applications (applications that are not part of your module repository) locally. To do so, you can use [`npm pack`](https://docs.npmjs.com/cli/commands/npm-pack/) command, or your package manager equivalent, to create a tarball from your module. Then in your test project, you can add your module to `package.json` packages as: `"my-module": "file:/path/to/tarball.tgz"`.
|
|
762
845
|
|
|
763
846
|
After that, you should be able to reference `my-module` like in any regular project.
|
|
764
847
|
|
|
@@ -860,7 +943,7 @@ Watch Vue School video about Nuxt module types.
|
|
|
860
943
|
|
|
861
944
|
### Module Types
|
|
862
945
|
|
|
863
|
-
**Official modules** are modules prefixed (scoped) with `@nuxt/` (e.g. [`@nuxt/content`](https://content.
|
|
946
|
+
**Official modules** are modules prefixed (scoped) with `@nuxt/` (e.g. [`@nuxt/content`](https://content.nuxt.com)). They are made and maintained actively by the Nuxt team. Like with the framework, contributions from the community are more than welcome to help make them better!
|
|
864
947
|
|
|
865
948
|
**Community modules** are modules prefixed (scoped) with `@nuxtjs/` (e.g. [`@nuxtjs/tailwindcss`](https://tailwindcss.nuxtjs.org)). They are proven modules made and maintained by community members. Again, contributions are welcome from anyone.
|
|
866
949
|
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
|
|
10
10
|
In Nuxt, you can access runtime app context within composables, components and plugins.
|
|
11
11
|
|
|
12
|
-
::read-more{to="https://v2.nuxt.com/docs/internals-glossary/context
|
|
12
|
+
::read-more{to="https://v2.nuxt.com/docs/internals-glossary/context/#the-context" target="_blank"}
|
|
13
13
|
In Nuxt 2, this was referred to as **Nuxt context**.
|
|
14
14
|
::
|
|
15
15
|
|
|
@@ -23,7 +23,7 @@ Jump over the `NuxtApp` interface documentation.
|
|
|
23
23
|
|
|
24
24
|
Many composables and utilities, both built-in and user-made, may require access to the Nuxt instance. This doesn't exist everywhere on your application, because a fresh instance is created on every request.
|
|
25
25
|
|
|
26
|
-
Currently, the Nuxt context is only accessible in [plugins](/docs/4.x/guide/directory-structure/plugins), [Nuxt hooks](/docs/4.x/guide/going-further/hooks), [Nuxt middleware](/docs/4.x/guide/directory-structure/app/middleware) (if wrapped in `defineNuxtRouteMiddleware`), and [setup functions](https://vuejs.org/api/composition-api-setup
|
|
26
|
+
Currently, the Nuxt context is only accessible in [plugins](/docs/4.x/guide/directory-structure/app/plugins), [Nuxt hooks](/docs/4.x/guide/going-further/hooks), [Nuxt middleware](/docs/4.x/guide/directory-structure/app/middleware) (if wrapped in `defineNuxtRouteMiddleware`), and [setup functions](https://vuejs.org/api/composition-api-setup) (in pages and components).
|
|
27
27
|
|
|
28
28
|
If a composable is called without access to the context, you may get an error stating that 'A composable that requires access to the Nuxt instance was called outside of a plugin, Nuxt hook, Nuxt middleware, or Vue setup function.' In that case, you can also explicitly call functions within this context by using [`nuxtApp.runWithContext`](/docs/4.x/api/composables/use-nuxt-app#runwithcontext).
|
|
29
29
|
|
|
@@ -42,7 +42,7 @@ If your composable does not always need `nuxtApp` or you simply want to check if
|
|
|
42
42
|
|
|
43
43
|
Plugins also receive `nuxtApp` as the first argument for convenience.
|
|
44
44
|
|
|
45
|
-
:read-more{to="/docs/4.x/guide/directory-structure/plugins"}
|
|
45
|
+
:read-more{to="/docs/4.x/guide/directory-structure/app/plugins"}
|
|
46
46
|
|
|
47
47
|
## Providing Helpers
|
|
48
48
|
|
|
@@ -55,10 +55,10 @@ nuxtApp.provide('hello', name => `Hello ${name}!`)
|
|
|
55
55
|
console.log(nuxtApp.$hello('name')) // Prints "Hello name!"
|
|
56
56
|
```
|
|
57
57
|
|
|
58
|
-
::read-more{to="/docs/4.x/guide/directory-structure/plugins#providing-helpers"}
|
|
58
|
+
::read-more{to="/docs/4.x/guide/directory-structure/app/plugins#providing-helpers"}
|
|
59
59
|
It is possible to inject helpers by returning an object with a `provide` key in plugins.
|
|
60
60
|
::
|
|
61
61
|
|
|
62
|
-
::read-more{to="https://v2.nuxt.com/docs/directory-structure/plugins
|
|
62
|
+
::read-more{to="https://v2.nuxt.com/docs/directory-structure/plugins/#inject-in-root--context" target="_blank"}
|
|
63
63
|
In Nuxt 2 plugins, this was referred to as **inject function**.
|
|
64
64
|
::
|
|
@@ -20,9 +20,9 @@ Additionally, certain other files in the layer directory will be auto-scanned an
|
|
|
20
20
|
- [`app/layouts/*`](/docs/4.x/guide/directory-structure/app/layouts) - Extend the default layouts
|
|
21
21
|
- [`app/middleware/*`](/docs/4.x/guide/directory-structure/app/middleware) - Extend the default middleware
|
|
22
22
|
- [`app/pages/*`](/docs/4.x/guide/directory-structure/app/pages) - Extend the default pages
|
|
23
|
-
- [`app/plugins/*`](/docs/4.x/guide/directory-structure/plugins) - Extend the default plugins
|
|
23
|
+
- [`app/plugins/*`](/docs/4.x/guide/directory-structure/app/plugins) - Extend the default plugins
|
|
24
24
|
- [`app/utils/*`](/docs/4.x/guide/directory-structure/app/utils) - Extend the default utils
|
|
25
|
-
- [`app/app.config.ts`](/docs/4.x/guide/directory-structure/app-config) - Extend the default app config
|
|
25
|
+
- [`app/app.config.ts`](/docs/4.x/guide/directory-structure/app/app-config) - Extend the default app config
|
|
26
26
|
- [`server/*`](/docs/4.x/guide/directory-structure/server) - Extend the default server endpoints & middleware
|
|
27
27
|
- [`nuxt.config.ts`](/docs/4.x/guide/directory-structure/nuxt-config)- Extend the default nuxt config
|
|
28
28
|
|
|
@@ -68,29 +68,46 @@ Additionally, certain other files in the layer directory will be auto-scanned an
|
|
|
68
68
|
|
|
69
69
|
## Layer Priority
|
|
70
70
|
|
|
71
|
-
When extending from multiple layers, it's important to understand the priority
|
|
71
|
+
When extending from 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.
|
|
72
72
|
|
|
73
|
-
|
|
74
|
-
2. **Auto-scanned local layers** from `~~/layers` directory in alphabetical order (Z overrides A)
|
|
75
|
-
3. **Your project** has the highest priority in the stack - it will always override other layers
|
|
73
|
+
The priority order from highest to lowest is:
|
|
76
74
|
|
|
77
|
-
|
|
75
|
+
1. **Your project files** - always have the highest priority
|
|
76
|
+
2. **Auto-scanned layers** from `~~/layers` directory - sorted alphabetically (Z has higher priority than A)
|
|
77
|
+
3. **Layers in `extends`** config - first entry has higher priority than second
|
|
78
|
+
|
|
79
|
+
### When to Use Each
|
|
80
|
+
|
|
81
|
+
- **`extends`** - Use for external dependencies (npm packages, remote repositories) or layers outside your project directory
|
|
82
|
+
- **`~~/layers` directory** - Use for local layers that are part of your project
|
|
83
|
+
|
|
84
|
+
::tip
|
|
85
|
+
If you need to control the order of auto-scanned layers, you can prefix them with numbers: `~/layers/1.z-layer`, `~/layers/2.a-layer`. This way `2.a-layer` will have higher priority than `1.z-layer`.
|
|
86
|
+
::
|
|
87
|
+
|
|
88
|
+
### Example
|
|
78
89
|
|
|
79
90
|
```ts [nuxt.config.ts]
|
|
80
91
|
export default defineNuxtConfig({
|
|
81
92
|
extends: [
|
|
82
|
-
//
|
|
83
|
-
'
|
|
84
|
-
//
|
|
85
|
-
'
|
|
86
|
-
//
|
|
87
|
-
'
|
|
93
|
+
// Local layer outside the project
|
|
94
|
+
'../base',
|
|
95
|
+
// NPM package
|
|
96
|
+
'@my-themes/awesome',
|
|
97
|
+
// Remote repository
|
|
98
|
+
'github:my-themes/awesome#v1',
|
|
88
99
|
],
|
|
89
|
-
// Your project has the highest priority
|
|
90
100
|
})
|
|
91
101
|
```
|
|
92
102
|
|
|
93
|
-
If you also have
|
|
103
|
+
If you also have `~~/layers/custom`, the priority order is:
|
|
104
|
+
- Your project files (highest)
|
|
105
|
+
- `~~/layers/custom`
|
|
106
|
+
- `../base`
|
|
107
|
+
- `@my-themes/awesome`
|
|
108
|
+
- `github:my-themes/awesome#v1` (lowest)
|
|
109
|
+
|
|
110
|
+
This means your project files will override any layer, and `~~/layers/custom` will override anything in `extends`.
|
|
94
111
|
|
|
95
112
|
## Starter Template
|
|
96
113
|
|
|
@@ -234,22 +251,30 @@ export default defineNuxtConfig({
|
|
|
234
251
|
|
|
235
252
|
## Multi-Layer Support for Nuxt Modules
|
|
236
253
|
|
|
237
|
-
You can use the
|
|
254
|
+
You can use the [`getLayerDirectories`](/docs/4.x/api/kit/layers#getlayerdirectories) utility from Nuxt Kit to support custom multi-layer handling for your modules.
|
|
238
255
|
|
|
239
256
|
```ts [modules/my-module.ts]
|
|
257
|
+
import { defineNuxtModule, getLayerDirectories } from 'nuxt/kit'
|
|
258
|
+
|
|
240
259
|
export default defineNuxtModule({
|
|
241
260
|
setup (_options, nuxt) {
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
261
|
+
const layerDirs = getLayerDirectories()
|
|
262
|
+
|
|
263
|
+
for (const [index, layer] of layerDirs.entries()) {
|
|
264
|
+
console.log(`Layer ${index}:`)
|
|
265
|
+
console.log(` Root: ${layer.root}`)
|
|
266
|
+
console.log(` App: ${layer.app}`)
|
|
267
|
+
console.log(` Server: ${layer.server}`)
|
|
268
|
+
console.log(` Pages: ${layer.appPages}`)
|
|
269
|
+
// ... other directories
|
|
245
270
|
}
|
|
246
271
|
},
|
|
247
272
|
})
|
|
248
273
|
```
|
|
249
274
|
|
|
250
275
|
**Notes:**
|
|
251
|
-
- Earlier items in the
|
|
252
|
-
- The user's project is the first item in the
|
|
276
|
+
- Earlier items in the array have higher priority and override later ones
|
|
277
|
+
- The user's project is the first item in the array
|
|
253
278
|
|
|
254
279
|
## Going Deeper
|
|
255
280
|
|
|
@@ -36,7 +36,7 @@ It is possible to debug your Nuxt app in your IDE while you are developing it.
|
|
|
36
36
|
|
|
37
37
|
### Example VS Code Debug Configuration
|
|
38
38
|
|
|
39
|
-
You may need to update the config below with a path to your web browser. For more information, visit the [VS Code documentation about debug configuration](https://
|
|
39
|
+
You may need to update the config below with a path to your web browser. For more information, visit the [VS Code documentation about debug configuration](https://code.visualstudio.com/docs/debugtest/debugging#_launch-configurations).
|
|
40
40
|
|
|
41
41
|
```json5
|
|
42
42
|
{
|
|
@@ -17,7 +17,7 @@ If it returns `null` or `undefined`, Nuxt will fall back to the default routes (
|
|
|
17
17
|
import type { RouterConfig } from '@nuxt/schema'
|
|
18
18
|
|
|
19
19
|
export default {
|
|
20
|
-
// https://router.vuejs.org/api/interfaces/routeroptions
|
|
20
|
+
// https://router.vuejs.org/api/interfaces/routeroptions#routes
|
|
21
21
|
routes: _routes => [
|
|
22
22
|
{
|
|
23
23
|
name: 'home',
|
|
@@ -81,13 +81,13 @@ The [Nuxt kit](/docs/4.x/guide/going-further/kit) provides a few ways [to add ro
|
|
|
81
81
|
|
|
82
82
|
## Router Options
|
|
83
83
|
|
|
84
|
-
On top of customizing options for [`vue-router`](https://router.vuejs.org/api/interfaces/routeroptions
|
|
84
|
+
On top of customizing options for [`vue-router`](https://router.vuejs.org/api/interfaces/routeroptions), Nuxt offers [additional options](/docs/4.x/api/nuxt-config#router) to customize the router.
|
|
85
85
|
|
|
86
86
|
### Using `router.options`
|
|
87
87
|
|
|
88
88
|
This is the recommended way to specify [router options](/docs/4.x/api/nuxt-config#router).
|
|
89
89
|
|
|
90
|
-
```ts [router.options.ts]
|
|
90
|
+
```ts [app/router.options.ts]
|
|
91
91
|
import type { RouterConfig } from '@nuxt/schema'
|
|
92
92
|
|
|
93
93
|
export default {
|
|
@@ -175,7 +175,7 @@ import type { RouterConfig } from '@nuxt/schema'
|
|
|
175
175
|
import { createMemoryHistory } from 'vue-router'
|
|
176
176
|
|
|
177
177
|
export default {
|
|
178
|
-
// https://router.vuejs.org/api/interfaces/routeroptions
|
|
178
|
+
// https://router.vuejs.org/api/interfaces/routeroptions
|
|
179
179
|
history: base => import.meta.client ? createMemoryHistory(base) : null, /* default */
|
|
180
180
|
} satisfies RouterConfig
|
|
181
181
|
```
|
|
@@ -63,3 +63,44 @@ import config from '~/data/hello.yaml'
|
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
::
|
|
66
|
+
|
|
67
|
+
## Using Vite Plugins in Nuxt Modules
|
|
68
|
+
|
|
69
|
+
If you're developing a Nuxt module and need to add Vite plugins, you should use the [`addVitePlugin`](/docs/4.x/api/kit/builder#addviteplugin) utility:
|
|
70
|
+
|
|
71
|
+
```ts [modules/my-module.ts]
|
|
72
|
+
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
|
|
73
|
+
import yaml from '@rollup/plugin-yaml'
|
|
74
|
+
|
|
75
|
+
export default defineNuxtModule({
|
|
76
|
+
setup () {
|
|
77
|
+
addVitePlugin(yaml())
|
|
78
|
+
},
|
|
79
|
+
})
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
For environment-specific plugins in Nuxt 5+, use the `applyToEnvironment()` method:
|
|
83
|
+
|
|
84
|
+
```ts [modules/my-module.ts]
|
|
85
|
+
import { addVitePlugin, defineNuxtModule } from '@nuxt/kit'
|
|
86
|
+
|
|
87
|
+
export default defineNuxtModule({
|
|
88
|
+
setup () {
|
|
89
|
+
addVitePlugin(() => ({
|
|
90
|
+
name: 'my-client-plugin',
|
|
91
|
+
applyToEnvironment (environment) {
|
|
92
|
+
return environment.name === 'client'
|
|
93
|
+
},
|
|
94
|
+
// Plugin configuration
|
|
95
|
+
}))
|
|
96
|
+
},
|
|
97
|
+
})
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
::important
|
|
101
|
+
If you're writing code that needs to access resolved Vite configuration, you should use the `config` and `configResolved` hooks _within_ your Vite plugin, rather than using Nuxt's `vite:extend`, `vite:extendConfig` and `vite:configResolved`.
|
|
102
|
+
::
|
|
103
|
+
|
|
104
|
+
::read-more{to="/docs/4.x/api/kit/builder#addviteplugin"}
|
|
105
|
+
Read more about `addVitePlugin` in the Nuxt Kit documentation.
|
|
106
|
+
::
|
|
@@ -12,7 +12,7 @@ However, Nuxt provides a way to create a custom fetcher for your API (or multipl
|
|
|
12
12
|
|
|
13
13
|
## Custom `$fetch`
|
|
14
14
|
|
|
15
|
-
Let's create a custom `$fetch` instance with a [Nuxt plugin](/docs/4.x/guide/directory-structure/plugins).
|
|
15
|
+
Let's create a custom `$fetch` instance with a [Nuxt plugin](/docs/4.x/guide/directory-structure/app/plugins).
|
|
16
16
|
|
|
17
17
|
::note
|
|
18
18
|
`$fetch` is a configured instance of [ofetch](https://github.com/unjs/ofetch) which supports adding the base URL of your Nuxt server as well as direct function calls during SSR (avoiding HTTP roundtrips).
|
|
@@ -184,5 +184,5 @@ onMounted(() => {
|
|
|
184
184
|
4. **Avoid side effects in setup**: Move browser-dependent code to `onMounted`
|
|
185
185
|
|
|
186
186
|
::tip
|
|
187
|
-
You can read the [Vue documentation on SSR hydration mismatch](https://vuejs.org/guide/scaling-up/ssr
|
|
187
|
+
You can read the [Vue documentation on SSR hydration mismatch](https://vuejs.org/guide/scaling-up/ssr#hydration-mismatch) for a better understanding of hydration.
|
|
188
188
|
::
|
|
@@ -12,7 +12,7 @@ links:
|
|
|
12
12
|
|
|
13
13
|
Usage of `<NuxtPicture>` is almost identical to [`<NuxtImg>`](/docs/4.x/api/components/nuxt-img) but it also allows serving modern formats like `webp` when possible.
|
|
14
14
|
|
|
15
|
-
Learn more about the [`<picture>` tag on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/
|
|
15
|
+
Learn more about the [`<picture>` tag on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/picture).
|
|
16
16
|
|
|
17
17
|
## Setup
|
|
18
18
|
|
|
@@ -4,7 +4,7 @@ description: The <Teleport> component teleports a component to a different locat
|
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
::warning
|
|
7
|
-
The `to` target of [`<Teleport>`](https://vuejs.org/guide/built-ins/teleport
|
|
7
|
+
The `to` target of [`<Teleport>`](https://vuejs.org/guide/built-ins/teleport) expects a CSS selector string or an actual DOM node. Nuxt currently has SSR support for teleports to `#teleports` only, with client-side support for other targets using a `<ClientOnly>` wrapper.
|
|
8
8
|
::
|
|
9
9
|
|
|
10
10
|
## Body Teleport
|
|
@@ -16,7 +16,7 @@ This component is available in Nuxt v3.12+.
|
|
|
16
16
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
|
-
Add `<NuxtRouteAnnouncer/>` in your [`app.vue`](/docs/4.x/guide/directory-structure/app) or [`app/layouts/`](/docs/4.x/guide/directory-structure/app/layouts) to enhance accessibility by informing assistive technologies about page title changes. This ensures that navigational changes are announced to users relying on screen readers.
|
|
19
|
+
Add `<NuxtRouteAnnouncer/>` in your [`app.vue`](/docs/4.x/guide/directory-structure/app/app) or [`app/layouts/`](/docs/4.x/guide/directory-structure/app/layouts) to enhance accessibility by informing assistive technologies about page title changes. This ensures that navigational changes are announced to users relying on screen readers.
|
|
20
20
|
|
|
21
21
|
```vue [app/app.vue]
|
|
22
22
|
<template>
|
|
@@ -104,13 +104,17 @@ Enables relative time formatting using the Intl.RelativeTimeFormat API:
|
|
|
104
104
|
|
|
105
105
|
When `relative` is set to `true`, the component also accepts properties from [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat):
|
|
106
106
|
|
|
107
|
+
::warning
|
|
108
|
+
Due to `style` being a reserved prop, `relativeStyle` prop is used instead.
|
|
109
|
+
::
|
|
110
|
+
|
|
107
111
|
```vue
|
|
108
112
|
<template>
|
|
109
113
|
<NuxtTime
|
|
110
114
|
:datetime="Date.now() - 3 * 24 * 60 * 60 * 1000"
|
|
111
115
|
relative
|
|
112
116
|
numeric="auto"
|
|
113
|
-
style="long"
|
|
117
|
+
relative-style="long"
|
|
114
118
|
/>
|
|
115
119
|
</template>
|
|
116
120
|
```
|
|
@@ -11,7 +11,7 @@ links:
|
|
|
11
11
|
`<NuxtPage>` is a built-in component that comes with Nuxt. It lets you display top-level or nested pages located in the [`app/pages/`](/docs/4.x/guide/directory-structure/app/pages) directory.
|
|
12
12
|
|
|
13
13
|
::note
|
|
14
|
-
`<NuxtPage>` is a wrapper around [`<RouterView>`](https://router.vuejs.org/api/interfaces/
|
|
14
|
+
`<NuxtPage>` is a wrapper around [`<RouterView>`](https://router.vuejs.org/api/interfaces/routerviewprops) from Vue Router. It should be used instead of `<RouterView>` because the former takes additional care of internal states. Otherwise, `useRoute()` may return incorrect paths.
|
|
15
15
|
::
|
|
16
16
|
|
|
17
17
|
`<NuxtPage>` includes the following components:
|
|
@@ -244,21 +244,21 @@ export default defineNuxtConfig({
|
|
|
244
244
|
|
|
245
245
|
### RouterLink
|
|
246
246
|
|
|
247
|
-
When not using `external`, `<NuxtLink>` supports all Vue Router's [`RouterLink` props](https://router.vuejs.org/api/interfaces/
|
|
247
|
+
When not using `external`, `<NuxtLink>` supports all Vue Router's [`RouterLink` props](https://router.vuejs.org/api/interfaces/routerlinkprops)
|
|
248
248
|
|
|
249
|
-
- `to`: Any URL or a [route location object](https://router.vuejs.org/api/type-aliases/
|
|
250
|
-
- `custom`: Whether `<NuxtLink>` should wrap its content in an `<a>` element. It allows taking full control of how a link is rendered and how navigation works when it is clicked. Works the same as [Vue Router's `custom` prop](https://router.vuejs.org/api/interfaces/
|
|
251
|
-
- `exactActiveClass`: A class to apply on exact active links. Works the same as [Vue Router's `exactActiveClass` prop](https://router.vuejs.org/api/interfaces/
|
|
252
|
-
- `activeClass`: A class to apply on active links. Works the same as [Vue Router's `activeClass` prop](https://router.vuejs.org/api/interfaces/
|
|
253
|
-
- `replace`: Works the same as [Vue Router's `replace` prop](https://router.vuejs.org/api/interfaces/
|
|
254
|
-
- `ariaCurrentValue`: An `aria-current` attribute value to apply on exact active links. Works the same as [Vue Router's `ariaCurrentValue` prop](https://router.vuejs.org/api/interfaces/
|
|
249
|
+
- `to`: Any URL or a [route location object](https://router.vuejs.org/api/type-aliases/routelocation) from Vue Router
|
|
250
|
+
- `custom`: Whether `<NuxtLink>` should wrap its content in an `<a>` element. It allows taking full control of how a link is rendered and how navigation works when it is clicked. Works the same as [Vue Router's `custom` prop](https://router.vuejs.org/api/interfaces/routerlinkprops#custom-)
|
|
251
|
+
- `exactActiveClass`: A class to apply on exact active links. Works the same as [Vue Router's `exactActiveClass` prop](https://router.vuejs.org/api/interfaces/routerlinkprops#exactActiveClass-) on internal links. Defaults to Vue Router's default (`"router-link-exact-active"`)
|
|
252
|
+
- `activeClass`: A class to apply on active links. Works the same as [Vue Router's `activeClass` prop](https://router.vuejs.org/api/interfaces/routerlinkprops#activeClass-) on internal links. Defaults to Vue Router's default (`"router-link-active"`)
|
|
253
|
+
- `replace`: Works the same as [Vue Router's `replace` prop](https://router.vuejs.org/api/interfaces/routelocationoptions#replace-) on internal links
|
|
254
|
+
- `ariaCurrentValue`: An `aria-current` attribute value to apply on exact active links. Works the same as [Vue Router's `ariaCurrentValue` prop](https://router.vuejs.org/api/interfaces/routerlinkprops#ariaCurrentValue-) on internal links
|
|
255
255
|
|
|
256
256
|
### NuxtLink
|
|
257
257
|
|
|
258
258
|
- `href`: An alias for `to`. If used with `to`, `href` will be ignored
|
|
259
259
|
- `noRel`: If set to `true`, no `rel` attribute will be added to the external link
|
|
260
260
|
- `external`: Forces the link to be rendered as an `<a>` tag instead of a Vue Router `RouterLink`.
|
|
261
|
-
- `prefetch`: When enabled will prefetch middleware, layouts and payloads (when using [payloadExtraction](/docs/4.x/
|
|
261
|
+
- `prefetch`: When enabled will prefetch middleware, layouts and payloads (when using [payloadExtraction](/docs/4.x/guide/going-further/experimental-features#payloadextraction)) of links in the viewport. Used by the experimental [crossOriginPrefetch](/docs/4.x/guide/going-further/experimental-features#crossoriginprefetch) config.
|
|
262
262
|
- `prefetchOn`: Allows custom control of when to prefetch links. Possible options are `interaction` and `visibility` (default). You can also pass an object for full control, for example: `{ interaction: true, visibility: true }`. This prop is only used when `prefetch` is enabled (default) and `noPrefetch` is not set.
|
|
263
263
|
- `noPrefetch`: Disables prefetching.
|
|
264
264
|
- `prefetchedClass`: A class to apply to links that have been prefetched.
|
|
@@ -276,7 +276,7 @@ Defaults can be overwritten, see [overwriting defaults](/docs/4.x/api/components
|
|
|
276
276
|
|
|
277
277
|
### In Nuxt Config
|
|
278
278
|
|
|
279
|
-
You can overwrite some `<NuxtLink>` defaults in your [`nuxt.config`](/docs/4.x/
|
|
279
|
+
You can overwrite some `<NuxtLink>` defaults in your [`nuxt.config`](/docs/4.x/guide/going-further/experimental-features#defaults)
|
|
280
280
|
|
|
281
281
|
::important
|
|
282
282
|
These options will likely be moved elsewhere in the future, such as into `app.config` or into the `app/` directory.
|
|
@@ -336,8 +336,8 @@ function defineNuxtLink (options: NuxtLinkOptions): Component {}
|
|
|
336
336
|
|
|
337
337
|
- `componentName`: A name for the component. Default is `NuxtLink`.
|
|
338
338
|
- `externalRelAttribute`: A default `rel` attribute value applied on external links. Defaults to `"noopener noreferrer"`. Set it to `""` to disable
|
|
339
|
-
- `activeClass`: A default class to apply on active links. Works the same as [Vue Router's `linkActiveClass` option](https://router.vuejs.org/api/interfaces/
|
|
340
|
-
- `exactActiveClass`: A default class to apply on exact active links. Works the same as [Vue Router's `linkExactActiveClass` option](https://router.vuejs.org/api/interfaces/
|
|
339
|
+
- `activeClass`: A default class to apply on active links. Works the same as [Vue Router's `linkActiveClass` option](https://router.vuejs.org/api/interfaces/routeroptions#linkActiveClass-). Defaults to Vue Router's default (`"router-link-active"`)
|
|
340
|
+
- `exactActiveClass`: A default class to apply on exact active links. Works the same as [Vue Router's `linkExactActiveClass` option](https://router.vuejs.org/api/interfaces/routeroptions#linkExactActiveClass-). Defaults to Vue Router's default (`"router-link-exact-active"`)
|
|
341
341
|
- `trailingSlash`: An option to either add or remove trailing slashes in the `href`. If unset or not matching the valid values `append` or `remove`, it will be ignored.
|
|
342
342
|
- `prefetch`: Whether or not to prefetch links by default.
|
|
343
343
|
- `prefetchOn`: Granular control of which prefetch strategies to apply by default.
|
|
@@ -10,7 +10,7 @@ links:
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
Add `<NuxtLoadingIndicator/>` in your [`app.vue`](/docs/4.x/guide/directory-structure/app) or [`app/layouts/`](/docs/4.x/guide/directory-structure/app/layouts).
|
|
13
|
+
Add `<NuxtLoadingIndicator/>` in your [`app.vue`](/docs/4.x/guide/directory-structure/app/app) or [`app/layouts/`](/docs/4.x/guide/directory-structure/app/layouts).
|
|
14
14
|
|
|
15
15
|
```vue [app/app.vue]
|
|
16
16
|
<template>
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
::tip
|
|
12
|
-
The `<NuxtErrorBoundary>` uses Vue's [`onErrorCaptured`](https://vuejs.org/api/composition-api-lifecycle
|
|
12
|
+
The `<NuxtErrorBoundary>` uses Vue's [`onErrorCaptured`](https://vuejs.org/api/composition-api-lifecycle#onerrorcaptured) hook under the hood.
|
|
13
13
|
::
|
|
14
14
|
|
|
15
15
|
## Events
|