diff --git a/examples/custom-routes/nuxt.config.js b/examples/custom-routes/nuxt.config.js index bb0765f744..3ca5309ed5 100644 --- a/examples/custom-routes/nuxt.config.js +++ b/examples/custom-routes/nuxt.config.js @@ -1,5 +1,14 @@ module.exports = { build: { vendor: ['axios'] + }, + generate: { + routeParams: { + '/users/:id': [{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}], + '/posts/:slug': [{slug: 'welcome'}, {slug: 'foo'}, {slug: 'bar'}], + '/posts/:slug/comments': [{slug: 'welcome'}, {slug: 'foo'}, {slug: 'bar'}], + '/posts/:slug/:name': [{slug: 'welcome', name: 'a'}, {slug: 'foo', name: 'b'}, {slug: 'bar', name: 'a'}], + '/projects/:slug': [{slug: 'toto'}, {slug: 'titi'}, {slug: 'tutu'}] + } } } diff --git a/lib/build/index.js b/lib/build/index.js index a1bf94b98f..8fd05707de 100644 --- a/lib/build/index.js +++ b/lib/build/index.js @@ -137,7 +137,7 @@ function * generateRoutesAndFiles () { */ const files = yield glob('pages/**/*.vue', { cwd: this.srcDir }) this.routes = _.uniq(_.map(files, (file) => { - return file.replace(/^pages/, '').replace(/\.vue$/, '').replace('/index', '').replace('_', ':').replace('', '/').replace(/\/{2,}/g, '/') + return file.replace(/^pages/, '').replace(/\.vue$/, '').replace(/\/index/g, '').replace(/_/g, ':').replace('', '/').replace(/\/{2,}/g, '/') })) /* ** Interpret and move template files to .nuxt/ diff --git a/lib/generate.js b/lib/generate.js index fddef50686..9632636ab2 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -69,15 +69,15 @@ module.exports = function () { */ let routes = [] this.routes.forEach((route) => { - if (route.path.includes(':') || route.path.includes('*')) { - const routeParams = this.options.generate.routeParams[route.path] + if (route.includes(':') || route.includes('*')) { + const routeParams = this.options.generate.routeParams[route] if (!routeParams) { - console.error(`Could not generate the dynamic route ${route.path}, please add the mapping params in nuxt.config.js (generate.routeParams).`) // eslint-disable-line no-console + console.error(`Could not generate the dynamic route ${route}, please add the mapping params in nuxt.config.js (generate.routeParams).`) // eslint-disable-line no-console return process.exit(1) } - const toPath = pathToRegexp.compile(route.path) + const toPath = pathToRegexp.compile(route) routes = routes.concat(routeParams.map((params) => { - return Object.assign({}, route, { path: toPath(params) }) + return toPath(params) })) } else { routes.push(route) @@ -87,8 +87,8 @@ module.exports = function () { while (routes.length) { yield routes.splice(0, 500).map((route) => { return co(function * () { - const { html } = yield self.renderRoute(route.path) - var path = join(route.path, sep, 'index.html') // /about -> /about/index.html + const { html } = yield self.renderRoute(route) + var path = join(route, sep, 'index.html') // /about -> /about/index.html debug('Generate file: ' + path) path = join(distPath, path) // Make sure the sub folders are created