From 4f1e82e959101c11fdcaada11f33420245ffdea4 Mon Sep 17 00:00:00 2001 From: Sebastien Chopin Date: Tue, 30 May 2017 12:00:31 +0200 Subject: [PATCH] fix: payload is not fully working on nuxt generate --- lib/app/utils.js | 2 +- lib/generate.js | 26 ++++++++++++++++--------- test/basic.generate.test.js | 4 ++-- test/fixtures/basic/nuxt.config.js | 2 +- test/fixtures/basic/pages/users/_id.vue | 3 ++- 5 files changed, 23 insertions(+), 14 deletions(-) diff --git a/lib/app/utils.js b/lib/app/utils.js index 9b88a3a79f..048d3e9be0 100644 --- a/lib/app/utils.js +++ b/lib/app/utils.js @@ -61,7 +61,7 @@ export function getContext (context, app) { app: app, <%= (store ? 'store: context.store,' : '') %> route: (context.to ? context.to : context.route), - payload : context.payload, + payload: context.payload, error: context.error, base: '<%= router.base %>', env: <%= JSON.stringify(env) %> diff --git a/lib/generate.js b/lib/generate.js index cd780a2492..65de915b46 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -80,14 +80,21 @@ export default async function () { } } function decorateWithPayloads (routes) { - let routeMap = _(routes).map((route) => { - return [route, {route, payload: {}}] - }).fromPairs().value() + let routeMap = {} + // Fill routeMap for known routes + routes.forEach((route) => { + routeMap[route] = { + route, + payload: null + } + }) + // Fill routeMap with given generate.routes generateRoutes.forEach((route) => { - // route argument is either a string or like {route : "/my_route/1"} - route = _.isString(route) ? route : route.route - if (!routeMap[route]) { - routeMap[route] = {route, payload: route.payload} + // route is either a string or like {route : "/my_route/1"} + const path = _.isString(route) ? route : route.route + routeMap[path] = { + route: path, + payload: route.payload || null } }) return _.values(routeMap) @@ -95,7 +102,8 @@ export default async function () { /* ** Generate only index.html for router.mode = 'hash' */ - let routes = (this.options.router.mode === 'hash') ? [{route: '/'}] : decorateWithPayloads(this.routes) + let routes = (this.options.router.mode === 'hash') ? ['/'] : this.routes + routes = decorateWithPayloads(routes) while (routes.length) { let n = 0 @@ -110,7 +118,7 @@ export default async function () { } } catch (err) { /* istanbul ignore next */ - errors.push({ type: 'unhandled', route, error: err }) + return errors.push({ type: 'unhandled', route, error: err }) } if (this.options.generate.minify) { try { diff --git a/test/basic.generate.test.js b/test/basic.generate.test.js index 254dc1453e..02855fb73a 100644 --- a/test/basic.generate.test.js +++ b/test/basic.generate.test.js @@ -74,9 +74,9 @@ test('/users/2', async t => { t.true(html.includes('

User: 2

')) }) -test('/users/3', async t => { +test('/users/3 (payload given)', async t => { const html = await rp(url('/users/3')) - t.true(html.includes('

User: 3

')) + t.true(html.includes('

User: 3000

')) }) test('/users/4 -> Not found', async t => { diff --git a/test/fixtures/basic/nuxt.config.js b/test/fixtures/basic/nuxt.config.js index d6ba223b68..fb6e6734c7 100644 --- a/test/fixtures/basic/nuxt.config.js +++ b/test/fixtures/basic/nuxt.config.js @@ -3,7 +3,7 @@ module.exports = { routes: [ '/users/1', '/users/2', - '/users/3' + { route: '/users/3', payload: { id: 3000 } } ], interval: 200 } diff --git a/test/fixtures/basic/pages/users/_id.vue b/test/fixtures/basic/pages/users/_id.vue index 8cd8e0f0d1..ed71a446ec 100644 --- a/test/fixtures/basic/pages/users/_id.vue +++ b/test/fixtures/basic/pages/users/_id.vue @@ -4,7 +4,8 @@