From d6f22c904d736ca0cf855ff1baba4ea16690f433 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Tue, 6 Mar 2018 14:03:09 +0800 Subject: [PATCH 1/3] fix: throw jsdomError to avoid renderAndGetWindow hanging --- lib/core/renderer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/core/renderer.js b/lib/core/renderer.js index aea3be7d60..996ef6ff6e 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -422,7 +422,8 @@ module.exports = class Renderer { } } if (opts.virtualConsole !== false) { - options.virtualConsole = new jsdom.VirtualConsole().sendTo(console) + options.virtualConsole = new jsdom.VirtualConsole().sendTo(console, { omitJSDOMErrors: true }) + options.virtualConsole.on('jsdomError', err => { throw err }) } url = url || 'http://localhost:3000' const { window } = await jsdom.JSDOM.fromURL(url, options) From d9d747c9a8d577374b7e2653ddbe08259c1acc97 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Tue, 6 Mar 2018 16:14:34 +0800 Subject: [PATCH 2/3] refator: only throw jsdomerror in window initialization --- lib/core/renderer.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/core/renderer.js b/lib/core/renderer.js index 996ef6ff6e..f35275f833 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -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__' : '
' From 0783be279012ebd55c316e88a690d666d3e227a4 Mon Sep 17 00:00:00 2001 From: Clark Du Date: Tue, 6 Mar 2018 16:29:21 +0800 Subject: [PATCH 3/3] refator: remove jsdomError listener before returning window --- lib/core/renderer.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/core/renderer.js b/lib/core/renderer.js index f35275f833..5826568243 100644 --- a/lib/core/renderer.js +++ b/lib/core/renderer.js @@ -423,16 +423,12 @@ module.exports = class Renderer { } const jsdomErrHandler = err => { throw err } if (opts.virtualConsole !== false) { - options.virtualConsole = new jsdom.VirtualConsole().sendTo(console, { omitJSDOMErrors: false }) + options.virtualConsole = new jsdom.VirtualConsole().sendTo(console) // 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__' : '
' @@ -447,6 +443,10 @@ module.exports = class Renderer { await new Promise(resolve => { window._onNuxtLoaded = () => resolve(window) }) + if (opts.virtualConsole !== false) { + // after window initialized successfully + options.virtualConsole.removeListener('jsdomError', jsdomErrHandler) + } // Send back window object return window }