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`: Tasks for `0.9.10`:
- [ ] `build.publicPath` #25 - [x] `build.publicPath` #25
- [ ] Use [name].[chunkhash].js for generated js (production) #218 - [ ] Use [name].[chunkhash].js for generated js (production) #218
- [ ] Add expired headers (production) - [ ] Add expired headers (production)
- [ ] Activate layout only on afterEach #214 - [x] Activate layout only on afterEach #214
- [ ] Custom layout in layouts/error.vue #172 - [ ] 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' if (!layout || !layouts['_' + layout]) layout = 'default'
this.layoutName = layout this.layoutName = layout
let _layout = '_' + layout let _layout = '_' + layout
if (typeof layouts[_layout] === 'function') {
return this.loadLayout(_layout)
}
this.layout = layouts[_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]() return layouts[_layout]()
.then((Component) => { .then((Component) => {
layouts[_layout] = Component layouts[_layout] = Component
this.layout = layouts[_layout] return layouts[_layout]
return this.layout
}) })
.catch((e) => { .catch((e) => {
if (this.$nuxt) { if (this.$nuxt) {

View File

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

View File

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

View File

@ -6,6 +6,7 @@ import router from './router.js'
<% if (store) { %>import store from './store.js'<% } %> <% if (store) { %>import store from './store.js'<% } %>
import NuxtChild from './components/nuxt-child.js' import NuxtChild from './components/nuxt-child.js'
import NuxtLink from './components/nuxt-link.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 Nuxt from './components/nuxt.vue'
import App from '<%= appPath %>' import App from '<%= appPath %>'
@ -85,4 +86,4 @@ const app = {
...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 { stringify } from 'querystring'
import { omit } from 'lodash' import { omit } from 'lodash'
import middleware from './middleware' 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' import { getMatchedComponents, getContext, promiseSeries, promisify, urlJoin } from './utils'
const isDev = <%= isDev %> const isDev = <%= isDev %>
@ -72,7 +72,7 @@ export default context => {
return Component return Component
}) })
// Set layout // Set layout
return _app.setLayout(Components.length ? Components[0].options.layout : '') return _app.setLayout(Components.length ? Components[0].options.layout : NuxtError.layout)
}) })
.then((layout) => { .then((layout) => {
// Call middleware // 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), loading: (typeof this.options.loading === 'string' ? r(this.srcDir, this.options.loading) : this.options.loading),
transition: this.options.transition, transition: this.options.transition,
components: { components: {
ErrorPage: './nuxt-error.vue' ErrorPage: null
} }
} }
// Format routes for the lib/app/router.js template // Format routes for the lib/app/router.js template