Adds generate error report from renderer and minification. Changes the behavior where generate would terminate on error, now it instead gathers the error from crashing routes and compiles an error report.

This commit is contained in:
Johan Roxendal 2017-04-24 22:21:11 +02:00 committed by johan.roxendal@gu.se
parent ded7de0e72
commit d469f38d98
2 changed files with 36 additions and 5 deletions

View File

@ -41,6 +41,7 @@ const defaults = {
export default function () { export default function () {
const s = Date.now() const s = Date.now()
let errors = []
/* /*
** Set variables ** Set variables
*/ */
@ -97,14 +98,26 @@ export default function () {
yield routes.splice(0, 500).map((route) => { yield routes.splice(0, 500).map((route) => {
return co(function * () { return co(function * () {
yield waitFor(n++ * self.options.generate.interval) yield waitFor(n++ * self.options.generate.interval)
var { html } = yield self.renderRoute(route, { _generate: true }) var { html, error } = yield self.renderRoute(route, { _generate: true })
html = minify(html, self.options.generate.minify) // TODO: if error is truthy and html null, do continue
if(error) {
// console.log("error caught", error)
errors.push([route, error.errorObj])
return
}
try {
var minifiedHtml = minify(html, self.options.generate.minify)
} catch(err) {
let minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`)
errors.push([route, minifyErr])
return
}
var path = join(route, sep, 'index.html') // /about -> /about/index.html var path = join(route, sep, 'index.html') // /about -> /about/index.html
debug('Generate file: ' + path) debug('Generate file: ' + path)
path = join(distPath, path) path = join(distPath, path)
// Make sure the sub folders are created // Make sure the sub folders are created
yield mkdirp(dirname(path)) yield mkdirp(dirname(path))
yield writeFile(path, html, 'utf8') yield writeFile(path, minifiedHtml, 'utf8')
}) })
}) })
} }
@ -119,6 +132,12 @@ export default function () {
.then(() => { .then(() => {
const duration = Math.round((Date.now() - s) / 100) / 10 const duration = Math.round((Date.now() - s) / 100) / 10
debug(`HTML Files generated in ${duration}s`) debug(`HTML Files generated in ${duration}s`)
if(errors.length) {
console.error("==== Error report ==== \n" + errors.map( ([route, err]) => {
return `Route: '${route}'\n${err.stack}`
}).join("\n\n"))
}
return this return this
}) })
} }

View File

@ -96,7 +96,19 @@ export function renderRoute (url, context = {}) {
// Call renderToSting from the bundleRenderer and generate the HTML (will update the context as well) // Call renderToSting from the bundleRenderer and generate the HTML (will update the context as well)
const self = this const self = this
return co(function * () { return co(function * () {
let APP = yield self.renderToString(context) try {
var APP = yield self.renderToString(context)
} catch(err) {
return {
html : null,
error : {
statusCode : 500,
message : err.message,
errorObj : err
},
redirected: context.redirected
}
}
if (!context.nuxt.serverRendered) { if (!context.nuxt.serverRendered) {
APP = '<div id="__nuxt"></div>' APP = '<div id="__nuxt"></div>'
} }