mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-23 06:05:11 +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()
|
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)
|
||||||
|
@ -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(
|
||||||
|
Loading…
Reference in New Issue
Block a user