mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
fix(utils): include routes with children for generation (#7761)
This commit is contained in:
parent
45f204c12c
commit
6af76334c5
@ -4,27 +4,38 @@ import consola from 'consola'
|
||||
|
||||
import { r } from './resolve'
|
||||
|
||||
const routeChildren = function (route) {
|
||||
const hasChildWithEmptyPath = route.children.some(child => child.path === '')
|
||||
if (hasChildWithEmptyPath) {
|
||||
return route.children
|
||||
}
|
||||
return [
|
||||
// Add default child to render parent page
|
||||
{
|
||||
...route,
|
||||
children: undefined
|
||||
},
|
||||
...route.children
|
||||
]
|
||||
}
|
||||
|
||||
export const flatRoutes = function flatRoutes (router, fileName = '', routes = []) {
|
||||
router.forEach((r) => {
|
||||
if ([':', '*'].some(c => r.path.includes(c))) {
|
||||
return
|
||||
}
|
||||
const route = `${fileName}${r.path}/`.replace(/\/+/g, '/')
|
||||
if (r.children) {
|
||||
if (fileName === '' && r.path === '/') {
|
||||
routes.push('/')
|
||||
return flatRoutes(routeChildren(r), route, routes)
|
||||
}
|
||||
|
||||
return flatRoutes(r.children, fileName + r.path + '/', routes)
|
||||
}
|
||||
fileName = fileName.replace(/\/+/g, '/')
|
||||
|
||||
// if child path is already absolute, do not make any concatenations
|
||||
if (r.path && r.path.startsWith('/')) {
|
||||
routes.push(r.path)
|
||||
} else if (r.path === '' && fileName[fileName.length - 1] === '/') {
|
||||
routes.push(fileName.slice(0, -1) + r.path)
|
||||
} else if (route !== '/' && route[route.length - 1] === '/') {
|
||||
routes.push(route.slice(0, -1))
|
||||
} else {
|
||||
routes.push(fileName + r.path)
|
||||
routes.push(route)
|
||||
}
|
||||
})
|
||||
return routes
|
||||
|
@ -14,7 +14,7 @@ describe('util: route', () => {
|
||||
]
|
||||
}
|
||||
])
|
||||
expect(routes).toEqual(['/login', '/about', '', '/post'])
|
||||
expect(routes).toEqual(['/login', '/about', '/', '/post'])
|
||||
})
|
||||
|
||||
test('should ignore route with * and :', () => {
|
||||
@ -52,7 +52,7 @@ describe('util: route', () => {
|
||||
}
|
||||
])
|
||||
|
||||
expect(routes).toEqual(['/foo/bar', '/foo/baz'])
|
||||
expect(routes).toEqual(['/foo', '/foo/bar', '/foo/baz'])
|
||||
})
|
||||
|
||||
test('should flat absolute routes with empty path', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user