fix(generator): encode routes and show error summary (#7518)

This commit is contained in:
pooya parsa 2020-06-16 17:53:29 +02:00 committed by GitHub
parent ed34199146
commit 92ef65c8ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 18 deletions

View File

@ -23,7 +23,6 @@ export default class Generator {
this.distPath, this.distPath,
isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath isUrl(this.options.build.publicPath) ? '' : this.options.build.publicPath
) )
this.generatedRoutes = new Set()
// Shared payload // Shared payload
this._payload = null this._payload = null
@ -153,9 +152,16 @@ export default class Generator {
async generateRoutes (routes) { async generateRoutes (routes) {
const errors = [] const errors = []
this.routes = routes this.routes = []
// Add routes to the tracked generated routes (for crawler) this.generatedRoutes = new Set()
this.routes.forEach(({ route }) => this.generatedRoutes.add(route))
routes.forEach(({ route, ...props }) => {
route = decodeURI(route)
this.routes.push({ route, ...props })
// Add routes to the tracked generated routes (for crawler)
this.generatedRoutes.add(route)
})
// Start generate process // Start generate process
while (this.routes.length) { while (this.routes.length) {
let n = 0 let n = 0
@ -308,11 +314,11 @@ export default class Generator {
.split('#')[0] .split('#')[0]
.trim() .trim()
const href = sanitizedHref + possibleTrailingSlash const route = decodeURI(sanitizedHref + possibleTrailingSlash)
if (href.startsWith('/') && !path.extname(href) && this.shouldGenerateRoute(href) && !this.generatedRoutes.has(href)) { if (route.startsWith('/') && !path.extname(route) && this.shouldGenerateRoute(route) && !this.generatedRoutes.has(route)) {
this.generatedRoutes.add(href) // add the route to the tracked list this.generatedRoutes.add(route)
this.routes.push({ route: href }) this.routes.push({ route })
} }
}) })
} }
@ -377,10 +383,10 @@ export default class Generator {
await this.nuxt.callHook('export:routeCreated', { route, path: page.path, errors: pageErrors }) await this.nuxt.callHook('export:routeCreated', { route, path: page.path, errors: pageErrors })
if (pageErrors.length) { if (pageErrors.length) {
consola.error('Error generating ' + route) consola.error(`Error generating route "${route}": ${pageErrors.map(e => e.error.message).join(', ')}`)
errors.push(...pageErrors) errors.push(...pageErrors)
} else { } else {
consola.success('Generated ' + route) consola.success(`Generated route "${route}"`)
} }
return true return true

View File

@ -95,12 +95,9 @@ describe('generator: generate routes', () => {
const generator = new Generator(nuxt) const generator = new Generator(nuxt)
generator.generateRoute = jest.fn() generator.generateRoute = jest.fn()
jest.spyOn(routes, 'splice')
const errors = await generator.generateRoutes(routes) const errors = await generator.generateRoutes(routes)
expect(routes.splice).toBeCalledTimes(3)
expect(routes.splice).toBeCalledWith(0, 2)
expect(waitFor).toBeCalledTimes(5) expect(waitFor).toBeCalledTimes(5)
expect(waitFor).nthCalledWith(1, 0) expect(waitFor).nthCalledWith(1, 0)
expect(waitFor).nthCalledWith(2, 100) expect(waitFor).nthCalledWith(2, 100)
@ -119,8 +116,6 @@ describe('generator: generate routes', () => {
expect(generator._formatErrors).toBeCalledTimes(1) expect(generator._formatErrors).toBeCalledTimes(1)
expect(generator._formatErrors).toBeCalledWith(errors) expect(generator._formatErrors).toBeCalledWith(errors)
routes.splice.mockRestore()
}) })
test('should format errors', () => { test('should format errors', () => {

View File

@ -118,7 +118,7 @@ describe('generator: generate route', () => {
const returned = await generator.generateRoute({ route, payload, errors }) const returned = await generator.generateRoute({ route, payload, errors })
expect(consola.error).toBeCalledTimes(1) expect(consola.error).toBeCalledTimes(1)
expect(consola.error).toBeCalledWith('Error generating /foo') expect(consola.error).toBeCalledWith('Error generating route "/foo": render route failed')
expect(errors).toEqual([{ expect(errors).toEqual([{
error, error,
route, route,

View File

@ -311,7 +311,7 @@ export default {
route = (route.replace(/\/+$/, '') || '/').split('?')[0] route = (route.replace(/\/+$/, '') || '/').split('?')[0]
const src = urlJoin(base, staticAssetsBase, route, 'payload.js') const src = urlJoin(base, staticAssetsBase, route, 'payload.js')
try { try {
const payload = await window.__NUXT_IMPORT__(route, src) const payload = await window.__NUXT_IMPORT__(decodeURI(route), encodeURI(src))
this.setPagePayload(payload) this.setPagePayload(payload)
return payload return payload
} catch (err) { } catch (err) {

View File

@ -10,8 +10,11 @@
<NLink to="/pagination/1"> <NLink to="/pagination/1">
Pagination Pagination
</NLink> </NLink>
<NLink to="/dynamic/foo bar">
Dynamic route 1
</NLink>
<NLink to="/dynamic/foo%20bar"> <NLink to="/dynamic/foo%20bar">
Dynamic route Dynamic route 2
</NLink> </NLink>
</nav> </nav>
<br> <br>