From c5ac12b2cbec8cecacd031b04dea2694f22602e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Wed, 7 Dec 2016 19:02:06 +0100 Subject: [PATCH] Add renderAndGetWindow --- lib/render.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/render.js b/lib/render.js index d026a6d4fb..4e9cc52989 100644 --- a/lib/render.js +++ b/lib/render.js @@ -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) + }) + } + }) + }) +}