@nuxt/docs-nightly 4.1.0-29259944.c0e5065b → 4.1.0-29262602.c2b288d6
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.
|
@@ -51,7 +51,7 @@ The [`<NuxtLink>`](/docs/api/components/nuxt-link) component links pages between
|
|
|
51
51
|
|
|
52
52
|
When a [`<NuxtLink>`](/docs/api/components/nuxt-link) enters the viewport on the client side, Nuxt will automatically prefetch components and payload (generated pages) of the linked pages ahead of time, resulting in faster navigation.
|
|
53
53
|
|
|
54
|
-
```vue [pages/
|
|
54
|
+
```vue [pages/index.vue]
|
|
55
55
|
<template>
|
|
56
56
|
<header>
|
|
57
57
|
<nav>
|
|
@@ -142,6 +142,44 @@ This is true even if you throw an error in your middleware on the server, and an
|
|
|
142
142
|
Rendering an error page is an entirely separate page load, meaning any registered middleware will run again. You can use [`useError`](/docs/getting-started/error-handling#useerror) in middleware to check if an error is being handled.
|
|
143
143
|
::
|
|
144
144
|
|
|
145
|
+
## Accessing Route in Middleware
|
|
146
|
+
|
|
147
|
+
Always use the `to` and `from` parameters in your middleware to access the next and previous routes. Avoid using the [`useRoute()`](/docs/api/composables/use-route) composable in this context altogether.
|
|
148
|
+
There is **no concept of a "current route" in middleware**, as middleware can abort a navigation or redirect to a different route. The `useRoute()` composable will always be inaccurate in this context.
|
|
149
|
+
|
|
150
|
+
::warning
|
|
151
|
+
Sometimes, you might call a composable that uses `useRoute()` internally, which can trigger this warning even if there is no direct call in your middleware.
|
|
152
|
+
This leads to the **same issue as above**, so you should structure your functions to accept the route as an argument instead when they are used in middleware.
|
|
153
|
+
::
|
|
154
|
+
|
|
155
|
+
::code-group
|
|
156
|
+
```ts twoslash [middleware/access-route.ts]
|
|
157
|
+
// @errors: 2304
|
|
158
|
+
export default defineNuxtRouteMiddleware(to => {
|
|
159
|
+
// passing the route to the function to avoid calling `useRoute()` in middleware
|
|
160
|
+
doSomethingWithRoute(to)
|
|
161
|
+
|
|
162
|
+
// ❌ this will output a warning and is NOT recommended
|
|
163
|
+
callsRouteInternally()
|
|
164
|
+
})
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
```ts twoslash [utils/handle-route.ts]
|
|
168
|
+
// providing the route as an argument so that it can be used in middleware correctly
|
|
169
|
+
export function doSomethingWithRoute(route = useRoute()) {
|
|
170
|
+
// ...
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
```ts twoslash [utils/dont-do-this.ts]
|
|
174
|
+
// ❌ this function is not suitable for use in middleware
|
|
175
|
+
export function callsRouteInternally() {
|
|
176
|
+
const route = useRoute()
|
|
177
|
+
// ...
|
|
178
|
+
}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
::
|
|
182
|
+
|
|
145
183
|
## Adding Middleware Dynamically
|
|
146
184
|
|
|
147
185
|
It is possible to add global or named route middleware manually using the [`addRouteMiddleware()`](/docs/api/utils/add-route-middleware) helper function, such as from within a plugin.
|
|
@@ -12,6 +12,12 @@ links:
|
|
|
12
12
|
Within the template of a Vue component, you can access the route using `$route`.
|
|
13
13
|
::
|
|
14
14
|
|
|
15
|
+
The `useRoute` composable is a wrapper around the identically named composable from `vue-router`, providing access to the current route in a Nuxt application.
|
|
16
|
+
|
|
17
|
+
The key difference is that in Nuxt, the composable ensures that the route is updated **only after** the page content has changed after navigation.
|
|
18
|
+
In contrast, the `vue-router` version updates the route **immediately**, which can lead to synchronization issues between different parts of the template
|
|
19
|
+
that rely on the route metadata, for example.
|
|
20
|
+
|
|
15
21
|
## Example
|
|
16
22
|
|
|
17
23
|
In the following example, we call an API via [`useFetch`](/docs/api/composables/use-fetch) using a dynamic page parameter - `slug` - as part of the URL.
|
|
@@ -45,8 +51,37 @@ Apart from dynamic parameters and query parameters, `useRoute()` also provides t
|
|
|
45
51
|
- `path`: encoded pathname section of the URL
|
|
46
52
|
- `redirectedFrom`: route location that was attempted to access before ending up on the current route location
|
|
47
53
|
|
|
48
|
-
|
|
49
|
-
|
|
54
|
+
## Common Pitfalls
|
|
55
|
+
|
|
56
|
+
### Route Synchronization Issues
|
|
57
|
+
|
|
58
|
+
It’s important to use the `useRoute()` composable from Nuxt rather than the one from `vue-router` to avoid synchronization issues during page navigation.
|
|
59
|
+
Importing `useRoute` directly from `vue-router` bypasses Nuxt's implementation.
|
|
60
|
+
|
|
61
|
+
```ts twoslash
|
|
62
|
+
// ❌ do not use `useRoute` from `vue-router`
|
|
63
|
+
// @errors: 2300
|
|
64
|
+
import { useRoute } from 'vue-router'
|
|
65
|
+
// ✅ use Nuxt's `useRoute` composable
|
|
66
|
+
import { useRoute } from '#app'
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Calling `useRoute` in Middleware
|
|
70
|
+
|
|
71
|
+
Using `useRoute` in middleware is not recommended because it can lead to unexpected behavior.
|
|
72
|
+
There is no concept of a "current route" in middleware.
|
|
73
|
+
The `useRoute()` composable should only be used in the setup function of a Vue component or in a Nuxt plugin.
|
|
74
|
+
|
|
75
|
+
::warning
|
|
76
|
+
This applies to any composable that uses `useRoute()` internally too.
|
|
50
77
|
::
|
|
51
78
|
|
|
79
|
+
::read-more{to="/docs/4.x/guide/directory-structure/app/middleware"}
|
|
80
|
+
Read more about accessing the route in the middleware section.
|
|
81
|
+
::
|
|
82
|
+
|
|
83
|
+
### Hydration Issues with `route.fullPath`
|
|
84
|
+
|
|
85
|
+
Browsers don't send [URL fragments](https://url.spec.whatwg.org/#concept-url-fragment) (for example `#foo`) when making requests. So using `route.fullPath` to affect the template can trigger hydration issues because this will include the fragment on client but not the server.
|
|
86
|
+
|
|
52
87
|
:read-more{icon="i-simple-icons-vuedotjs" to="https://router.vuejs.org/api/type-aliases/RouteLocationNormalizedLoaded.html"}
|