refator: only throw jsdomerror in window initialization

This commit is contained in:
Clark Du 2018-03-06 16:14:34 +08:00
parent d6f22c904d
commit d9d747c9a8
No known key found for this signature in database
GPG Key ID: D0E5986AF78B86D9

View File

@ -421,12 +421,18 @@ module.exports = class Renderer {
window.scrollTo = () => {}
}
}
const jsdomErrHandler = err => { throw err }
if (opts.virtualConsole !== false) {
options.virtualConsole = new jsdom.VirtualConsole().sendTo(console, { omitJSDOMErrors: true })
options.virtualConsole.on('jsdomError', err => { throw err })
options.virtualConsole = new jsdom.VirtualConsole().sendTo(console, { omitJSDOMErrors: false })
// throw error when window creation failed
options.virtualConsole.on('jsdomError', jsdomErrHandler)
}
url = url || 'http://localhost:3000'
const { window } = await jsdom.JSDOM.fromURL(url, options)
if (opts.virtualConsole !== false) {
// after window initialized successfully
options.virtualConsole.removeListener('jsdomError', jsdomErrHandler)
}
// If Nuxt could not be loaded (error from the server-side)
const nuxtExists = window.document.body.innerHTML.includes(
this.options.render.ssr ? 'window.__NUXT__' : '<div id="__nuxt">'