Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add warning about redirect check #20680

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/2.guide/2.directory-structure/1.middleware.md
Expand Up @@ -30,6 +30,9 @@ export default defineNuxtRouteMiddleware((to, from) => {
if (to.params.id === '1') {
return abortNavigation()
}
// In a real app you would probably not redirect every route to `/`
// however it is important to check `to.path` before redirecting or you
// might get an infinite redirect loop
if (to.path !== '/') {
return navigateTo('/')
}
Expand Down
4 changes: 2 additions & 2 deletions docs/3.api/3.utils/abort-navigation.md
Expand Up @@ -36,7 +36,7 @@ export default defineNuxtRouteMiddleware((to, from) => {
return abortNavigation()
}

if (to.path !== '/edit-post') {
if (from.path !== '/edit-post') {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return navigateTo('/edit-post')
}
})
Expand All @@ -48,7 +48,7 @@ You can pass the error as a string:

```ts [middleware/auth.ts]
export default defineNuxtRouteMiddleware((to, from) => {
const auth = useState('auth')
const user = useState('user')

if (!user.value.isAuthorized) {
return abortNavigation('Insufficient permissions.')
Expand Down
2 changes: 1 addition & 1 deletion docs/3.api/3.utils/define-nuxt-route-middleware.md
Expand Up @@ -56,7 +56,7 @@ export default defineNuxtRouteMiddleware((to, from) => {
return navigateTo('/login')
}

if (to.path !== '/dashboard') {
if (from.path !== '/dashboard') {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return navigateTo('/dashboard')
}
})
Expand Down
2 changes: 1 addition & 1 deletion docs/3.api/3.utils/define-page-meta.md
Expand Up @@ -144,7 +144,7 @@ The example below shows how the middleware can be defined using a `function` dir
return navigateTo('/login')
}

if (to.path !== '/checkout') {
if (from.path !== '/checkout') {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
return navigateTo('/checkout')
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/3.api/3.utils/navigate-to.md
Expand Up @@ -96,7 +96,7 @@ await navigateTo({

```ts
export default defineNuxtRouteMiddleware((to, from) => {
if (to.path !== '/search') {
if (from.path !== '/search') {
danielroe marked this conversation as resolved.
Show resolved Hide resolved
// setting the redirect code to '301 Moved Permanently'
return navigateTo('/search', { redirectCode: 301 })
}
Expand Down