mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 14:15:13 +00:00
fix(generator): normalize trailing slashes before extendRoutes
(#8803)
This commit is contained in:
parent
99995745ed
commit
6d64ea580a
@ -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)
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user