mirror of
https://github.com/nuxt/nuxt.git
synced 2025-02-19 23:21:09 +00:00
fix: trailingSlash
and child routes not working (#8423)
This commit is contained in:
parent
5318172554
commit
b06df71753
@ -80,15 +80,11 @@ function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-',
|
|||||||
}
|
}
|
||||||
route.name = route.name.replace(regExpIndex, '')
|
route.name = route.name.replace(regExpIndex, '')
|
||||||
if (route.children) {
|
if (route.children) {
|
||||||
const indexRoutePath = trailingSlash === false ? '/' : ''
|
const defaultChildRoute = route.children.find(child => child.path === '/' || child.path === '')
|
||||||
const defaultChildRoute = route.children.find(child => child.path === indexRoutePath)
|
|
||||||
const routeName = route.name
|
const routeName = route.name
|
||||||
if (defaultChildRoute) {
|
if (defaultChildRoute) {
|
||||||
if (trailingSlash === false) {
|
|
||||||
defaultChildRoute.name = route.name
|
|
||||||
}
|
|
||||||
route.children.forEach((child) => {
|
route.children.forEach((child) => {
|
||||||
if (child.path !== indexRoutePath) {
|
if (child.path !== defaultChildRoute.path) {
|
||||||
const parts = child.path.split('/')
|
const parts = child.path.split('/')
|
||||||
parts[1] = parts[1].endsWith('?') ? parts[1].substr(0, parts[1].length - 1) : parts[1]
|
parts[1] = parts[1].endsWith('?') ? parts[1].substr(0, parts[1].length - 1) : parts[1]
|
||||||
child.path = parts.join('/')
|
child.path = parts.join('/')
|
||||||
|
@ -27,16 +27,9 @@ Array [
|
|||||||
"path": "/parent/child/test",
|
"path": "/parent/child/test",
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"children": Array [
|
|
||||||
Object {
|
|
||||||
"chunkName": "pages/another_route/_id",
|
|
||||||
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
|
||||||
"name": "another_route-id",
|
|
||||||
"path": "",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"chunkName": "pages/another_route/_id",
|
"chunkName": "pages/another_route/_id",
|
||||||
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
||||||
|
"name": "another_route-id",
|
||||||
"path": "/another_route/:id?",
|
"path": "/another_route/:id?",
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
@ -87,16 +80,9 @@ Array [
|
|||||||
"path": "/parent/child/test",
|
"path": "/parent/child/test",
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"children": Array [
|
|
||||||
Object {
|
|
||||||
"chunkName": "pages/another_route/_id",
|
|
||||||
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\another_route\\\\\\\\_id.vue",
|
|
||||||
"name": "another_route-id",
|
|
||||||
"path": "",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"chunkName": "pages/another_route/_id",
|
"chunkName": "pages/another_route/_id",
|
||||||
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\another_route\\\\\\\\_id.vue",
|
"component": "\\\\\\\\\\\\\\\\some\\\\\\\\nuxt\\\\\\\\app\\\\\\\\pages\\\\\\\\another_route\\\\\\\\_id.vue",
|
||||||
|
"name": "another_route-id",
|
||||||
"path": "/another_route/:id?",
|
"path": "/another_route/:id?",
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
@ -159,17 +145,6 @@ Array [
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"children": Array [
|
|
||||||
Object {
|
|
||||||
"chunkName": "pages/another_route/_id",
|
|
||||||
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
|
||||||
"name": "another_route-id",
|
|
||||||
"path": "",
|
|
||||||
"pathToRegexpOptions": Object {
|
|
||||||
"strict": true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"chunkName": "pages/another_route/_id",
|
"chunkName": "pages/another_route/_id",
|
||||||
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
||||||
"name": "another_route-id",
|
"name": "another_route-id",
|
||||||
@ -247,19 +222,9 @@ Array [
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
Object {
|
Object {
|
||||||
"children": Array [
|
|
||||||
Object {
|
|
||||||
"chunkName": "pages/another_route/_id",
|
|
||||||
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
|
||||||
"name": "another_route-id",
|
|
||||||
"path": "",
|
|
||||||
"pathToRegexpOptions": Object {
|
|
||||||
"strict": true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"chunkName": "pages/another_route/_id",
|
"chunkName": "pages/another_route/_id",
|
||||||
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
"component": "/some/nuxt/app/pages/another_route/_id.vue",
|
||||||
|
"name": "another_route-id",
|
||||||
"path": "/another_route/:id?",
|
"path": "/another_route/:id?",
|
||||||
"pathToRegexpOptions": Object {
|
"pathToRegexpOptions": Object {
|
||||||
"strict": true,
|
"strict": true,
|
||||||
|
@ -210,7 +210,6 @@ describe('util: route', () => {
|
|||||||
'pages/subpage/_param.vue',
|
'pages/subpage/_param.vue',
|
||||||
'pages/snake_case_route.vue',
|
'pages/snake_case_route.vue',
|
||||||
'pages/another_route/_id.vue',
|
'pages/another_route/_id.vue',
|
||||||
'pages/another_route/_id.vue',
|
|
||||||
'pages/parent/index.vue',
|
'pages/parent/index.vue',
|
||||||
'pages/parent/child/index.vue',
|
'pages/parent/child/index.vue',
|
||||||
'pages/parent/child/test.vue'
|
'pages/parent/child/test.vue'
|
||||||
|
92
test/dev/trailing-slash.test.js
Normal file
92
test/dev/trailing-slash.test.js
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import { loadFixture, getPort, Nuxt } from '../utils'
|
||||||
|
|
||||||
|
function runTest (name, expectations) {
|
||||||
|
describe(name, () => {
|
||||||
|
let port
|
||||||
|
let nuxt = null
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
const options = await loadFixture(name)
|
||||||
|
nuxt = new Nuxt(options)
|
||||||
|
await nuxt.ready()
|
||||||
|
port = await getPort()
|
||||||
|
await nuxt.server.listen(port, 'localhost')
|
||||||
|
})
|
||||||
|
|
||||||
|
for (const route in expectations) {
|
||||||
|
test(route, async () => {
|
||||||
|
const { html } = await nuxt.server.renderRoute(route)
|
||||||
|
for (const exp of expectations[route]) {
|
||||||
|
expect(html).toContain(exp)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await nuxt.close()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
runTest('trailing-slash/with-true', {
|
||||||
|
'/': [
|
||||||
|
'[pages/index]'
|
||||||
|
],
|
||||||
|
'/posts': [
|
||||||
|
'statusCode:404'
|
||||||
|
],
|
||||||
|
'/posts/': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/index]'
|
||||||
|
],
|
||||||
|
'/posts/foo': [
|
||||||
|
'statusCode:404'
|
||||||
|
],
|
||||||
|
'/posts/foo/': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/_slug]'
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
runTest('trailing-slash/with-false', {
|
||||||
|
'/': [
|
||||||
|
'[pages/index]'
|
||||||
|
],
|
||||||
|
'/posts': [
|
||||||
|
'[pages/posts]'
|
||||||
|
// '[pages/posts/index]' // <--seems wired
|
||||||
|
],
|
||||||
|
'/posts/': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/index]'
|
||||||
|
],
|
||||||
|
'/posts/foo': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/_slug]'
|
||||||
|
],
|
||||||
|
'/posts/foo/': [
|
||||||
|
'statusCode:404'
|
||||||
|
]
|
||||||
|
})
|
||||||
|
|
||||||
|
runTest('trailing-slash/with-default', {
|
||||||
|
'/': [
|
||||||
|
'[pages/index]'
|
||||||
|
],
|
||||||
|
'/posts': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/index]'
|
||||||
|
],
|
||||||
|
'/posts/': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/index]'
|
||||||
|
],
|
||||||
|
'/posts/foo': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/_slug]'
|
||||||
|
],
|
||||||
|
'/posts/foo/': [
|
||||||
|
'[pages/posts]',
|
||||||
|
'[pages/posts/_slug]'
|
||||||
|
]
|
||||||
|
})
|
5
test/fixtures/trailing-slash/pages/index.vue
vendored
Normal file
5
test/fixtures/trailing-slash/pages/index.vue
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
[pages/index]
|
||||||
|
</div>
|
||||||
|
</template>
|
6
test/fixtures/trailing-slash/pages/posts.vue
vendored
Normal file
6
test/fixtures/trailing-slash/pages/posts.vue
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
[pages/posts]
|
||||||
|
<nuxt-child />
|
||||||
|
</div>
|
||||||
|
</template>
|
5
test/fixtures/trailing-slash/pages/posts/_slug.vue
vendored
Normal file
5
test/fixtures/trailing-slash/pages/posts/_slug.vue
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
[pages/posts/_slug]
|
||||||
|
</div>
|
||||||
|
</template>
|
5
test/fixtures/trailing-slash/pages/posts/index.vue
vendored
Normal file
5
test/fixtures/trailing-slash/pages/posts/index.vue
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<template>
|
||||||
|
<div>
|
||||||
|
[pages/posts/index]
|
||||||
|
</div>
|
||||||
|
</template>
|
5
test/fixtures/trailing-slash/trailing-slash.test.js
vendored
Normal file
5
test/fixtures/trailing-slash/trailing-slash.test.js
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import { buildFixture } from '../../utils/build'
|
||||||
|
|
||||||
|
buildFixture('trailing-slash/with-default')
|
||||||
|
buildFixture('trailing-slash/with-true')
|
||||||
|
buildFixture('trailing-slash/with-false')
|
9
test/fixtures/trailing-slash/with-default/nuxt.config.js
vendored
Normal file
9
test/fixtures/trailing-slash/with-default/nuxt.config.js
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { resolve } from 'path'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
rootDir: __dirname,
|
||||||
|
srcDir: resolve(__dirname, '..'),
|
||||||
|
router: {
|
||||||
|
// trailingSlash: undefined
|
||||||
|
}
|
||||||
|
}
|
9
test/fixtures/trailing-slash/with-false/nuxt.config.js
vendored
Normal file
9
test/fixtures/trailing-slash/with-false/nuxt.config.js
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { resolve } from 'path'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
rootDir: __dirname,
|
||||||
|
srcDir: resolve(__dirname, '..'),
|
||||||
|
router: {
|
||||||
|
trailingSlash: false
|
||||||
|
}
|
||||||
|
}
|
9
test/fixtures/trailing-slash/with-true/nuxt.config.js
vendored
Normal file
9
test/fixtures/trailing-slash/with-true/nuxt.config.js
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { resolve } from 'path'
|
||||||
|
|
||||||
|
export default {
|
||||||
|
rootDir: __dirname,
|
||||||
|
srcDir: resolve(__dirname, '..'),
|
||||||
|
router: {
|
||||||
|
trailingSlash: true
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,12 @@
|
|||||||
import { loadFixture, Nuxt, Builder, BundleBuilder, listPaths, equalOrStartsWith } from './index'
|
import { loadFixture, Nuxt, Builder, BundleBuilder } from './index'
|
||||||
|
|
||||||
export const buildFixture = function (fixture, callback, hooks = []) {
|
export const buildFixture = function (fixture, callback, hooks = [], overrides) {
|
||||||
const pathsBefore = {}
|
|
||||||
let nuxt
|
let nuxt
|
||||||
|
|
||||||
test(`Build ${fixture}`, async () => {
|
test(`Build ${fixture}`, async () => {
|
||||||
const config = await loadFixture(fixture)
|
const config = await loadFixture(fixture, overrides)
|
||||||
nuxt = new Nuxt(config)
|
nuxt = new Nuxt(config)
|
||||||
|
|
||||||
pathsBefore.root = listPaths(nuxt.options.rootDir)
|
|
||||||
if (nuxt.options.rootDir !== nuxt.options.srcDir) {
|
|
||||||
pathsBefore.src = listPaths(nuxt.options.srcDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
const buildDone = jest.fn()
|
const buildDone = jest.fn()
|
||||||
hooks.forEach(([hook, fn]) => nuxt.hook(hook, fn))
|
hooks.forEach(([hook, fn]) => nuxt.hook(hook, fn))
|
||||||
nuxt.hook('build:done', buildDone)
|
nuxt.hook('build:done', buildDone)
|
||||||
@ -24,17 +18,4 @@ export const buildFixture = function (fixture, callback, hooks = []) {
|
|||||||
callback(builder)
|
callback(builder)
|
||||||
}
|
}
|
||||||
}, 120000)
|
}, 120000)
|
||||||
|
|
||||||
test('Check changed files', () => {
|
|
||||||
expect.hasAssertions()
|
|
||||||
|
|
||||||
// When building Nuxt we only expect files to changed
|
|
||||||
// within the nuxt.options.buildDir
|
|
||||||
Object.keys(pathsBefore).forEach((key) => {
|
|
||||||
const paths = listPaths(nuxt.options[`${key}Dir`], pathsBefore[key])
|
|
||||||
paths.forEach((item) => {
|
|
||||||
expect(equalOrStartsWith(nuxt.options.buildDir, item.path)).toBe(true)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user