How do I add redirect routes with the experimental file router (also without polluting the types)? #2749
Unanswered
ZachHaber
asked this question in
Help and Questions
Replies: 2 comments 14 replies
|
For redirect-only records that should not affect typed routes, add them to the imported import { createRouter, createWebHistory } from 'vue-router'
import { routes } from 'vue-router/auto-routes'
routes.push({
path: '/:pathMatch(.*)*',
redirect: (to) => {
if (/^\/\d+\//.test(to.path)) return `/p${to.fullPath}`
return { name: 'Home', params: { env: getEnv() } }
},
})
export const router = createRouter({
history: createWebHistory(),
routes,
})That is different from |
2 replies
|
If you want to omit teh page from the generated types, set |
12 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I'm using the experimental router with the new param parsing functionality, but I've run into an issue. I'm trying to add in a 404 catch-all route to do some redirecting from both the root and very old urls/bookmarks that might exist without certain path params to add them back in. Based on that, I was using
/:patchMatch(.*)*as my path for the redirection. I also have a couple more specific redirects that I was trying to add in addition as well.The docs explicitly mention that you can add redirect routes through the
routesarray and that it can be useful to avoid having the redirects influence the compile-time type definitions.I was only able to figure out why that wasn't working when out of desperation, I swapped
router.jstorouter.tsand was confused that routes was showing a TS error. I looked into it and saw "This router does not haveaddRoute()andremoveRoute()..."When I tried to add a new page
[...path].vueas recommended by the docs for a catch-all/404 route using the same redirect as I was previously using, I ran into an issue where it isn't even running the redirect (I put a console.log in at the start to check) but it was telling me "Detected a possibly infinite redirection in a navigation guard when going from "/" to "/". Aborting to avoid a Stack Overflow." As it turns out, the redirect wasn't firing, so my global beforeEach navguard triggers instead, and it wasn't built for needing to handle the redirect. Not only that, the new route was affecting the type definitions as well which is something I would have preferred to have avoided to be able to actually benefit fully from the enhanced type definitions of the params parsing. For example, every single route has a root[env]param defined so I can just use an unknownroute.params.envand TS will know what it is, while if one of the redirect routes are added, then it will no longer appear to exist without narrowing out the various redirect routes.This is what I have in that
[...path].vuepage. The redirect seems fine but is never called.I'm unsure how to get even the redirect to function at all. I'm having similar issues with my old options api components not triggering their in-component nav guards.
All reactions