fix(generator): normalize trailing slashes before extendRoutes (#8803)

This commit is contained in:
Daniel Roe 2021-02-10 10:27:57 +00:00 committed by GitHub
parent 99995745ed
commit 6d64ea580a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View File

@ -169,7 +169,7 @@ export default class Generator {
this.generatedRoutes = new Set()
routes.forEach(({ route, ...props }) => {
route = decodeURI(route)
route = decodeURI(this.normalizeSlash(route))
this.routes.push({ route, ...props })
// Add routes to the tracked generated routes (for crawler)
this.generatedRoutes.add(route)
@ -274,18 +274,22 @@ export default class Generator {
await this.nuxt.callHook('export:distCopied', this)
}
normalizeSlash (route) {
return this.options.router && this.options.router.trailingSlash ? withTrailingSlash(route) : withoutTrailingSlash(route)
}
decorateWithPayloads (routes, generateRoutes) {
const routeMap = {}
// Fill routeMap for known routes
routes.forEach((route) => {
routeMap[route] = { route, payload: null }
routeMap[route] = { route: this.normalizeSlash(route), payload: null }
})
// Fill routeMap with given generate.routes
generateRoutes.forEach((route) => {
// route is either a string or like { route : '/my_route/1', payload: {} }
const path = isString(route) ? route : route.route
routeMap[path] = {
route: path,
route: this.normalizeSlash(path),
payload: route.payload || null
}
})
@ -296,9 +300,7 @@ export default class Generator {
let html
const pageErrors = []
if (this.options.router && this.options.router.trailingSlash) {
route = withTrailingSlash(route)
}
route = this.normalizeSlash(route)
const setPayload = (_payload) => {
payload = defu(_payload, payload)
@ -322,7 +324,6 @@ export default class Generator {
// If crawler activated and called from generateRoutes()
if (this.options.generate.crawler && this.options.render.ssr) {
const possibleTrailingSlash = this.options.router.trailingSlash ? '/' : ''
parse(html).querySelectorAll('a').map((el) => {
const sanitizedHref = (el.getAttribute('href') || '')
.replace(this.options.router.base, '/')
@ -331,7 +332,7 @@ export default class Generator {
.replace(/\/+$/, '')
.trim()
const foundRoute = decodeURI(sanitizedHref + possibleTrailingSlash)
const foundRoute = decodeURI(this.normalizeSlash(sanitizedHref))
if (foundRoute.startsWith('/') && !foundRoute.startsWith('//') && !path.extname(foundRoute) && this.shouldGenerateRoute(foundRoute) && !this.generatedRoutes.has(foundRoute)) {
this.generatedRoutes.add(foundRoute)

View File

@ -28,11 +28,11 @@ describe('generator: generate route', () => {
const returned = await generator.generateRoute({ route, payload, errors })
expect(nuxt.server.renderRoute).toBeCalledTimes(1)
expect(nuxt.server.renderRoute).toBeCalledWith(route, { payload })
expect(nuxt.server.renderRoute).toBeCalledWith('/foo', { payload })
const genernatePageHookCall = hookCalls(nuxt, 'generate:page')[0][0]
expect(genernatePageHookCall).toMatchObject({
route,
route: '/foo',
html: 'rendered html'
})
expect(genernatePageHookCall.path).toBePath(
@ -42,7 +42,7 @@ describe('generator: generate route', () => {
const genernateRouteCreatedHookCall = hookCalls(nuxt, 'generate:routeCreated')[0][0]
expect(genernateRouteCreatedHookCall).toMatchObject({
route,
route: '/foo',
errors: []
})
expect(genernateRouteCreatedHookCall.path).toBePath(