diff --git a/packages/generator/src/generator.js b/packages/generator/src/generator.js index a2de4f1483..6fe8bc61a0 100644 --- a/packages/generator/src/generator.js +++ b/packages/generator/src/generator.js @@ -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) diff --git a/packages/generator/test/generator.route.test.js b/packages/generator/test/generator.route.test.js index 67e59049d7..8f173867de 100644 --- a/packages/generator/test/generator.route.test.js +++ b/packages/generator/test/generator.route.test.js @@ -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(