Layout on afterEach

This commit is contained in:
Sébastien Chopin 2017-02-20 22:11:34 +00:00
parent 7d630bf5d9
commit 9a2767ac90
7 changed files with 33 additions and 21 deletions

View File

@ -1,6 +1,8 @@
Tasks for `0.9.10`:
- [ ] `build.publicPath` #25
- [x] `build.publicPath` #25
- [ ] Use [name].[chunkhash].js for generated js (production) #218
- [ ] Add expired headers (production)
- [ ] Activate layout only on afterEach #214
- [x] Activate layout only on afterEach #214
- [ ] Custom layout in layouts/error.vue #172
-> Not possible to have custom layout for a page, it should do the condition inside the layout itself (because of the middleware strategy)

View File

@ -33,18 +33,19 @@ export default {
if (!layout || !layouts['_' + layout]) layout = 'default'
this.layoutName = layout
let _layout = '_' + layout
if (typeof layouts[_layout] === 'function') {
return this.loadLayout(_layout)
}
this.layout = layouts[_layout]
return Promise.resolve(this.layout)
return this.layout
},
loadLayout (_layout) {
loadLayout (layout) {
if (!layout || !layouts['_' + layout]) layout = 'default'
let _layout = '_' + layout
if (typeof layouts[_layout] !== 'function') {
return Promise.resolve(layouts[_layout])
}
return layouts[_layout]()
.then((Component) => {
layouts[_layout] = Component
this.layout = layouts[_layout]
return this.layout
return layouts[_layout]
})
.catch((e) => {
if (this.$nuxt) {

View File

@ -2,7 +2,7 @@
import Vue from 'vue'
import middleware from './middleware'
import { app, router<%= (store ? ', store' : '') %> } from './index'
import { app, router<%= (store ? ', store' : '') %>, NuxtError } from './index'
import { getMatchedComponents, getMatchedComponentsInstances, flatMapComponents, getContext, promiseSeries, promisify, getLocation, compile } from './utils'
const noopData = () => { return {} }
const noopFetch = () => {}
@ -88,7 +88,7 @@ function render (to, from, next) {
this._hadError = !!this.$options._nuxt.err
if (!Components.length) {
// Default layout
this.setLayout()
this.loadLayout(NuxtError.layout)
.then(callMiddleware.bind(this, Components, context))
.then(() => {
this.error({ statusCode: 404, message: 'This page could not be found.' })
@ -117,7 +117,7 @@ function render (to, from, next) {
this.setTransitions(mapTransitions(Components, to, from))
let nextCalled = false
// Set layout
this.setLayout(Components[0].options.layout)
this.loadLayout(Components[0].options.layout)
.then(callMiddleware.bind(this, Components, context))
.then(() => {
// Pass validation?
@ -175,8 +175,11 @@ function render (to, from, next) {
.catch((error) => {
_lastPaths = []
error.statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500
this.error(error)
next(false)
this.loadLayout(NuxtError.layout)
.then(() => {
this.error(error)
next(false)
})
})
}
@ -213,6 +216,8 @@ function fixPrepatch (to, ___) {
if (this._hadError && this._dateLastError === this.$options._nuxt.dateErr) {
this.error()
}
// Set layout
this.setLayout(this.$options._nuxt.err ? NuxtError.layout : to.matched[0].components.default.options.layout)
// hot reloading
hotReloadAPI(this)
})
@ -234,8 +239,9 @@ function hotReloadAPI (_app) {
let promises = []
// If layout changed
if (_app.layoutName !== Component.options.layout) {
let promise = _app.setLayout(Component.options.layout)
let promise = _app.loadLayout(Component.options.layout)
promise.then(() => {
_app.setLayout(Component.options.layout)
hotReloadAPI(_app)
})
promises.push(promise)
@ -338,8 +344,10 @@ Promise.all(resolveComponents)
.then((Components) => {
const _app = new Vue(app)
return _app.setLayout(Components.length ? Components[0].options.layout : '')
let layout = Components.length ? Components[0].options.layout : NuxtError.layout
return _app.loadLayout(layout)
.then(() => {
_app.setLayout(layout)
return { _app, Components }
})
})

View File

@ -6,7 +6,7 @@
<script>
import Vue from 'vue'
import NuxtChild from './nuxt-child'
import NuxtError from '<%= components.ErrorPage %>'
import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./nuxt-error.vue" %>'
export default {
name: 'nuxt',

View File

@ -6,6 +6,7 @@ import router from './router.js'
<% if (store) { %>import store from './store.js'<% } %>
import NuxtChild from './components/nuxt-child.js'
import NuxtLink from './components/nuxt-link.js'
import NuxtError from '<%= components.ErrorPage ? components.ErrorPage : "./components/nuxt-error.vue" %>'
import Nuxt from './components/nuxt.vue'
import App from '<%= appPath %>'
@ -85,4 +86,4 @@ const app = {
...App
}
export { app, router<%= (store ? ', store' : '') %> }
export { app, router<%= (store ? ', store' : '') %>, NuxtError }

View File

@ -6,7 +6,7 @@ import Vue from 'vue'
import { stringify } from 'querystring'
import { omit } from 'lodash'
import middleware from './middleware'
import { app, router<%= (store ? ', store' : '') %> } from './index'
import { app, router<%= (store ? ', store' : '') %>, NuxtError } from './index'
import { getMatchedComponents, getContext, promiseSeries, promisify, urlJoin } from './utils'
const isDev = <%= isDev %>
@ -72,7 +72,7 @@ export default context => {
return Component
})
// Set layout
return _app.setLayout(Components.length ? Components[0].options.layout : '')
return _app.setLayout(Components.length ? Components[0].options.layout : NuxtError.layout)
})
.then((layout) => {
// Call middleware

View File

@ -206,7 +206,7 @@ function * generateRoutesAndFiles () {
loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading),
transition: this.options.transition,
components: {
ErrorPage: './nuxt-error.vue'
ErrorPage: null
}
}
// Format routes for the lib/app/router.js template