diff --git a/lib/app/server.js b/lib/app/server.js index 22eaa43e65..c458729d5d 100644 --- a/lib/app/server.js +++ b/lib/app/server.js @@ -84,6 +84,10 @@ export default context => { }) }) if (!isValid) { + // Don't server-render the page in generate mode + if (context._generate) { + context.nuxt.serverRendered = false + } // Call the 404 error by making the Components array empty Components = [] return _app @@ -126,6 +130,9 @@ export default context => { return _app }) .catch(function (error) { + if (error && error instanceof Error) { + error = { statusCode: 500, message: error.message } + } context.nuxt.error = context.error(error) <%= (store ? 'context.nuxt.state = store.state' : '') %> return _app diff --git a/lib/generate.js b/lib/generate.js index 5e3703fdb3..bcebe0e5ab 100644 --- a/lib/generate.js +++ b/lib/generate.js @@ -88,7 +88,7 @@ module.exports = function () { while (routes.length) { yield routes.splice(0, 500).map((route) => { return co(function * () { - var { html } = yield self.renderRoute(route) + var { html } = yield self.renderRoute(route, { _generate: true }) html = minify(html, { collapseBooleanAttributes: true, collapseWhitespace: true, @@ -96,8 +96,8 @@ module.exports = function () { minifyCSS: true, minifyJS: true, processConditionalComments: true, - removeAttributeQuotes: true, - removeComments: true, + removeAttributeQuotes: false, + removeComments: false, removeEmptyAttributes: true, removeOptionalTags: true, removeRedundantAttributes: true, diff --git a/lib/render.js b/lib/render.js index c6af7d3429..cb1e56183b 100644 --- a/lib/render.js +++ b/lib/render.js @@ -36,6 +36,7 @@ exports.render = function (req, res) { const url = req.url req.url = req.url.replace(self._nuxtRegexp, '/') yield self.serveStaticNuxt(req, res) + /* istanbul ignore next */ req.url = url } }) @@ -70,10 +71,7 @@ exports.renderRoute = function (url, context = {}) { const self = this return co(function * () { let app = yield self.renderToString(context) - if (context.nuxt && context.nuxt.error instanceof Error) { - context.nuxt.error = { statusCode: 500, message: context.nuxt.error.message } - } - if (context.redirected) { + if (!context.nuxt.serverRendered) { app = '
' } const html = self.appTemplate({ @@ -121,6 +119,8 @@ exports.renderAndGetWindow = function renderAndGetWindow (url) { virtualConsole, done (err, window) { if (err) return reject(err) + // Mock window.scrollTo + window.scrollTo = function () {} // If Nuxt could not be loaded (error from the server-side) if (!window.__NUXT__) { return reject({ diff --git a/test/fixtures/with-config/pages/index.vue b/test/fixtures/with-config/pages/index.vue new file mode 100644 index 0000000000..a24e4f00e8 --- /dev/null +++ b/test/fixtures/with-config/pages/index.vue @@ -0,0 +1,6 @@ + diff --git a/test/with-config.test.js b/test/with-config.test.js new file mode 100644 index 0000000000..ad8a677b2d --- /dev/null +++ b/test/with-config.test.js @@ -0,0 +1,37 @@ +import test from 'ava' +import { resolve } from 'path' +const port = 4004 +const url = (route) => 'http://localhost:' + port + route + +let nuxt = null +let server = null + +// Init nuxt.js and create server listening on localhost:4000 +test.before('Init Nuxt.js', async t => { + const Nuxt = require('../') + const rootDir = resolve(__dirname, 'fixtures/with-config') + let config = require(resolve(rootDir, 'nuxt.config.js')) + config.rootDir = rootDir + config.dev = false + nuxt = new Nuxt(config) + await nuxt.build() + server = new nuxt.Server(nuxt) + server.listen(port, 'localhost') +}) + +test('/', async t => { + const { html } = await nuxt.renderRoute('/') + t.true(html.includes('

I have custom configurations

')) +}) + +test('/test/ (router base)', async t => { + const window = await nuxt.renderAndGetWindow(url('/test/')) + const html = window.document.body.innerHTML + t.true(html.includes('

I have custom configurations

')) +}) + +// Close server and ask nuxt to stop listening to file changes +test.after('Closing server and nuxt.js', t => { + server.close() + nuxt.close() +})