Skip to content

Commit

Permalink
fix(build): better error if render fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hjvedvik committed Sep 26, 2018
1 parent de9793e commit 7158300
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions gridsome/lib/commands/utils/createRenderFn.js
@@ -1,3 +1,4 @@
const chalk = require('chalk')
const { createHTMLRenderer } = require('../../utils/html')
const { createBundleRenderer } = require('vue-server-renderer')

Expand All @@ -18,33 +19,36 @@ module.exports = function createRenderFn ({

return async function render (url, data = {}) {
const context = { url, pageQuery: { data }}
let app = ''

try {
const app = await renderer.renderToString(context)
const inject = context.meta ? context.meta.inject() : null

const head = '' +
(inject ? inject.title.text() : '') +
(inject ? inject.meta.text() : '') +
(inject ? inject.link.text() : '') +
(inject ? inject.style.text() : '') +
(inject ? inject.script.text() : '') +
(inject ? inject.noscript.text() : '') +
context.renderResourceHints() +
context.renderStyles()

const htmlAttrs = inject ? inject.htmlAttrs.text() : ''
const bodyAttrs = inject ? inject.bodyAttrs.text() : ''

return renderHTML({
htmlAttrs: inject ? `data-html-server-rendered="true" ${htmlAttrs}` : '',
scripts: context.renderScripts(),
bodyAttrs,
head,
app
})
app = await renderer.renderToString(context)
} catch (err) {
console.error(chalk.red(`Failed to render ${url}`))
throw err
}

const inject = context.meta.inject()

const head = '' +
inject.title.text() +
inject.meta.text() +
inject.link.text() +
inject.style.text() +
inject.script.text() +
inject.noscript.text() +
context.renderResourceHints() +
context.renderStyles()

const htmlAttrs = inject.htmlAttrs.text()
const bodyAttrs = inject.bodyAttrs.text()

return renderHTML({
htmlAttrs: inject ? `data-html-server-rendered="true" ${htmlAttrs}` : '',
scripts: context.renderScripts(),
bodyAttrs,
head,
app
})
}
}

0 comments on commit 7158300

Please sign in to comment.