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

View File

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