mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 15:42:09 +00:00
Add renderAndGetWindow
This commit is contained in:
parent
648033f077
commit
c5ac12b2cb
@ -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)
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user