Handle validate for nuxt generate

This commit is contained in:
Sébastien Chopin 2016-12-21 15:03:23 +01:00
parent 20763b1cb6
commit 8102ce48eb
5 changed files with 57 additions and 7 deletions

View File

@ -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

View File

@ -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,

View File

@ -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 = '<div id="__nuxt"></div>'
}
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({

View File

@ -0,0 +1,6 @@
<template>
<div>
<h1>I have custom configurations</h1>
<nuxt-link to="/about">About page</nuxt-link>
</div>
</template>

37
test/with-config.test.js Normal file
View File

@ -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('<h1>I have custom configurations</h1>'))
})
test('/test/ (router base)', async t => {
const window = await nuxt.renderAndGetWindow(url('/test/'))
const html = window.document.body.innerHTML
t.true(html.includes('<h1>I have custom configurations</h1>'))
})
// Close server and ask nuxt to stop listening to file changes
test.after('Closing server and nuxt.js', t => {
server.close()
nuxt.close()
})