Merge branch 'fix-middleware' of github.com:Atinux/nuxt.js into fix-middleware

This commit is contained in:
Sébastien Chopin 2017-05-14 19:34:23 +02:00
commit 96db377919
8 changed files with 33 additions and 32 deletions

View File

@ -7,7 +7,7 @@
<a href="https://www.npmjs.com/package/nuxt"><img src="https://img.shields.io/npm/v/nuxt.svg" alt="Version"></a>
<a href="https://www.npmjs.com/package/nuxt"><img src="https://img.shields.io/npm/l/nuxt.svg" alt="License"></a>
<a href="https://gitter.im/nuxt/nuxt.js"><img src="https://img.shields.io/badge/GITTER-join%20chat-green.svg" alt="Gitter"></a>
<a href="https://donorbox.org/nuxt"><img src="https://img.shields.io/badge/Support%20us-donate-41B883.svg" alt="Support us"></a>
<a href="https://opencollective.com/nuxtjs"><img src="https://img.shields.io/badge/Support%20us-donate-41B883.svg" alt="Support us"></a>
</p>
@ -173,4 +173,4 @@ https://github.com/nuxt/nuxt.js/projects/1
Feel free to make a donation to support us.
<a href="https://donorbox.org/nuxt"><img src="https://img.shields.io/badge/Support%20us-donate-41B883.svg" alt="Support us"></a>
<a href="https://opencollective.com/nuxtjs"><img src="https://img.shields.io/badge/Support%20us-donate-41B883.svg" alt="Support us"></a>

View File

@ -1,4 +1,3 @@
export default function ({ store, route, redirect }) {
store.commit('ADD_VISIT', route.path)
if (route.fullPath === '/') return redirect('/foo')
}

View File

@ -12,7 +12,6 @@
<script>
export default {
asyncData ({ store, route, userAgent }) {
console.log('Call async data', route.fullPath)
return {
userAgent,
slugs: [

View File

@ -82,10 +82,13 @@ function callMiddleware (Components, context, layout) {
return middlewareSeries(midd, context)
}
function render (to, from, next) {
async function render (to, from, next) {
if (this._hashChanged) return next()
let layout
let nextCalled = false
const _next = function (path) {
<%= (loading ? 'this.$loading.finish && this.$loading.finish()' : '') %>
if (nextCalled) return
nextCalled = true
next(path)
}
@ -96,14 +99,13 @@ function render (to, from, next) {
this._hadError = !!this.$options._nuxt.err
if (!Components.length) {
// Default layout
callMiddleware.call(this, Components, context)
.then(() => this.loadLayout(typeof NuxtError.layout === 'function' ? NuxtError.layout(context) : NuxtError.layout))
.then(callMiddleware.bind(this, Components, context))
.then(() => {
this.error({ statusCode: 404, message: 'This page could not be found.' })
return next()
})
return
await callMiddleware.call(this, Components, context)
if (context._redirected) return
layout = await this.loadLayout(typeof NuxtError.layout === 'function' ? NuxtError.layout(context) : NuxtError.layout)
await callMiddleware.call(this, Components, context, layout)
if (context._redirected) return
this.error({ statusCode: 404, message: 'This page could not be found.' })
return next()
}
// Update ._data and other properties if hot reloaded
Components.forEach(function (Component) {
@ -113,18 +115,17 @@ function render (to, from, next) {
}
})
this.setTransitions(mapTransitions(Components, to, from))
let nextCalled = false
// Set layout
callMiddleware.call(this, Components, context)
.then(() => {
let layout = Components[0].options.layout
try {
// Set layout
await callMiddleware.call(this, Components, context)
if (context._redirected) return
layout = Components[0].options.layout
if (typeof layout === 'function') {
layout = layout(context)
}
return this.loadLayout(layout)
})
.then(callMiddleware.bind(this, Components, context))
.then(() => {
layout = await this.loadLayout(layout)
await callMiddleware.call(this, Components, context, layout)
if (context._redirected) return
// Pass validation?
let isValid = true
Components.forEach((Component) => {
@ -139,7 +140,7 @@ function render (to, from, next) {
this.error({ statusCode: 404, message: 'This page could not be found.' })
return next()
}
return Promise.all(Components.map((Component, i) => {
await Promise.all(Components.map((Component, i) => {
// Check if only children route changed
Component._path = compile(to.matched[i].path)(to.params)
if (!this._hadError && Component._path === _lastPaths[i] && (i + 1) !== Components.length) {
@ -163,16 +164,13 @@ function render (to, from, next) {
}
return Promise.all(promises)
}))
})
.then(() => {
_lastPaths = Components.map((Component, i) => compile(to.matched[i].path)(to.params))
<%= (loading ? 'this.$loading.finish && this.$loading.finish()' : '') %>
// If not redirected
if (!nextCalled) {
next()
}
})
.catch((error) => {
} catch (error) {
_lastPaths = []
error.statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500
let layout = NuxtError.layout
@ -184,7 +182,7 @@ function render (to, from, next) {
this.error(error)
next(false)
})
})
}
}
// Fix components format in matched, it's due to code-splitting of vue-router

View File

@ -104,13 +104,16 @@ export default function () {
errors.push({ type: 'handled', route, error })
}
} catch (err) {
/* istanbul ignore next */
errors.push({ type: 'unhandled', route, error: err })
return
}
try {
var minifiedHtml = minify(html, self.options.generate.minify)
} catch (err) {
/* istanbul ignore next */
let minifyErr = new Error(`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`)
/* istanbul ignore next */
errors.push({ type: 'unhandled', route, error: minifyErr })
return
}
@ -137,13 +140,14 @@ export default function () {
if (errors.length) {
const report = errors.map(({ type, route, error }) => {
/* istanbul ignore next */
if (type === 'unhandled') {
return `Route: '${route}'\n${error.stack}`
} else {
return `Route: '${route}' thrown an error: \n` + JSON.stringify(error)
}
})
console.error('==== Error report ==== \n' + report).join('\n\n') // eslint-disable-line no-console
console.error('==== Error report ==== \n' + report.join('\n\n')) // eslint-disable-line no-console
}
return this
})

View File

@ -18,7 +18,7 @@ export default function ({ isClient }) {
'css': styleLoader.call(this, 'css'),
'less': styleLoader.call(this, 'less', 'less-loader'),
'sass': styleLoader.call(this, 'sass', 'sass-loader?indentedSyntax'),
'scss': styleLoader.call(this, 'sass', 'scss-loader'),
'scss': styleLoader.call(this, 'sass', 'sass-loader'),
'stylus': styleLoader.call(this, 'stylus', 'stylus-loader'),
'styl': styleLoader.call(this, 'stylus', 'stylus-loader')
},

View File

@ -23,6 +23,7 @@ module.exports = {
string: 'Nuxt.js'
},
build: {
extractCSS: true,
publicPath: '/orion/',
analyze: {
analyzerMode: 'disabled',
@ -33,6 +34,6 @@ module.exports = {
}
},
css: [
{src: '~/assets/app.css'}
{ src: '~/assets/app.css' }
]
}

View File

@ -87,7 +87,7 @@ test('/test/about-bis (added with extendRoutes)', async t => {
test('Check stats.json generated by build.analyze', t => {
const stats = require(resolve(__dirname, 'fixtures/with-config/.nuxt/dist/stats.json'))
t.is(stats.assets.length, 27)
t.is(stats.assets.length, 29)
})
// Close server and ask nuxt to stop listening to file changes