nuxt: Removing trailingSlash with nested route cause some problem
Version
Reproduction link
https://codesandbox.io/s/codesandbox-nuxt-9e9n1?fontsize=14
Steps to reproduce
- Setup a nested route in
pagesdirectory - Add
router: { trailingSlash: false}tonuxt.config.js
In the codesandbox, I created the following files
pages/
...
- users.vue
- users/
- index.vue
What is expected ?
In https://9e9n1.sse.codesandbox.io/users, the contents of pages/users.vue & pages/users/index.vue are shown.
In https://9e9n1.sse.codesandbox.io/users/ (with trailingSlash), This page could not be found is shown
What is actually happening?
In https://9e9n1.sse.codesandbox.io/users, the contents of pages/users/index.vue is not shown. Only pages/users.vue is shown.
But in https://9e9n1.sse.codesandbox.io/users/ (with trailingSlash), the contents of both are shown.
Also, the following error appeared in console.
[vue-router] Named Route 'users' has a default child route. When navigating to this named route (:to="{name: 'users'"), the default child route will not be rendered. Remove the name from this route and use the name of the default child route for named links instead.
[vue-router] Duplicate named routes definition: { name: "users", path: "/users" }
Additional comments?
The following is the summary of generated .nuxt/router.js
...
export const routerOptions = {
mode: 'history',
base: decodeURI('/'),
linkActiveClass: 'nuxt-link-active',
linkExactActiveClass: 'nuxt-link-exact-active',
scrollBehavior,
routes: [{
path: "/users",
component: ...,
pathToRegexpOptions: {"strict":true},
name: "users",
children: [{
path: "",
component: ...,
pathToRegexpOptions: {"strict":true},
name: "users"
}]
},
...
],
fallback: false
}
...
In my opinion, to remove trailing slash,
- The path of children which is default root of nested route, should be same as parent’s root.
- We should not set
nameto parent of nested route. It’s duplicated in children
So expected router.js may be
...
export const routerOptions = {
mode: 'history',
base: decodeURI('/'),
linkActiveClass: 'nuxt-link-active',
linkExactActiveClass: 'nuxt-link-exact-active',
scrollBehavior,
routes: [{
path: "/users",
component: ...,
pathToRegexpOptions: {"strict":true},
children: [{
path: "/users",
component: ...,
pathToRegexpOptions: {"strict":true},
name: "users"
}]
},
...
],
fallback: false
}
...
Currently, my workaround is configuring nuxt.config.js as following
...
router: {
trailingSlash: false,
extendRoutes (routes) {
routes.forEach(r => {
if (!r.children) return
delete r.name
r.children.forEach(c => {
if (c.path !== '') return
c.path = r.path
})
})
}
}
...
<div align="right">This bug report is available on Nuxt community (#c9930)</div>About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 3
- Comments: 15 (9 by maintainers)
@danielroe:
But then you are left with a trailing slash even though you’ve explicitly used
trailingSlash: falsein nuxt.config.Codesandbox example: https://lp5ku.sse.codesandbox.io/item/1 <- this is the URL we want to see in the browser address bar https://lp5ku.sse.codesandbox.io/item/1/ <- I don’t want to see that trailing slash in the browser URL
Again, this example works fine without a trailing slash in the URL: https://lp5ku.sse.codesandbox.io/item/1/details
Thanks for the report! I’ve implemented a naive bug fix in #6594 according to the error message. ☺️
Thanks for your contribution to Nuxt! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you would like this issue to remain open:
Issues that are labeled as
pendingwill not be automatically marked as stale.Thanks @nathanchase – unfortunately doesn’t work for me. This is such a nightmare to be honest!
With
trailingSlash: falseI am actually noticing that a dynamic child will return a name ofundefinedinstead of the correct name, e.g. dynamic child of$route.name: exhibitionsshould be$route.name: exhibitions-exhibitionbut withtrailingSlash: falsereturns$route.name: undefined…Removing the
trailingSlash: falsesetting fixes this issue but of course will cause me many more when duplicate pages are found by Google and the site is crawled.