Add renderAndGetWindow

This commit is contained in:
Sébastien Chopin 2016-12-07 19:02:06 +01:00
parent 648033f077
commit c5ac12b2cb
1 changed files with 34 additions and 0 deletions

View File

@ -4,6 +4,10 @@ const { urlJoin } = require('./utils')
const { getContext } = require('./utils')
exports.render = function (req, res) {
if (!this.renderer && !this.dev) {
console.error('> No build files found, please run `nuxt build` before launching `nuxt start`')
process.exit(1)
}
if (!this.renderer) {
setTimeout(() => {
this.render(req, res)
@ -87,3 +91,33 @@ exports.renderRoute = function (url, context = {}) {
}
})
}
// Function used to do dom checking via jsdom
exports.renderAndGetWindow = function renderAndGetWindow (jsdom, url) {
const virtualConsole = jsdom.createVirtualConsole().sendTo(console)
url = url || 'http://localhost:3000'
return new Promise((resolve, reject) => {
jsdom.env({
url: url,
features: {
FetchExternalResources: ['script', 'link'],
ProcessExternalResources: ['script']
},
virtualConsole,
done (err, window) {
if (err) return reject(err)
// If Nuxt could not be loaded (error from the server-side)
if (!window.__NUXT__) {
return reject({
message: 'Could not load the nuxt app',
body: window.document.getElementsByTagName('body')[0].innerHTML
})
}
// Used by nuxt.js to say when the components are loaded and the app ready
window.onNuxtReady(() => {
resolve(window)
})
}
})
})
}