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,
<%= (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) %>

View File

@ -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 {

View File

@ -74,9 +74,9 @@ test('/users/2', async t => {
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'))
t.true(html.includes('<h1>User: 3</h1>'))
t.true(html.includes('<h1>User: 3000</h1>'))
})
test('/users/4 -> Not found', async t => {

View File

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

View File

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