mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-30 09:27:13 +00:00
fix(utils): flat routes if child routes have absolute paths (#7604)
This commit is contained in:
parent
d0056fbcc8
commit
4a0cf8f404
@ -13,14 +13,19 @@ export const flatRoutes = function flatRoutes (router, fileName = '', routes = [
|
|||||||
if (fileName === '' && r.path === '/') {
|
if (fileName === '' && r.path === '/') {
|
||||||
routes.push('/')
|
routes.push('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
return flatRoutes(r.children, fileName + r.path + '/', routes)
|
return flatRoutes(r.children, fileName + r.path + '/', routes)
|
||||||
}
|
}
|
||||||
fileName = fileName.replace(/\/+/g, '/')
|
fileName = fileName.replace(/\/+/g, '/')
|
||||||
routes.push(
|
|
||||||
(r.path === '' && fileName[fileName.length - 1] === '/'
|
// if child path is already absolute, do not make any concatenations
|
||||||
? fileName.slice(0, -1)
|
if (r.path && r.path.startsWith('/')) {
|
||||||
: fileName) + r.path
|
routes.push(r.path)
|
||||||
)
|
} else if (r.path === '' && fileName[fileName.length - 1] === '/') {
|
||||||
|
routes.push(fileName.slice(0, -1) + r.path)
|
||||||
|
} else {
|
||||||
|
routes.push(fileName + r.path)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
return routes
|
return routes
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,37 @@ describe('util: route', () => {
|
|||||||
expect(routes).toEqual(['/', '/foo/bar', '/foo/baz'])
|
expect(routes).toEqual(['/', '/foo/bar', '/foo/baz'])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('should flat absolute routes', () => {
|
||||||
|
const routes = flatRoutes([
|
||||||
|
{
|
||||||
|
name: 'foo',
|
||||||
|
path: '/foo',
|
||||||
|
children: [
|
||||||
|
{ name: 'foo-bar', path: '/foo/bar' },
|
||||||
|
{ name: 'foo-baz', path: '/foo/baz' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(routes).toEqual(['/foo/bar', '/foo/baz'])
|
||||||
|
})
|
||||||
|
|
||||||
|
test('should flat absolute routes with empty path', () => {
|
||||||
|
const routes = flatRoutes([
|
||||||
|
{
|
||||||
|
name: 'foo',
|
||||||
|
path: '/foo',
|
||||||
|
children: [
|
||||||
|
{ name: 'foo-root', path: '' },
|
||||||
|
{ name: 'foo-bar', path: '/foo/bar' },
|
||||||
|
{ name: 'foo-baz', path: '/foo/baz' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
])
|
||||||
|
|
||||||
|
expect(routes).toEqual(['/foo', '/foo/bar', '/foo/baz'])
|
||||||
|
})
|
||||||
|
|
||||||
describe('util: route guard', () => {
|
describe('util: route guard', () => {
|
||||||
test('should guard parent dir', () => {
|
test('should guard parent dir', () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
|
Loading…
Reference in New Issue
Block a user