[Feature: generate] Added support for rendering dynamic routes using data cached by generate.routes in the config file.

This commit is contained in:
Johan Roxendal 2017-05-20 07:09:05 +02:00
parent b2ef8c320f
commit 64e7cab8fa
3 changed files with 17 additions and 12 deletions

View File

@ -61,6 +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),
routePayload : context.routePayload,
error: context.error, error: context.error,
base: '<%= router.base %>', base: '<%= router.base %>',
env: <%= JSON.stringify(env) %> env: <%= JSON.stringify(env) %>

View File

@ -78,26 +78,30 @@ export default async function () {
process.exit(1) process.exit(1)
throw e // eslint-disable-line no-unreachable throw e // eslint-disable-line no-unreachable
} }
/* }
** Generate html files from routes function decorateWithPayloads (routes) {
*/ let routeMap = _(routes).map((route) => {
generateRoutes.forEach((route) => { return [route, {route, routePayload: {}}]
if (this.routes.indexOf(route) < 0) { }).fromPairs().value()
this.routes.push(route) generateRoutes.forEach(({route, routePayload}) => {
if (!routeMap[route]) {
routeMap[route] = {route, routePayload}
} }
}) })
return _.values(routeMap)
} }
/* /*
** Generate only index.html for router.mode = 'hash' ** Generate only index.html for router.mode = 'hash'
*/ */
let routes = (this.options.router.mode === 'hash') ? ['/'] : this.routes let routes = (this.options.router.mode === 'hash') ? [{route: '/'}] : decorateWithPayloads(this.routes)
while (routes.length) { while (routes.length) {
let n = 0 let n = 0
await Promise.all(routes.splice(0, 500).map(async (route) => { await Promise.all(routes.splice(0, 500).map(async ({route, routePayload}) => {
await waitFor(n++ * this.options.generate.interval) await waitFor(n++ * this.options.generate.interval)
let html let html
try { try {
const res = await this.renderRoute(route, { _generate: true }) const res = await this.renderRoute(route, { _generate: true, routePayload })
html = res.html html = res.html
if (res.error) { if (res.error) {
errors.push({ type: 'handled', route, error: res.error }) errors.push({ type: 'handled', route, error: res.error })

View File

@ -1,9 +1,9 @@
module.exports = { module.exports = {
generate: { generate: {
routes: [ routes: [
'/users/1', {route: '/users/1'},
'/users/2', {route: '/users/2'},
'/users/3' {route: '/users/3'}
], ],
interval: 200 interval: 200
} }