fix: payload is not fully working on nuxt generate

This commit is contained in:
Sebastien Chopin 2017-05-30 12:00:31 +02:00
parent d9d5d1a5d8
commit 4f1e82e959
5 changed files with 23 additions and 14 deletions

View File

@ -61,7 +61,7 @@ export function getContext (context, app) {
app: app, app: app,
<%= (store ? 'store: context.store,' : '') %> <%= (store ? 'store: context.store,' : '') %>
route: (context.to ? context.to : context.route), route: (context.to ? context.to : context.route),
payload : context.payload, payload: context.payload,
error: context.error, error: context.error,
base: '<%= router.base %>', base: '<%= router.base %>',
env: <%= JSON.stringify(env) %> env: <%= JSON.stringify(env) %>

View File

@ -80,14 +80,21 @@ export default async function () {
} }
} }
function decorateWithPayloads (routes) { function decorateWithPayloads (routes) {
let routeMap = _(routes).map((route) => { let routeMap = {}
return [route, {route, payload: {}}] // Fill routeMap for known routes
}).fromPairs().value() routes.forEach((route) => {
routeMap[route] = {
route,
payload: null
}
})
// Fill routeMap with given generate.routes
generateRoutes.forEach((route) => { generateRoutes.forEach((route) => {
// route argument is either a string or like {route : "/my_route/1"} // route is either a string or like {route : "/my_route/1"}
route = _.isString(route) ? route : route.route const path = _.isString(route) ? route : route.route
if (!routeMap[route]) { routeMap[path] = {
routeMap[route] = {route, payload: route.payload} route: path,
payload: route.payload || null
} }
}) })
return _.values(routeMap) return _.values(routeMap)
@ -95,7 +102,8 @@ export default async function () {
/* /*
** Generate only index.html for router.mode = 'hash' ** 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) { while (routes.length) {
let n = 0 let n = 0
@ -110,7 +118,7 @@ export default async function () {
} }
} catch (err) { } catch (err) {
/* istanbul ignore next */ /* istanbul ignore next */
errors.push({ type: 'unhandled', route, error: err }) return errors.push({ type: 'unhandled', route, error: err })
} }
if (this.options.generate.minify) { if (this.options.generate.minify) {
try { try {

View File

@ -74,9 +74,9 @@ test('/users/2', async t => {
t.true(html.includes('<h1>User: 2</h1>')) t.true(html.includes('<h1>User: 2</h1>'))
}) })
test('/users/3', async t => { test('/users/3 (payload given)', async t => {
const html = await rp(url('/users/3')) const html = await rp(url('/users/3'))
t.true(html.includes('<h1>User: 3</h1>')) t.true(html.includes('<h1>User: 3000</h1>'))
}) })
test('/users/4 -> Not found', async t => { test('/users/4 -> Not found', async t => {

View File

@ -3,7 +3,7 @@ module.exports = {
routes: [ routes: [
'/users/1', '/users/1',
'/users/2', '/users/2',
'/users/3' { route: '/users/3', payload: { id: 3000 } }
], ],
interval: 200 interval: 200
} }

View File

@ -4,7 +4,8 @@
<script> <script>
export default { export default {
asyncData ({ params }) { asyncData ({ params, payload }) {
if (payload) return payload
return { id: params.id } return { id: params.id }
} }
} }