@nuxt/docs 3.20.1 → 3.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/1.getting-started/01.introduction.md +5 -5
- package/1.getting-started/02.installation.md +4 -7
- package/1.getting-started/03.configuration.md +32 -32
- package/1.getting-started/04.views.md +10 -8
- package/1.getting-started/05.assets.md +8 -8
- package/1.getting-started/06.styling.md +24 -18
- package/1.getting-started/07.routing.md +11 -11
- package/1.getting-started/08.seo-meta.md +10 -6
- package/1.getting-started/09.transitions.md +12 -12
- package/1.getting-started/10.data-fetching.md +18 -18
- package/1.getting-started/11.state-management.md +7 -7
- package/1.getting-started/12.error-handling.md +28 -22
- package/1.getting-started/13.server.md +9 -9
- package/1.getting-started/14.layers.md +52 -18
- package/1.getting-started/15.prerendering.md +10 -4
- package/1.getting-started/16.deployment.md +3 -2
- package/1.getting-started/17.testing.md +18 -8
- package/1.getting-started/18.upgrade.md +111 -60
- package/{2.guide/1.directory-structure → 2.directory-structure}/0.nuxt.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/0.output.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.assets.md +3 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.components.md +13 -9
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.composables.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.content.md +2 -2
- package/2.directory-structure/1.layers.md +87 -0
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.layouts.md +37 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.middleware.md +8 -8
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.modules.md +10 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.node_modules.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.pages.md +43 -22
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.plugins.md +6 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.public.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.server.md +29 -9
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.shared.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/1.utils.md +5 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.env.md +4 -4
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtignore.md +2 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/2.nuxtrc.md +6 -3
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.app-config.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.app.md +5 -5
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.error.md +10 -8
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.nuxt-config.md +2 -2
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.package.md +1 -1
- package/{2.guide/1.directory-structure → 2.directory-structure}/3.tsconfig.md +3 -2
- package/2.directory-structure/index.md +65 -0
- package/3.guide/0.index.md +28 -0
- package/{2.guide/2.concepts/3.rendering.md → 3.guide/1.concepts/1.rendering.md} +8 -33
- package/{2.guide/2.concepts/2.vuejs-development.md → 3.guide/1.concepts/10.vuejs-development.md} +11 -10
- package/{2.guide/2.concepts/10.nuxt-lifecycle.md → 3.guide/1.concepts/2.nuxt-lifecycle.md} +34 -27
- package/{2.guide/2.concepts/1.auto-imports.md → 3.guide/1.concepts/3.auto-imports.md} +10 -10
- package/{2.guide/2.concepts → 3.guide/1.concepts}/4.server-engine.md +3 -3
- package/{2.guide/2.concepts → 3.guide/1.concepts}/5.modules.md +15 -2
- package/{2.guide/2.concepts → 3.guide/1.concepts}/7.esm.md +5 -4
- package/{2.guide/2.concepts → 3.guide/1.concepts}/8.typescript.md +11 -17
- package/{2.guide/2.concepts → 3.guide/1.concepts}/9.code-style.md +1 -1
- package/{2.guide/5.best-practices → 3.guide/2.best-practices}/hydration.md +1 -1
- package/{2.guide/5.best-practices → 3.guide/2.best-practices}/performance.md +5 -5
- package/3.guide/3.ai/.navigation.yml +3 -0
- package/3.guide/3.ai/1.mcp.md +277 -0
- package/3.guide/3.ai/2.llms-txt.md +65 -0
- package/3.guide/4.modules/.navigation.yml +3 -0
- package/3.guide/4.modules/1.getting-started.md +103 -0
- package/3.guide/4.modules/2.module-anatomy.md +138 -0
- package/3.guide/4.modules/3.recipes-basics.md +330 -0
- package/3.guide/4.modules/4.recipes-advanced.md +243 -0
- package/3.guide/4.modules/5.testing.md +76 -0
- package/3.guide/4.modules/6.best-practices.md +104 -0
- package/3.guide/4.modules/7.ecosystem.md +32 -0
- package/3.guide/4.modules/index.md +36 -0
- package/{2.guide/4.recipes → 3.guide/5.recipes}/1.custom-routing.md +4 -4
- package/{2.guide/4.recipes → 3.guide/5.recipes}/2.vite-plugin.md +5 -1
- package/{2.guide/4.recipes → 3.guide/5.recipes}/3.custom-usefetch.md +3 -3
- package/{2.guide/4.recipes → 3.guide/5.recipes}/4.sessions-and-authentication.md +2 -2
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.events.md +3 -4
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.experimental-features.md +212 -87
- package/3.guide/6.going-further/1.features.md +108 -0
- package/{2.guide/3.going-further → 3.guide/6.going-further}/1.internals.md +5 -4
- package/{2.guide/3.going-further → 3.guide/6.going-further}/10.runtime-config.md +3 -3
- package/{2.guide/3.going-further → 3.guide/6.going-further}/11.nightly-release-channel.md +1 -1
- package/{2.guide/3.going-further → 3.guide/6.going-further}/2.hooks.md +7 -7
- package/{2.guide/3.going-further → 3.guide/6.going-further}/4.kit.md +3 -3
- package/{2.guide/3.going-further → 3.guide/6.going-further}/6.nuxt-app.md +6 -6
- package/{2.guide/3.going-further → 3.guide/6.going-further}/7.layers.md +40 -14
- package/{2.guide/3.going-further → 3.guide/6.going-further}/9.debugging.md +3 -2
- package/{3.api → 4.api}/1.components/1.nuxt-client-fallback.md +5 -1
- package/{3.api → 4.api}/1.components/10.nuxt-picture.md +1 -1
- package/{3.api → 4.api}/1.components/11.teleports.md +2 -2
- package/{3.api → 4.api}/1.components/12.nuxt-route-announcer.md +1 -3
- package/{3.api → 4.api}/1.components/13.nuxt-time.md +0 -2
- package/{3.api → 4.api}/1.components/2.nuxt-page.md +4 -4
- package/{3.api → 4.api}/1.components/3.nuxt-layout.md +7 -7
- package/{3.api → 4.api}/1.components/4.nuxt-link.md +13 -13
- package/{3.api → 4.api}/1.components/5.nuxt-loading-indicator.md +2 -2
- package/{3.api → 4.api}/1.components/6.nuxt-error-boundary.md +2 -2
- package/{3.api → 4.api}/1.components/7.nuxt-welcome.md +2 -2
- package/{3.api → 4.api}/1.components/8.nuxt-island.md +9 -2
- package/{3.api → 4.api}/2.composables/use-app-config.md +1 -1
- package/{3.api → 4.api}/2.composables/use-async-data.md +5 -5
- package/4.api/2.composables/use-cookie.md +183 -0
- package/{3.api → 4.api}/2.composables/use-error.md +3 -3
- package/{3.api → 4.api}/2.composables/use-fetch.md +37 -37
- package/{3.api → 4.api}/2.composables/use-head-safe.md +1 -1
- package/{3.api → 4.api}/2.composables/use-head.md +22 -7
- package/{3.api → 4.api}/2.composables/use-lazy-async-data.md +1 -1
- package/{3.api → 4.api}/2.composables/use-lazy-fetch.md +9 -9
- package/{3.api → 4.api}/2.composables/use-nuxt-app.md +11 -9
- package/{3.api → 4.api}/2.composables/use-response-header.md +1 -1
- package/{3.api → 4.api}/2.composables/use-route-announcer.md +0 -2
- package/{3.api → 4.api}/2.composables/use-route.md +2 -2
- package/4.api/2.composables/use-router.md +94 -0
- package/{3.api → 4.api}/2.composables/use-runtime-config.md +5 -5
- package/{3.api → 4.api}/2.composables/use-seo-meta.md +2 -2
- package/{3.api → 4.api}/2.composables/use-server-seo-meta.md +2 -2
- package/{3.api → 4.api}/2.composables/use-state.md +12 -2
- package/{3.api → 4.api}/3.utils/$fetch.md +2 -2
- package/{3.api → 4.api}/3.utils/abort-navigation.md +3 -3
- package/{3.api → 4.api}/3.utils/add-route-middleware.md +1 -1
- package/{3.api → 4.api}/3.utils/call-once.md +2 -4
- package/{3.api → 4.api}/3.utils/clear-error.md +1 -1
- package/{3.api → 4.api}/3.utils/create-error.md +7 -7
- package/{3.api → 4.api}/3.utils/define-lazy-hydration-component.md +5 -5
- package/{3.api → 4.api}/3.utils/define-nuxt-component.md +1 -1
- package/{3.api → 4.api}/3.utils/define-nuxt-plugin.md +12 -12
- package/{3.api → 4.api}/3.utils/define-nuxt-route-middleware.md +3 -3
- package/{3.api → 4.api}/3.utils/define-page-meta.md +23 -16
- package/{3.api → 4.api}/3.utils/define-route-rules.md +2 -2
- package/{3.api → 4.api}/3.utils/navigate-to.md +14 -14
- package/{3.api → 4.api}/3.utils/on-before-route-leave.md +1 -1
- package/{3.api → 4.api}/3.utils/on-before-route-update.md +1 -1
- package/{3.api → 4.api}/3.utils/preload-route-components.md +2 -2
- package/{3.api → 4.api}/3.utils/refresh-cookie.md +0 -2
- package/{3.api → 4.api}/3.utils/refresh-nuxt-data.md +1 -1
- package/{3.api → 4.api}/3.utils/reload-nuxt-app.md +1 -1
- package/{3.api → 4.api}/3.utils/set-page-layout.md +36 -0
- package/{3.api → 4.api}/3.utils/set-response-status.md +3 -3
- package/{3.api → 4.api}/3.utils/show-error.md +4 -4
- package/{3.api → 4.api}/3.utils/update-app-config.md +2 -2
- package/{3.api → 4.api}/4.commands/add.md +11 -11
- package/4.api/4.commands/analyze.md +42 -0
- package/4.api/4.commands/build-module.md +42 -0
- package/4.api/4.commands/build.md +47 -0
- package/{3.api → 4.api}/4.commands/cleanup.md +6 -6
- package/4.api/4.commands/dev.md +60 -0
- package/{3.api → 4.api}/4.commands/devtools.md +7 -7
- package/4.api/4.commands/generate.md +42 -0
- package/4.api/4.commands/info.md +33 -0
- package/4.api/4.commands/init.md +50 -0
- package/4.api/4.commands/module.md +84 -0
- package/4.api/4.commands/prepare.md +41 -0
- package/4.api/4.commands/preview.md +44 -0
- package/4.api/4.commands/test.md +40 -0
- package/4.api/4.commands/typecheck.md +44 -0
- package/4.api/4.commands/upgrade.md +37 -0
- package/{3.api → 4.api}/5.kit/1.modules.md +31 -18
- package/{3.api → 4.api}/5.kit/10.templates.md +23 -23
- package/{3.api → 4.api}/5.kit/11.nitro.md +36 -36
- package/{3.api → 4.api}/5.kit/12.resolving.md +2 -2
- package/{3.api → 4.api}/5.kit/14.builder.md +35 -23
- package/{3.api → 4.api}/5.kit/16.layers.md +16 -16
- package/{3.api → 4.api}/5.kit/2.programmatic.md +2 -2
- package/{3.api → 4.api}/5.kit/3.compatibility.md +2 -2
- package/{3.api → 4.api}/5.kit/4.autoimports.md +18 -18
- package/4.api/5.kit/5.components.md +146 -0
- package/{3.api → 4.api}/5.kit/6.context.md +1 -1
- package/{3.api → 4.api}/5.kit/8.layout.md +1 -1
- package/4.api/6.advanced/1.hooks.md +105 -0
- package/{3.api → 4.api}/index.md +7 -7
- package/5.community/4.contribution.md +10 -10
- package/5.community/5.framework-contribution.md +9 -9
- package/5.community/6.roadmap.md +25 -25
- package/5.community/7.changelog.md +20 -0
- package/6.bridge/1.overview.md +9 -1
- package/6.bridge/3.bridge-composition-api.md +2 -2
- package/6.bridge/4.plugins-and-middleware.md +4 -4
- package/6.bridge/5.nuxt3-compatible-api.md +1 -1
- package/6.bridge/8.nitro.md +4 -0
- package/7.migration/10.bundling.md +1 -1
- package/7.migration/11.server.md +3 -3
- package/7.migration/2.configuration.md +6 -6
- package/7.migration/20.module-authors.md +3 -3
- package/7.migration/3.auto-imports.md +1 -1
- package/7.migration/4.meta.md +1 -1
- package/7.migration/5.plugins-and-middleware.md +3 -3
- package/7.migration/6.pages-and-layouts.md +9 -9
- package/7.migration/7.component-options.md +6 -6
- package/7.migration/8.runtime-config.md +1 -1
- package/package.json +1 -1
- package/2.guide/0.index.md +0 -25
- package/2.guide/3.going-further/1.features.md +0 -103
- package/2.guide/3.going-further/3.modules.md +0 -901
- package/3.api/2.composables/use-cookie.md +0 -183
- package/3.api/2.composables/use-router.md +0 -94
- package/3.api/4.commands/analyze.md +0 -42
- package/3.api/4.commands/build-module.md +0 -42
- package/3.api/4.commands/build.md +0 -47
- package/3.api/4.commands/dev.md +0 -60
- package/3.api/4.commands/generate.md +0 -42
- package/3.api/4.commands/info.md +0 -33
- package/3.api/4.commands/init.md +0 -50
- package/3.api/4.commands/module.md +0 -84
- package/3.api/4.commands/prepare.md +0 -41
- package/3.api/4.commands/preview.md +0 -44
- package/3.api/4.commands/test.md +0 -40
- package/3.api/4.commands/typecheck.md +0 -44
- package/3.api/4.commands/upgrade.md +0 -37
- package/3.api/5.kit/5.components.md +0 -146
- package/3.api/6.advanced/1.hooks.md +0 -105
- /package/{2.guide/1.directory-structure → 2.directory-structure}/.navigation.yml +0 -0
- /package/{2.guide/1.directory-structure → 2.directory-structure}/2.gitignore.md +0 -0
- /package/{2.guide → 3.guide}/.navigation.yml +0 -0
- /package/{2.guide/2.concepts → 3.guide/1.concepts}/.navigation.yml +0 -0
- /package/{2.guide/5.best-practices → 3.guide/2.best-practices}/.navigation.yml +0 -0
- /package/{2.guide/5.best-practices → 3.guide/2.best-practices}/plugins.md +0 -0
- /package/{2.guide/4.recipes → 3.guide/5.recipes}/.navigation.yml +0 -0
- /package/{2.guide/3.going-further → 3.guide/6.going-further}/.navigation.yml +0 -0
- /package/{2.guide/3.going-further → 3.guide/6.going-further}/index.md +0 -0
- /package/{3.api → 4.api}/.navigation.yml +0 -0
- /package/{3.api → 4.api}/1.components/.navigation.yml +0 -0
- /package/{3.api → 4.api}/1.components/1.client-only.md +0 -0
- /package/{3.api → 4.api}/1.components/1.dev-only.md +0 -0
- /package/{3.api → 4.api}/1.components/9.nuxt-img.md +0 -0
- /package/{3.api → 4.api}/2.composables/.navigation.yml +0 -0
- /package/{3.api → 4.api}/2.composables/on-prehydrate.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-hydration.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-loading-indicator.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-nuxt-data.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-preview-mode.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-event.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-fetch.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-header.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-headers.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-request-url.md +0 -0
- /package/{3.api → 4.api}/2.composables/use-runtime-hook.md +0 -0
- /package/{3.api → 4.api}/3.utils/.navigation.yml +0 -0
- /package/{3.api → 4.api}/3.utils/clear-nuxt-data.md +0 -0
- /package/{3.api → 4.api}/3.utils/clear-nuxt-state.md +0 -0
- /package/{3.api → 4.api}/3.utils/on-nuxt-ready.md +0 -0
- /package/{3.api → 4.api}/3.utils/prefetch-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/preload-components.md +0 -0
- /package/{3.api → 4.api}/3.utils/prerender-routes.md +0 -0
- /package/{3.api → 4.api}/4.commands/.navigation.yml +0 -0
- /package/{3.api → 4.api}/5.kit/.navigation.yml +0 -0
- /package/{3.api → 4.api}/5.kit/10.runtime-config.md +0 -0
- /package/{3.api → 4.api}/5.kit/13.logging.md +0 -0
- /package/{3.api → 4.api}/5.kit/15.examples.md +0 -0
- /package/{3.api → 4.api}/5.kit/7.pages.md +0 -0
- /package/{3.api → 4.api}/5.kit/9.head.md +0 -0
- /package/{3.api → 4.api}/5.kit/9.plugins.md +0 -0
- /package/{3.api → 4.api}/6.advanced/.navigation.yml +0 -0
- /package/{3.api → 4.api}/6.advanced/2.import-meta.md +0 -0
- /package/{3.api → 4.api}/6.nuxt-config.md +0 -0
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "useRouter"
|
|
3
|
+
description: "The useRouter composable returns the router instance."
|
|
4
|
+
links:
|
|
5
|
+
- label: Source
|
|
6
|
+
icon: i-simple-icons-github
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/composables/router.ts
|
|
8
|
+
size: xs
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
```vue [pages/index.vue]
|
|
12
|
+
<script setup lang="ts">
|
|
13
|
+
const router = useRouter()
|
|
14
|
+
</script>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
If you only need the router instance within your template, use `$router`:
|
|
18
|
+
|
|
19
|
+
```vue [pages/index.vue]
|
|
20
|
+
<template>
|
|
21
|
+
<button @click="$router.back()">
|
|
22
|
+
Back
|
|
23
|
+
</button>
|
|
24
|
+
</template>
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
If you have a `pages/` directory, `useRouter` is identical in behavior to the one provided by `vue-router`.
|
|
28
|
+
|
|
29
|
+
::read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/interfaces/router#Properties-currentRoute-" target="_blank"}
|
|
30
|
+
Read `vue-router` documentation about the `Router` interface.
|
|
31
|
+
::
|
|
32
|
+
|
|
33
|
+
## Basic Manipulation
|
|
34
|
+
|
|
35
|
+
- [`addRoute()`](https://router.vuejs.org/api/interfaces/router#addRoute-): Add a new route to the router instance. `parentName` can be provided to add new route as the child of an existing route.
|
|
36
|
+
- [`removeRoute()`](https://router.vuejs.org/api/interfaces/router#removeRoute-): Remove an existing route by its name.
|
|
37
|
+
- [`getRoutes()`](https://router.vuejs.org/api/interfaces/router#getRoutes-): Get a full list of all the route records.
|
|
38
|
+
- [`hasRoute()`](https://router.vuejs.org/api/interfaces/router#hasRoute-): Checks if a route with a given name exists.
|
|
39
|
+
- [`resolve()`](https://router.vuejs.org/api/interfaces/router#resolve-): Returns the normalized version of a route location. Also includes an `href` property that includes any existing base.
|
|
40
|
+
|
|
41
|
+
```ts [Example]
|
|
42
|
+
const router = useRouter()
|
|
43
|
+
|
|
44
|
+
router.addRoute({ name: 'home', path: '/home', component: Home })
|
|
45
|
+
router.removeRoute('home')
|
|
46
|
+
router.getRoutes()
|
|
47
|
+
router.hasRoute('home')
|
|
48
|
+
router.resolve({ name: 'home' })
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
::note
|
|
52
|
+
`router.addRoute()` adds route details into an array of routes and it is useful while building [Nuxt plugins](/docs/3.x/directory-structure/plugins) while `router.push()` on the other hand, triggers a new navigation immediately and it is useful in pages, Vue components and composable.
|
|
53
|
+
::
|
|
54
|
+
|
|
55
|
+
## Based on History API
|
|
56
|
+
|
|
57
|
+
- [`back()`](https://router.vuejs.org/api/interfaces/router#back-): Go back in history if possible, same as `router.go(-1)`.
|
|
58
|
+
- [`forward()`](https://router.vuejs.org/api/interfaces/router#forward-): Go forward in history if possible, same as `router.go(1)`.
|
|
59
|
+
- [`go()`](https://router.vuejs.org/api/interfaces/router#go-): Move forward or backward through the history without the hierarchical restrictions enforced in `router.back()` and `router.forward()`.
|
|
60
|
+
- [`push()`](https://router.vuejs.org/api/interfaces/router#push-): Programmatically navigate to a new URL by pushing an entry in the history stack. **It is recommended to use [`navigateTo`](/docs/3.x/api/utils/navigate-to) instead.**
|
|
61
|
+
- [`replace()`](https://router.vuejs.org/api/interfaces/router#replace-): Programmatically navigate to a new URL by replacing the current entry in the routes history stack. **It is recommended to use [`navigateTo`](/docs/3.x/api/utils/navigate-to) instead.**
|
|
62
|
+
|
|
63
|
+
```ts [Example]
|
|
64
|
+
const router = useRouter()
|
|
65
|
+
|
|
66
|
+
router.back()
|
|
67
|
+
router.forward()
|
|
68
|
+
router.go(3)
|
|
69
|
+
router.push({ path: '/home' })
|
|
70
|
+
router.replace({ hash: '#bio' })
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
::read-more{icon="i-simple-icons-mdnwebdocs" to="https://developer.mozilla.org/en-US/docs/Web/API/History" target="_blank"}
|
|
74
|
+
Read more about the browser's History API.
|
|
75
|
+
::
|
|
76
|
+
|
|
77
|
+
## Navigation Guards
|
|
78
|
+
|
|
79
|
+
`useRouter` composable provides `afterEach`, `beforeEach` and `beforeResolve` helper methods that acts as navigation guards.
|
|
80
|
+
|
|
81
|
+
However, Nuxt has a concept of **route middleware** that simplifies the implementation of navigation guards and provides a better developer experience.
|
|
82
|
+
|
|
83
|
+
:read-more{to="/docs/3.x/directory-structure/middleware"}
|
|
84
|
+
|
|
85
|
+
## Promise and Error Handling
|
|
86
|
+
|
|
87
|
+
- [`isReady()`](https://router.vuejs.org/api/interfaces/router#isReady-): Returns a Promise that resolves when the router has completed the initial navigation.
|
|
88
|
+
- [`onError`](https://router.vuejs.org/api/interfaces/router#onError-): Adds an error handler that is called every time a non caught error happens during navigation.
|
|
89
|
+
|
|
90
|
+
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/interfaces/router#Methods-" title="Vue Router Docs" target="_blank"}
|
|
91
|
+
|
|
92
|
+
## Universal Router Instance
|
|
93
|
+
|
|
94
|
+
If you do not have a `pages/` folder, then [`useRouter`](/docs/3.x/api/composables/use-router) will return a universal router instance with similar helper methods, but be aware that not all features may be supported or behave in exactly the same way as with `vue-router`.
|
|
@@ -22,7 +22,7 @@ export default defineEventHandler((event) => {
|
|
|
22
22
|
})
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
-
:read-more{to="/docs/guide/going-further/runtime-config"}
|
|
25
|
+
:read-more{to="/docs/3.x/guide/going-further/runtime-config"}
|
|
26
26
|
|
|
27
27
|
## Define Runtime Config
|
|
28
28
|
|
|
@@ -48,7 +48,7 @@ export default defineNuxtConfig({
|
|
|
48
48
|
Variables that need to be accessible on the server are added directly inside `runtimeConfig`. Variables that need to be accessible on both the client and the server are defined in `runtimeConfig.public`.
|
|
49
49
|
::
|
|
50
50
|
|
|
51
|
-
:read-more{to="/docs/guide/going-further/runtime-config"}
|
|
51
|
+
:read-more{to="/docs/3.x/guide/going-further/runtime-config"}
|
|
52
52
|
|
|
53
53
|
## Access Runtime Config
|
|
54
54
|
|
|
@@ -76,7 +76,7 @@ In this example, since `apiBase` is defined within the `public` namespace, it is
|
|
|
76
76
|
|
|
77
77
|
It is possible to update runtime config values using a matching environment variable name prefixed with `NUXT_`.
|
|
78
78
|
|
|
79
|
-
:read-more{to="/docs/guide/going-further/runtime-config"}
|
|
79
|
+
:read-more{to="/docs/3.x/guide/going-further/runtime-config"}
|
|
80
80
|
|
|
81
81
|
### Using the `.env` File
|
|
82
82
|
|
|
@@ -95,7 +95,7 @@ Any environment variables set within `.env` file are accessed using `process.env
|
|
|
95
95
|
In **production runtime**, you should use platform environment variables and `.env` is not used.
|
|
96
96
|
::
|
|
97
97
|
|
|
98
|
-
:read-more{to="/docs/
|
|
98
|
+
:read-more{to="/docs/3.x/directory-structure/env"}
|
|
99
99
|
|
|
100
100
|
## `app` namespace
|
|
101
101
|
|
|
@@ -139,4 +139,4 @@ export default defineEventHandler((event) => {
|
|
|
139
139
|
})
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
:read-more{to="/docs/guide/going-further/runtime-config"}
|
|
142
|
+
:read-more{to="/docs/3.x/guide/going-further/runtime-config"}
|
|
@@ -14,7 +14,7 @@ This helps you avoid common mistakes, such as using `name` instead of `property`
|
|
|
14
14
|
This is the recommended way to add meta tags to your site as it is XSS safe and has full TypeScript support.
|
|
15
15
|
::
|
|
16
16
|
|
|
17
|
-
:read-more{to="/docs/getting-started/seo-meta"}
|
|
17
|
+
:read-more{to="/docs/3.x/getting-started/seo-meta"}
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
@@ -48,7 +48,7 @@ useSeoMeta({
|
|
|
48
48
|
|
|
49
49
|
There are over 100 parameters. See the [full list of parameters in the source code](https://github.com/harlan-zw/zhead/blob/main/packages/zhead/src/metaFlat.ts#L1035).
|
|
50
50
|
|
|
51
|
-
:read-more{to="/docs/getting-started/seo-meta"}
|
|
51
|
+
:read-more{to="/docs/3.x/getting-started/seo-meta"}
|
|
52
52
|
|
|
53
53
|
## Performance
|
|
54
54
|
|
|
@@ -10,7 +10,7 @@ links:
|
|
|
10
10
|
|
|
11
11
|
Just like [`useSeoMeta`](/docs/3.x/api/composables/use-seo-meta), `useServerSeoMeta` composable lets you define your site's SEO meta tags as a flat object with full TypeScript support.
|
|
12
12
|
|
|
13
|
-
:read-more{to="/docs/api/composables/use-seo-meta"}
|
|
13
|
+
:read-more{to="/docs/3.x/api/composables/use-seo-meta"}
|
|
14
14
|
|
|
15
15
|
In most instances, the meta doesn't need to be reactive as robots will only scan the initial load. So we recommend using [`useServerSeoMeta`](/docs/3.x/api/composables/use-server-seo-meta) as a performance-focused utility that will not do anything (or return a `head` object) on the client.
|
|
16
16
|
|
|
@@ -24,4 +24,4 @@ useServerSeoMeta({
|
|
|
24
24
|
|
|
25
25
|
Parameters are exactly the same as with [`useSeoMeta`](/docs/3.x/api/composables/use-seo-meta)
|
|
26
26
|
|
|
27
|
-
:read-more{to="/docs/getting-started/seo-meta"}
|
|
27
|
+
:read-more{to="/docs/3.x/getting-started/seo-meta"}
|
|
@@ -15,7 +15,7 @@ links:
|
|
|
15
15
|
const count = useState('counter', () => Math.round(Math.random() * 100))
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
-
:read-more{to="/docs/getting-started/state-management"}
|
|
18
|
+
:read-more{to="/docs/3.x/getting-started/state-management"}
|
|
19
19
|
|
|
20
20
|
::important
|
|
21
21
|
Because the data inside `useState` will be serialized to JSON, it is important that it does not contain anything that cannot be serialized, such as classes, functions or symbols.
|
|
@@ -29,7 +29,7 @@ Because the data inside `useState` will be serialized to JSON, it is important t
|
|
|
29
29
|
|
|
30
30
|
## Using `shallowRef`
|
|
31
31
|
|
|
32
|
-
If you don't need your state to be deeply reactive, you can combine `useState` with [`shallowRef`](https://vuejs.org/api/reactivity-advanced
|
|
32
|
+
If you don't need your state to be deeply reactive, you can combine `useState` with [`shallowRef`](https://vuejs.org/api/reactivity-advanced#shallowref). This can improve performance when your state contains large objects and arrays.
|
|
33
33
|
|
|
34
34
|
```ts
|
|
35
35
|
const state = useState('my-shallow-state', () => shallowRef({ deep: 'not reactive' }))
|
|
@@ -46,3 +46,13 @@ export function useState<T> (key: string, init?: () => T | Ref<T>): Ref<T>
|
|
|
46
46
|
- `key`: A unique key ensuring that data fetching is properly de-duplicated across requests. If you do not provide a key, then a key that is unique to the file and line number of the instance of [`useState`](/docs/3.x/api/composables/use-state) will be generated for you.
|
|
47
47
|
- `init`: A function that provides initial value for the state when not initiated. This function can also return a `Ref`.
|
|
48
48
|
- `T`: (typescript only) Specify the type of state
|
|
49
|
+
|
|
50
|
+
## Troubleshooting
|
|
51
|
+
|
|
52
|
+
### `Cannot stringify arbitrary non-POJOs`
|
|
53
|
+
|
|
54
|
+
This error occurs when you try to store a non-serializable payload with `useState`, such as class instances.
|
|
55
|
+
|
|
56
|
+
If you want to store class instances with `useState` that are not supported by Nuxt, you can use [`definePayloadPlugin`](/docs/3.x/api/composables/use-nuxt-app#custom-reducerreviver) to add a custom serializer and deserializer for your classes.
|
|
57
|
+
|
|
58
|
+
:read-more{to="/docs/3.x/api/composables/use-nuxt-app#payload"}
|
|
@@ -11,7 +11,7 @@ links:
|
|
|
11
11
|
Nuxt uses [ofetch](https://github.com/unjs/ofetch) to expose globally the `$fetch` helper for making HTTP requests within your Vue app or API routes.
|
|
12
12
|
|
|
13
13
|
::tip{icon="i-lucide-rocket"}
|
|
14
|
-
During server-side rendering, calling `$fetch` to fetch your internal [API routes](/docs/3.x/
|
|
14
|
+
During server-side rendering, calling `$fetch` to fetch your internal [API routes](/docs/3.x/directory-structure/server) will directly call the relevant function (emulating the request), **saving an additional API call**.
|
|
15
15
|
::
|
|
16
16
|
|
|
17
17
|
::note{color="blue" icon="i-lucide-info"}
|
|
@@ -35,7 +35,7 @@ const { data } = await useFetch('/api/item')
|
|
|
35
35
|
</script>
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
:read-more{to="/docs/getting-started/data-fetching"}
|
|
38
|
+
:read-more{to="/docs/3.x/getting-started/data-fetching"}
|
|
39
39
|
|
|
40
40
|
You can use `$fetch` in any methods that are executed only on client-side.
|
|
41
41
|
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
::warning
|
|
12
|
-
`abortNavigation` is only usable inside a [route middleware handler](/docs/3.x/
|
|
12
|
+
`abortNavigation` is only usable inside a [route middleware handler](/docs/3.x/directory-structure/middleware).
|
|
13
13
|
::
|
|
14
14
|
|
|
15
15
|
## Type
|
|
@@ -22,7 +22,7 @@ export function abortNavigation (err?: Error | string): false
|
|
|
22
22
|
|
|
23
23
|
### `err`
|
|
24
24
|
|
|
25
|
-
- **Type**: [`Error`](https://developer.mozilla.org/
|
|
25
|
+
- **Type**: [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) | `string`
|
|
26
26
|
|
|
27
27
|
Optional error to be thrown by `abortNavigation`.
|
|
28
28
|
|
|
@@ -60,7 +60,7 @@ export default defineNuxtRouteMiddleware((to, from) => {
|
|
|
60
60
|
|
|
61
61
|
### `err` as an Error Object
|
|
62
62
|
|
|
63
|
-
You can pass the error as an [`Error`](https://developer.mozilla.org/
|
|
63
|
+
You can pass the error as an [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) object, e.g. caught by the `catch`-block:
|
|
64
64
|
|
|
65
65
|
```ts [middleware/auth.ts]
|
|
66
66
|
export default defineNuxtRouteMiddleware((to, from) => {
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
::note
|
|
12
|
-
Route middleware are navigation guards stored in the [`middleware/`](/docs/3.x/
|
|
12
|
+
Route middleware are navigation guards stored in the [`middleware/`](/docs/3.x/directory-structure/middleware) directory of your Nuxt application (unless [set otherwise](/docs/3.x/api/nuxt-config#middleware)).
|
|
13
13
|
::
|
|
14
14
|
|
|
15
15
|
## Type
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: "callOnce"
|
|
3
3
|
description: "Run a given function or block of code once during SSR or CSR."
|
|
4
|
-
navigation:
|
|
5
|
-
badge: New
|
|
6
4
|
links:
|
|
7
5
|
- label: Source
|
|
8
6
|
icon: i-simple-icons-github
|
|
@@ -54,11 +52,11 @@ await callOnce(async () => {
|
|
|
54
52
|
`navigation` mode is available since [Nuxt v3.15](/blog/v3-15).
|
|
55
53
|
::
|
|
56
54
|
|
|
57
|
-
::tip{to="/docs/getting-started/state-management#usage-with-pinia"}
|
|
55
|
+
::tip{to="/docs/3.x/getting-started/state-management#usage-with-pinia"}
|
|
58
56
|
`callOnce` is useful in combination with the [Pinia module](/modules/pinia) to call store actions.
|
|
59
57
|
::
|
|
60
58
|
|
|
61
|
-
:read-more{to="/docs/getting-started/state-management"}
|
|
59
|
+
:read-more{to="/docs/3.x/getting-started/state-management"}
|
|
62
60
|
|
|
63
61
|
::warning
|
|
64
62
|
Note that `callOnce` doesn't return anything. You should use [`useAsyncData`](/docs/3.x/api/composables/use-async-data) or [`useFetch`](/docs/3.x/api/composables/use-fetch) if you want to do data fetching during SSR.
|
|
@@ -26,4 +26,4 @@ clearError({ redirect: '/homepage' })
|
|
|
26
26
|
|
|
27
27
|
Errors are set in state using [`useError()`](/docs/3.x/api/composables/use-error). The `clearError` composable will reset this state and calls the `app:error:cleared` hook with the provided options.
|
|
28
28
|
|
|
29
|
-
:read-more{to="/docs/getting-started/error-handling"}
|
|
29
|
+
:read-more{to="/docs/3.x/getting-started/error-handling"}
|
|
@@ -12,9 +12,9 @@ You can use this function to create an error object with additional metadata. It
|
|
|
12
12
|
|
|
13
13
|
## Parameters
|
|
14
14
|
|
|
15
|
-
- `err`: `string | { cause, data, message, name, stack,
|
|
15
|
+
- `err`: `string | { cause, data, message, name, stack, status, statusText, fatal }`
|
|
16
16
|
|
|
17
|
-
You can pass either a string or an object to the `createError` function. If you pass a string, it will be used as the error `message`, and the `
|
|
17
|
+
You can pass either a string or an object to the `createError` function. If you pass a string, it will be used as the error `message`, and the `status` will default to `500`. If you pass an object, you can set multiple properties of the error, such as `status`, `message`, and other error properties.
|
|
18
18
|
|
|
19
19
|
## In Vue App
|
|
20
20
|
|
|
@@ -30,7 +30,7 @@ If you throw an error created with `createError`:
|
|
|
30
30
|
const route = useRoute()
|
|
31
31
|
const { data } = await useFetch(`/api/movies/${route.params.slug}`)
|
|
32
32
|
if (!data.value) {
|
|
33
|
-
throw createError({
|
|
33
|
+
throw createError({ status: 404, statusText: 'Page Not Found' })
|
|
34
34
|
}
|
|
35
35
|
</script>
|
|
36
36
|
```
|
|
@@ -44,12 +44,12 @@ Use `createError` to trigger error handling in server API routes.
|
|
|
44
44
|
```ts [server/api/error.ts]
|
|
45
45
|
export default eventHandler(() => {
|
|
46
46
|
throw createError({
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
status: 404,
|
|
48
|
+
statusText: 'Page Not Found',
|
|
49
49
|
})
|
|
50
50
|
})
|
|
51
51
|
```
|
|
52
52
|
|
|
53
|
-
In API routes, using `createError` by passing an object with a short `
|
|
53
|
+
In API routes, using `createError` by passing an object with a short `statusText` is recommended because it can be accessed on the client side. Otherwise, a `message` passed to `createError` on an API route will not propagate to the client. Alternatively, you can use the `data` property to pass data back to the client. In any case, always consider avoiding to put dynamic user input to the message to avoid potential security issues.
|
|
54
54
|
|
|
55
|
-
:read-more{to="/docs/getting-started/error-handling"}
|
|
55
|
+
:read-more{to="/docs/3.x/getting-started/error-handling"}
|
|
@@ -4,7 +4,7 @@ description: 'Define a lazy hydration component with a specific strategy.'
|
|
|
4
4
|
links:
|
|
5
5
|
- label: Source
|
|
6
6
|
icon: i-simple-icons-github
|
|
7
|
-
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/components/plugins/lazy-hydration-macro-transform.ts
|
|
7
|
+
to: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/components/plugins/lazy-hydration-macro-transform.ts
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
@@ -42,7 +42,7 @@ Read more about the options for `hydrate-on-visible`.
|
|
|
42
42
|
::
|
|
43
43
|
|
|
44
44
|
::note
|
|
45
|
-
Under the hood, this uses Vue's built-in [`hydrateOnVisible` strategy](https://vuejs.org/guide/components/async
|
|
45
|
+
Under the hood, this uses Vue's built-in [`hydrateOnVisible` strategy](https://vuejs.org/guide/components/async#hydrate-on-visible).
|
|
46
46
|
::
|
|
47
47
|
|
|
48
48
|
### Idle Strategy
|
|
@@ -70,7 +70,7 @@ The `hydrateOnIdle` prop is optional. You can pass a positive number to specify
|
|
|
70
70
|
Idle strategy is for components that can be hydrated when the browser is idle.
|
|
71
71
|
|
|
72
72
|
::note
|
|
73
|
-
Under the hood, this uses Vue's built-in [`hydrateOnIdle` strategy](https://vuejs.org/guide/components/async
|
|
73
|
+
Under the hood, this uses Vue's built-in [`hydrateOnIdle` strategy](https://vuejs.org/guide/components/async#hydrate-on-idle).
|
|
74
74
|
::
|
|
75
75
|
|
|
76
76
|
### Interaction Strategy
|
|
@@ -99,7 +99,7 @@ const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
|
99
99
|
The `hydrateOnInteraction` prop is optional. If you do not pass an event or a list of events, it defaults to hydrating on `pointerenter`, `click`, and `focus`.
|
|
100
100
|
|
|
101
101
|
::note
|
|
102
|
-
Under the hood, this uses Vue's built-in [`hydrateOnInteraction` strategy](https://vuejs.org/guide/components/async
|
|
102
|
+
Under the hood, this uses Vue's built-in [`hydrateOnInteraction` strategy](https://vuejs.org/guide/components/async#hydrate-on-interaction).
|
|
103
103
|
::
|
|
104
104
|
|
|
105
105
|
### Media Query Strategy
|
|
@@ -126,7 +126,7 @@ const LazyHydrationMyComponent = defineLazyHydrationComponent(
|
|
|
126
126
|
```
|
|
127
127
|
|
|
128
128
|
::note
|
|
129
|
-
Under the hood, this uses Vue's built-in [`hydrateOnMediaQuery` strategy](https://vuejs.org/guide/components/async
|
|
129
|
+
Under the hood, this uses Vue's built-in [`hydrateOnMediaQuery` strategy](https://vuejs.org/guide/components/async#hydrate-on-media-query).
|
|
130
130
|
::
|
|
131
131
|
|
|
132
132
|
### Time Strategy
|
|
@@ -9,7 +9,7 @@ links:
|
|
|
9
9
|
---
|
|
10
10
|
|
|
11
11
|
::note
|
|
12
|
-
`defineNuxtComponent()` is a helper function for defining type safe Vue components using options API similar to [`defineComponent()`](https://vuejs.org/api/general
|
|
12
|
+
`defineNuxtComponent()` is a helper function for defining type safe Vue components using options API similar to [`defineComponent()`](https://vuejs.org/api/general#definecomponent). `defineNuxtComponent()` wrapper also adds support for `asyncData` and `head` component options.
|
|
13
13
|
::
|
|
14
14
|
|
|
15
15
|
::note
|
|
@@ -16,7 +16,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
16
16
|
})
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
:read-more{to="/docs/
|
|
19
|
+
:read-more{to="/docs/3.x/directory-structure/plugins#creating-plugins"}
|
|
20
20
|
|
|
21
21
|
## Type
|
|
22
22
|
|
|
@@ -42,19 +42,19 @@ interface ObjectPlugin<T> {
|
|
|
42
42
|
## Parameters
|
|
43
43
|
|
|
44
44
|
**plugin**: A plugin can be defined in two ways:
|
|
45
|
-
1. **Function Plugin**: A function that receives the [`NuxtApp`](/docs/3.x/guide/going-further/internals#the-nuxtapp-interface) instance and can return a promise with
|
|
45
|
+
1. **Function Plugin**: A function that receives the [`NuxtApp`](/docs/3.x/guide/going-further/internals#the-nuxtapp-interface) instance and can return a promise with a potential object with a [`provide`](/docs/3.x/directory-structure/plugins#providing-helpers) property if you want to provide a helper on [`NuxtApp`](/docs/3.x/guide/going-further/internals#the-nuxtapp-interface) instance.
|
|
46
46
|
2. **Object Plugin**: An object that can include various properties to configure the plugin's behavior, such as `name`, `enforce`, `dependsOn`, `order`, `parallel`, `setup`, `hooks`, and `env`.
|
|
47
47
|
|
|
48
|
-
| Property
|
|
49
|
-
|
|
50
|
-
| `name`
|
|
51
|
-
| `enforce`
|
|
52
|
-
| `dependsOn` | `string[]`
|
|
53
|
-
| `order`
|
|
54
|
-
| `parallel`
|
|
55
|
-
| `setup`
|
|
56
|
-
| `hooks`
|
|
57
|
-
| `env`
|
|
48
|
+
| Property | Type | Required | Description |
|
|
49
|
+
|-------------|----------------------------------------|----------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
50
|
+
| `name` | `string` | `false` | Optional name for the plugin, useful for debugging and dependency management. |
|
|
51
|
+
| `enforce` | `'pre'` \| `'default'` \| `'post'` | `false` | Controls when the plugin runs relative to other plugins. |
|
|
52
|
+
| `dependsOn` | `string[]` | `false` | Array of plugin names this plugin depends on. Ensures proper execution order. |
|
|
53
|
+
| `order` | `number` | `false` | This allows more granular control over plugin order and should only be used by advanced users. **It overrides the value of `enforce` and is used to sort plugins.** |
|
|
54
|
+
| `parallel` | `boolean` | `false` | Whether to execute the plugin in parallel with other parallel plugins. |
|
|
55
|
+
| `setup` | `Plugin<T>`{lang="ts"} | `false` | The main plugin function, equivalent to a function plugin. |
|
|
56
|
+
| `hooks` | `Partial<RuntimeNuxtHooks>`{lang="ts"} | `false` | Nuxt app runtime hooks to register directly. |
|
|
57
|
+
| `env` | `{ islands?: boolean }`{lang="ts"} | `false` | Set this value to `false` if you don't want the plugin to run when rendering server-only or island components. |
|
|
58
58
|
|
|
59
59
|
:video-accordion{title="Watch a video from Alexander Lichter about the Object Syntax for Nuxt plugins" videoId="2aXZyXB1QGQ"}
|
|
60
60
|
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
Route middleware are stored in the [`middleware/`](/docs/3.x/
|
|
11
|
+
Route middleware are stored in the [`middleware/`](/docs/3.x/directory-structure/middleware) of your Nuxt application (unless [set otherwise](/docs/3.x/api/nuxt-config#middleware)).
|
|
12
12
|
|
|
13
13
|
## Type
|
|
14
14
|
|
|
@@ -28,7 +28,7 @@ interface RouteMiddleware {
|
|
|
28
28
|
|
|
29
29
|
A function that takes two Vue Router's route location objects as parameters: the next route `to` as the first, and the current route `from` as the second.
|
|
30
30
|
|
|
31
|
-
Learn more about available properties of `RouteLocationNormalized` in the **[Vue Router docs](https://router.vuejs.org/api/type-aliases/
|
|
31
|
+
Learn more about available properties of `RouteLocationNormalized` in the **[Vue Router docs](https://router.vuejs.org/api/type-aliases/routelocationnormalized)**.
|
|
32
32
|
|
|
33
33
|
## Examples
|
|
34
34
|
|
|
@@ -39,7 +39,7 @@ You can use route middleware to throw errors and show helpful error messages:
|
|
|
39
39
|
```ts [middleware/error.ts]
|
|
40
40
|
export default defineNuxtRouteMiddleware((to) => {
|
|
41
41
|
if (to.params.id === '1') {
|
|
42
|
-
throw createError({
|
|
42
|
+
throw createError({ status: 404, statusText: 'Page Not Found' })
|
|
43
43
|
}
|
|
44
44
|
})
|
|
45
45
|
```
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
`definePageMeta` is a compiler macro that you can use to set metadata for your **page** components located in the [`pages/`](/docs/3.x/
|
|
11
|
+
`definePageMeta` is a compiler macro that you can use to set metadata for your **page** components located in the [`pages/`](/docs/3.x/directory-structure/pages) directory (unless [set otherwise](/docs/3.x/api/nuxt-config#pages)). This way you can set custom metadata for each static or dynamic route of your Nuxt application.
|
|
12
12
|
|
|
13
13
|
```vue [pages/some-page.vue]
|
|
14
14
|
<script setup lang="ts">
|
|
@@ -18,7 +18,7 @@ definePageMeta({
|
|
|
18
18
|
</script>
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
:read-more{to="/docs/
|
|
21
|
+
:read-more{to="/docs/3.x/directory-structure/pages#page-metadata"}
|
|
22
22
|
|
|
23
23
|
## Type
|
|
24
24
|
|
|
@@ -32,6 +32,7 @@ interface PageMeta {
|
|
|
32
32
|
path?: string
|
|
33
33
|
props?: RouteRecordRaw['props']
|
|
34
34
|
alias?: string | string[]
|
|
35
|
+
groups?: string[]
|
|
35
36
|
pageTransition?: boolean | TransitionProps
|
|
36
37
|
layoutTransition?: boolean | TransitionProps
|
|
37
38
|
viewTransition?: boolean | 'always'
|
|
@@ -56,13 +57,13 @@ interface PageMeta {
|
|
|
56
57
|
|
|
57
58
|
- **Type**: `string`
|
|
58
59
|
|
|
59
|
-
You may define a name for this page's route. By default, name is generated based on path inside the [`pages/` directory](/docs/3.x/
|
|
60
|
+
You may define a name for this page's route. By default, name is generated based on path inside the [`pages/` directory](/docs/3.x/directory-structure/pages).
|
|
60
61
|
|
|
61
62
|
**`path`**
|
|
62
63
|
|
|
63
64
|
- **Type**: `string`
|
|
64
65
|
|
|
65
|
-
You may define a [custom regular expression](#using-a-custom-regular-expression) if you have a more complex pattern than can be expressed with the file name.
|
|
66
|
+
You may define a [custom regular expression](/docs/3.x/api/utils/define-page-meta#using-a-custom-regular-expression) if you have a more complex pattern than can be expressed with the file name.
|
|
66
67
|
|
|
67
68
|
**`props`**
|
|
68
69
|
|
|
@@ -76,11 +77,17 @@ interface PageMeta {
|
|
|
76
77
|
|
|
77
78
|
Aliases for the record. Allows defining extra paths that will behave like a copy of the record. Allows having paths shorthands like `/users/:id` and `/u/:id`. All `alias` and `path` values must share the same params.
|
|
78
79
|
|
|
80
|
+
**`groups`**
|
|
81
|
+
|
|
82
|
+
- **Type**: `string[]`
|
|
83
|
+
|
|
84
|
+
Route groups the page belongs to, based on the folder structure. Automatically populated for pages within [route groups](/docs/3.x/guide/directory-structure/app/pages#route-groups).
|
|
85
|
+
|
|
79
86
|
**`keepalive`**
|
|
80
87
|
|
|
81
|
-
- **Type**: `boolean` | [`KeepAliveProps`](https://vuejs.org/api/built-in-components
|
|
88
|
+
- **Type**: `boolean` | [`KeepAliveProps`](https://vuejs.org/api/built-in-components#keepalive)
|
|
82
89
|
|
|
83
|
-
Set to `true` when you want to preserve page state across route changes or use the [`KeepAliveProps`](https://vuejs.org/api/built-in-components
|
|
90
|
+
Set to `true` when you want to preserve page state across route changes or use the [`KeepAliveProps`](https://vuejs.org/api/built-in-components#keepalive) for a fine-grained control.
|
|
84
91
|
|
|
85
92
|
**`key`**
|
|
86
93
|
|
|
@@ -96,19 +103,19 @@ interface PageMeta {
|
|
|
96
103
|
|
|
97
104
|
**`layoutTransition`**
|
|
98
105
|
|
|
99
|
-
- **Type**: `boolean` | [`TransitionProps`](https://vuejs.org/api/built-in-components
|
|
106
|
+
- **Type**: `boolean` | [`TransitionProps`](https://vuejs.org/api/built-in-components#transition)
|
|
100
107
|
|
|
101
108
|
Set name of the transition to apply for current layout. You can also set this value to `false` to disable the layout transition.
|
|
102
109
|
|
|
103
110
|
**`middleware`**
|
|
104
111
|
|
|
105
|
-
- **Type**: `MiddlewareKey` | [`NavigationGuard`](https://router.vuejs.org/api/interfaces/
|
|
112
|
+
- **Type**: `MiddlewareKey` | [`NavigationGuard`](https://router.vuejs.org/api/interfaces/navigationguard) | `Array<MiddlewareKey | NavigationGuard>`
|
|
106
113
|
|
|
107
|
-
Define anonymous or named middleware directly within `definePageMeta`. Learn more about [route middleware](/docs/3.x/
|
|
114
|
+
Define anonymous or named middleware directly within `definePageMeta`. Learn more about [route middleware](/docs/3.x/directory-structure/middleware).
|
|
108
115
|
|
|
109
116
|
**`pageTransition`**
|
|
110
117
|
|
|
111
|
-
- **Type**: `boolean` | [`TransitionProps`](https://vuejs.org/api/built-in-components
|
|
118
|
+
- **Type**: `boolean` | [`TransitionProps`](https://vuejs.org/api/built-in-components#transition)
|
|
112
119
|
|
|
113
120
|
Set name of the transition to apply for current page. You can also set this value to `false` to disable the page transition.
|
|
114
121
|
|
|
@@ -122,7 +129,7 @@ interface PageMeta {
|
|
|
122
129
|
|
|
123
130
|
**`redirect`**
|
|
124
131
|
|
|
125
|
-
- **Type**: [`RouteRecordRedirectOption`](https://router.vuejs.org/guide/essentials/redirect-and-alias
|
|
132
|
+
- **Type**: [`RouteRecordRedirectOption`](https://router.vuejs.org/guide/essentials/redirect-and-alias)
|
|
126
133
|
|
|
127
134
|
Where to redirect if the route is directly matched. The redirection happens before any navigation guard and triggers a new navigation with the new target location.
|
|
128
135
|
|
|
@@ -130,19 +137,19 @@ interface PageMeta {
|
|
|
130
137
|
|
|
131
138
|
- **Type**: `(route: RouteLocationNormalized) => boolean | Promise<boolean> | Partial<NuxtError> | Promise<Partial<NuxtError>>`
|
|
132
139
|
|
|
133
|
-
Validate whether a given route can validly be rendered with this page. Return true if it is valid, or false if not. If another match can't be found, this will mean a 404. You can also directly return an object with `
|
|
140
|
+
Validate whether a given route can validly be rendered with this page. Return true if it is valid, or false if not. If another match can't be found, this will mean a 404. You can also directly return an object with `status`/`statusText` to respond immediately with an error (other matches will not be checked).
|
|
134
141
|
|
|
135
142
|
**`scrollToTop`**
|
|
136
143
|
|
|
137
144
|
- **Type**: `boolean | (to: RouteLocationNormalized, from: RouteLocationNormalized) => boolean`
|
|
138
145
|
|
|
139
|
-
Tell Nuxt to scroll to the top before rendering the page or not. If you want to overwrite the default scroll behavior of Nuxt, you can do so in `~/
|
|
146
|
+
Tell Nuxt to scroll to the top before rendering the page or not. If you want to overwrite the default scroll behavior of Nuxt, you can do so in `~/router.options.ts` (see [custom routing](/docs/3.x/guide/recipes/custom-routing#using-routeroptions)) for more info.
|
|
140
147
|
|
|
141
148
|
**`[key: string]`**
|
|
142
149
|
|
|
143
150
|
- **Type**: `any`
|
|
144
151
|
|
|
145
|
-
Apart from the above properties, you can also set **custom** metadata. You may wish to do so in a type-safe way by [augmenting the type of the `meta` object](/docs/3.x/
|
|
152
|
+
Apart from the above properties, you can also set **custom** metadata. You may wish to do so in a type-safe way by [augmenting the type of the `meta` object](/docs/3.x/directory-structure/pages/#typing-custom-metadata).
|
|
146
153
|
|
|
147
154
|
## Examples
|
|
148
155
|
|
|
@@ -215,11 +222,11 @@ definePageMeta({
|
|
|
215
222
|
</script>
|
|
216
223
|
```
|
|
217
224
|
|
|
218
|
-
For more examples see [Vue Router's Matching Syntax](https://router.vuejs.org/guide/essentials/route-matching-syntax
|
|
225
|
+
For more examples see [Vue Router's Matching Syntax](https://router.vuejs.org/guide/essentials/route-matching-syntax).
|
|
219
226
|
|
|
220
227
|
### Defining Layout
|
|
221
228
|
|
|
222
|
-
You can define the layout that matches the layout's file name located (by default) in the [`layouts/` directory](/docs/3.x/
|
|
229
|
+
You can define the layout that matches the layout's file name located (by default) in the [`layouts/` directory](/docs/3.x/directory-structure/layouts). You can also disable the layout by setting the `layout` to `false`:
|
|
223
230
|
|
|
224
231
|
```vue [pages/some-page.vue]
|
|
225
232
|
<script setup lang="ts">
|
|
@@ -8,7 +8,7 @@ links:
|
|
|
8
8
|
size: xs
|
|
9
9
|
---
|
|
10
10
|
|
|
11
|
-
::read-more{to="/docs/guide/going-further/experimental-features#inlinerouterules" icon="i-lucide-star"}
|
|
11
|
+
::read-more{to="/docs/3.x/guide/going-further/experimental-features#inlinerouterules" icon="i-lucide-star"}
|
|
12
12
|
This feature is experimental and in order to use it you must enable the `experimental.inlineRouteRules` option in your `nuxt.config`.
|
|
13
13
|
::
|
|
14
14
|
|
|
@@ -47,6 +47,6 @@ When running [`nuxt build`](/docs/3.x/api/commands/build), the home page will be
|
|
|
47
47
|
|
|
48
48
|
For more control, such as if you are using a custom `path` or `alias` set in the page's [`definePageMeta`](/docs/3.x/api/utils/define-page-meta), you should set `routeRules` directly within your `nuxt.config`.
|
|
49
49
|
|
|
50
|
-
::read-more{to="/docs/guide/concepts/rendering#hybrid-rendering" icon="i-lucide-medal"}
|
|
50
|
+
::read-more{to="/docs/3.x/guide/concepts/rendering#hybrid-rendering" icon="i-lucide-medal"}
|
|
51
51
|
Read more about the `routeRules`.
|
|
52
52
|
::
|