Nuxt/lib/app/client.js

581 lines
17 KiB
JavaScript
Raw Normal View History

2016-11-07 01:34:58 +00:00
import Vue from 'vue'
2017-02-03 14:09:27 +00:00
import middleware from './middleware'
2017-05-02 06:57:39 +00:00
import { createApp, NuxtError } from './index'
2017-07-27 14:26:36 +00:00
import {
2017-07-09 21:41:04 +00:00
applyAsyncData,
sanitizeComponent,
2017-07-27 14:26:36 +00:00
getMatchedComponents,
2017-07-09 21:41:04 +00:00
getMatchedComponentsInstances,
2017-07-27 14:26:36 +00:00
flatMapComponents,
getContext,
2017-07-09 21:41:04 +00:00
middlewareSeries,
promisify,
getLocation,
compile
} from './utils'
2017-08-05 21:33:46 +00:00
2016-11-07 01:34:58 +00:00
const noopData = () => { return {} }
const noopFetch = () => {}
2017-07-09 21:41:04 +00:00
// Global shared references
let _lastPaths = []
2016-12-20 12:44:00 +00:00
let _lastComponentsFiles = []
let app
let router
2017-07-09 21:41:04 +00:00
<% if (store) { %>let store<% } %>
// Try to rehydrate SSR data from window
const NUXT = window.__NUXT__ || {}
2017-08-20 13:13:42 +00:00
<% if (debug || mode === 'spa') { %>
// Setup global Vue error handler
const defaultErrorHandler = Vue.config.errorHandler
Vue.config.errorHandler = function (err, vm, info) {
2017-08-20 13:13:42 +00:00
err.statusCode = err.statusCode || err.name || 'Whoops!'
err.message = err.message || err.toString()
// Show Nuxt Error Page
2017-08-21 11:54:41 +00:00
if(vm && vm.$root && vm.$root.$nuxt && vm.$root.$nuxt.error && info !== 'render function') {
vm.$root.$nuxt.error(err)
}
// Call other handler if exist
if (typeof defaultErrorHandler === 'function') {
return defaultErrorHandler(...arguments)
}
2017-08-20 13:13:42 +00:00
// Log to console
if (process.env.NODE_ENV !== 'production') {
2017-08-20 13:13:42 +00:00
console.error(err)
} else {
console.error(err.message)
}
}
<% } %>
2017-07-09 21:41:04 +00:00
// Create and mount App
createApp()
.then(mountApp)
.catch(err => {
console.error('[nuxt] Error while initializing app', err)
})
function componentOption(component, key, ...args) {
if (!component || !component.options || !component.options[key]) {
return {}
}
const option = component.options[key]
if (typeof option === 'function') {
return option(...args)
}
return option
}
2017-05-02 06:57:39 +00:00
function mapTransitions(Components, to, from) {
2017-07-09 21:41:04 +00:00
const componentTransitions = component => {
const transition = componentOption(component, 'transition', to, from) || {}
return (typeof transition === 'string' ? { name: transition } : transition)
}
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
return Components.map(Component => {
// Clone original object to prevent overrides
const transitions = Object.assign({}, componentTransitions(Component))
2017-07-27 14:26:36 +00:00
// Combine transitions & prefer `leave` transitions of 'from' route
if (from && from.matched.length && from.matched[0].components.default) {
const from_transitions = componentTransitions(from.matched[0].components.default)
Object.keys(from_transitions)
.filter(key => from_transitions[key] && key.toLowerCase().indexOf('leave') !== -1)
.forEach(key => { transitions[key] = from_transitions[key] })
}
2017-07-27 14:26:36 +00:00
return transitions
})
}
2016-11-07 01:34:58 +00:00
2017-07-09 21:41:04 +00:00
async function loadAsyncComponents (to, from, next) {
// Check if route hash changed
2017-02-06 12:24:59 +00:00
const fromPath = from.fullPath.split('#')[0]
const toPath = to.fullPath.split('#')[0]
2017-07-09 21:41:04 +00:00
this._hashChanged = fromPath === toPath
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
<% if (loading) { %>
if (!this._hashChanged && this.$loading.start) {
this.$loading.start()
2017-01-29 06:49:36 +00:00
}
2017-07-09 21:41:04 +00:00
<% } %>
try {
await Promise.all(flatMapComponents(to, (Component, _, match, key) => {
// If component already resolved
if (typeof Component !== 'function' || Component.options) {
const _Component = sanitizeComponent(Component)
match.components[key] = _Component
return _Component
}
// Resolve component
return Component().then(Component => {
const _Component = sanitizeComponent(Component)
match.components[key] = _Component
return _Component
})
}))
next()
} catch (err) {
if (!err) err = {}
const statusCode = err.statusCode || err.status || (err.response && err.response.status) || 500
this.error({ statusCode, message: err.message })
next(false)
}
}
function applySSRData(Component) {
if (NUXT.serverRendered) {
applyAsyncData(Component, NUXT.data[index])
Component._Ctor = Component
}
return Component
}
2017-07-09 21:41:04 +00:00
// Get matched components
function resolveComponents(router) {
const path = getLocation(router.options.base, router.options.mode)
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
return flatMapComponents(router.match(path), (Component, _, match, key, index) => {
2017-07-27 14:26:36 +00:00
// If component already resolved
2017-07-09 21:41:04 +00:00
if (typeof Component !== 'function' || Component.options) {
const _Component = applySSRData(sanitizeComponent(Component))
2017-07-09 21:41:04 +00:00
match.components[key] = _Component
return _Component
}
// Resolve component
return Component().then(Component => {
const _Component = applySSRData(sanitizeComponent(Component))
match.components[key] = _Component
return _Component
2017-07-09 21:41:04 +00:00
})
2016-11-07 01:34:58 +00:00
})
}
2017-02-03 14:09:27 +00:00
function callMiddleware (Components, context, layout) {
let midd = <%= serialize(router.middleware, { isJSON: true }) %>
2017-03-17 17:02:58 +00:00
let unknownMiddleware = false
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
// If layout is undefined, only call global middleware
2017-03-17 17:02:58 +00:00
if (typeof layout !== 'undefined') {
2017-07-09 21:41:04 +00:00
midd = [] // Exclude global middleware if layout defined (already called before)
2017-03-17 17:02:58 +00:00
if (layout.middleware) {
midd = midd.concat(layout.middleware)
2017-02-03 14:09:27 +00:00
}
2017-07-09 21:41:04 +00:00
Components.forEach(Component => {
2017-03-17 17:02:58 +00:00
if (Component.options.middleware) {
midd = midd.concat(Component.options.middleware)
}
})
}
2017-07-09 21:41:04 +00:00
midd = midd.map(name => {
2017-02-03 14:09:27 +00:00
if (typeof middleware[name] !== 'function') {
2017-03-17 17:02:58 +00:00
unknownMiddleware = true
2017-02-03 14:09:27 +00:00
this.error({ statusCode: 500, message: 'Unknown middleware ' + name })
}
return middleware[name]
})
2017-07-27 14:26:36 +00:00
if (unknownMiddleware) return
2017-05-04 07:57:10 +00:00
return middlewareSeries(midd, context)
2017-02-03 14:09:27 +00:00
}
2017-05-09 12:43:47 +00:00
async function render (to, from, next) {
2017-01-29 06:49:36 +00:00
if (this._hashChanged) return next()
2017-07-09 21:41:04 +00:00
// nextCalled is true when redirected
2017-05-09 12:43:47 +00:00
let nextCalled = false
2017-07-09 21:41:04 +00:00
const _next = path => {
<% if(loading) { %>if(this.$loading.finish) this.$loading.finish()<% } %>
2017-05-09 12:43:47 +00:00
if (nextCalled) return
2017-02-03 14:09:27 +00:00
nextCalled = true
next(path)
}
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
// Update context
2017-07-27 14:26:36 +00:00
const context = getContext({
2017-07-09 21:41:04 +00:00
to,
2017-07-20 19:04:23 +00:00
from,
2017-07-09 21:41:04 +00:00
<% if (store) { %>store,<% } %>
isClient: true,
next: _next.bind(this),
2017-07-28 08:21:58 +00:00
error: this.error.bind(this)
}, app)
2017-03-17 17:02:58 +00:00
this._context = context
2017-01-27 22:10:02 +00:00
this._dateLastError = this.$options._nuxt.dateErr
this._hadError = !!this.$options._nuxt.err
2017-07-09 21:41:04 +00:00
// Get route's matched components
const Components = getMatchedComponents(to)
// If no Components matched, generate 404
2016-11-07 01:34:58 +00:00
if (!Components.length) {
2016-12-24 11:34:41 +00:00
// Default layout
2017-05-09 12:43:47 +00:00
await callMiddleware.call(this, Components, context)
if (context._redirected) return
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
// Load layout for error page
2017-07-27 14:26:36 +00:00
const layout = await this.loadLayout(typeof NuxtError.layout === 'function' ? NuxtError.layout(context) : NuxtError.layout)
2017-05-09 12:43:47 +00:00
await callMiddleware.call(this, Components, context, layout)
if (context._redirected) return
2017-07-27 14:26:36 +00:00
2017-09-01 16:30:49 +00:00
this.error({ statusCode: 404, message: '<%= messages.error_404 %>' })
2017-05-09 12:43:47 +00:00
return next()
2016-11-07 01:34:58 +00:00
}
2017-07-27 14:26:36 +00:00
2016-11-07 01:34:58 +00:00
// Update ._data and other properties if hot reloaded
2017-07-09 21:41:04 +00:00
Components.forEach(Component => {
2016-12-24 00:55:32 +00:00
if (Component._Ctor && Component._Ctor.options) {
2017-02-28 12:10:58 +00:00
Component.options.asyncData = Component._Ctor.options.asyncData
Component.options.fetch = Component._Ctor.options.fetch
2016-11-07 01:34:58 +00:00
}
})
2017-07-09 21:41:04 +00:00
// Apply transitions
this.setTransitions(mapTransitions(Components, to, from))
2017-07-09 21:41:04 +00:00
2017-05-09 12:43:47 +00:00
try {
2017-07-09 21:41:04 +00:00
// Call middleware
2017-05-09 12:43:47 +00:00
await callMiddleware.call(this, Components, context)
if (context._redirected) return
2017-07-09 21:41:04 +00:00
// Set layout
let layout = Components[0].options.layout
2017-03-17 17:02:58 +00:00
if (typeof layout === 'function') {
layout = layout(context)
}
2017-05-09 12:43:47 +00:00
layout = await this.loadLayout(layout)
2017-07-09 21:41:04 +00:00
// Call middleware for layout
2017-05-09 12:43:47 +00:00
await callMiddleware.call(this, Components, context, layout)
if (context._redirected) return
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
// Call .validate()
2016-12-24 11:34:41 +00:00
let isValid = true
2017-07-09 21:41:04 +00:00
Components.forEach(Component => {
2016-12-24 11:34:41 +00:00
if (!isValid) return
if (typeof Component.options.validate !== 'function') return
isValid = Component.options.validate({
2017-04-05 16:25:12 +00:00
params: to.params || {},
2017-07-09 21:41:04 +00:00
query : to.query || {},
<% if(store) { %>store: context.store <% } %>
2016-11-07 01:34:58 +00:00
})
2016-12-24 11:34:41 +00:00
})
2017-07-09 21:41:04 +00:00
// ...If .validate() returned false
2016-12-24 11:34:41 +00:00
if (!isValid) {
2017-09-01 16:30:49 +00:00
this.error({ statusCode: 404, message: '<%= messages.error_404 %>' })
2016-12-24 11:34:41 +00:00
return next()
2016-11-07 01:34:58 +00:00
}
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
// Call asyncData & fetch hooks on components matched by the route.
2017-05-09 12:43:47 +00:00
await Promise.all(Components.map((Component, i) => {
2016-12-24 11:34:41 +00:00
// Check if only children route changed
Component._path = compile(to.matched[i].path)(to.params)
2017-08-21 11:54:41 +00:00
if (!this._hadError && this._isMounted && Component._path === _lastPaths[i] && (i + 1) !== Components.length) {
2016-12-24 11:34:41 +00:00
return Promise.resolve()
}
2017-07-09 21:41:04 +00:00
2016-12-24 11:34:41 +00:00
let promises = []
2017-07-09 21:41:04 +00:00
const hasAsyncData = Component.options.asyncData && typeof Component.options.asyncData === 'function'
const hasFetch = !!Component.options.fetch
2017-07-27 14:26:36 +00:00
<% if(loading) { %>const loadingIncrease = (hasAsyncData && hasFetch) ? 30 : 45<% } %>
2017-07-09 21:41:04 +00:00
// Call asyncData(context)
if (hasAsyncData) {
const promise = promisify(Component.options.asyncData, context)
2017-07-09 21:41:04 +00:00
.then(asyncDataResult => {
2017-04-14 14:31:14 +00:00
applyAsyncData(Component, asyncDataResult)
2017-07-09 21:41:04 +00:00
<% if(loading) { %>if(this.$loading.increase) this.$loading.increase(loadingIncrease)<% } %>
2016-12-24 11:34:41 +00:00
})
promises.push(promise)
}
2017-07-09 21:41:04 +00:00
// Call fetch(context)
if (hasFetch) {
let p = Component.options.fetch(context)
2017-07-09 23:57:50 +00:00
if (!p || (!(p instanceof Promise) && (typeof p.then !== 'function'))) {
2017-07-27 14:26:36 +00:00
p = Promise.resolve(p)
2017-07-09 23:57:50 +00:00
}
p.then(fetchResult => {
2017-07-09 21:41:04 +00:00
<% if(loading) { %>if(this.$loading.increase) this.$loading.increase(loadingIncrease)<% } %>
})
2016-12-24 11:34:41 +00:00
promises.push(p)
}
2017-07-09 21:41:04 +00:00
2016-12-24 11:34:41 +00:00
return Promise.all(promises)
}))
2017-07-27 14:26:36 +00:00
_lastPaths = Components.map((Component, i) => compile(to.matched[i].path)(to.params))
2017-07-09 21:41:04 +00:00
<% if(loading) { %>if(this.$loading.finish) this.$loading.finish()<% } %>
2016-11-10 23:01:36 +00:00
// If not redirected
2017-07-09 21:41:04 +00:00
if (!nextCalled) next()
2017-05-09 12:43:47 +00:00
} catch (error) {
2017-07-09 21:41:04 +00:00
if (!error) error = {}
_lastPaths = []
error.statusCode = error.statusCode || error.status || (error.response && error.response.status) || 500
2017-07-09 21:41:04 +00:00
// Load error layout
2017-03-17 17:02:58 +00:00
let layout = NuxtError.layout
if (typeof layout === 'function') {
layout = layout(context)
}
2017-07-09 21:41:04 +00:00
await this.loadLayout(layout)
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
this.error(error)
next(false)
2017-05-09 12:43:47 +00:00
}
2016-11-07 01:34:58 +00:00
}
2017-01-20 17:32:43 +00:00
// Fix components format in matched, it's due to code-splitting of vue-router
function normalizeComponents (to, ___) {
flatMapComponents(to, (Component, _, match, key) => {
if (typeof Component === 'object' && !Component.options) {
// Updated via vue-router resolveAsyncComponents()
Component = Vue.extend(Component)
Component._Ctor = Component
match.components[key] = Component
}
return Component
})
}
2016-11-22 23:27:07 +00:00
// When navigating on a different route but the same component is used, Vue.js
2017-07-09 21:41:04 +00:00
// Will not update the instance data, so we have to update $data ourselves
2016-11-22 23:27:07 +00:00
function fixPrepatch (to, ___) {
2017-01-29 06:49:36 +00:00
if (this._hashChanged) return
2017-07-09 21:41:04 +00:00
2016-11-22 23:27:07 +00:00
Vue.nextTick(() => {
2017-07-09 21:41:04 +00:00
const instances = getMatchedComponentsInstances(to)
2016-12-20 12:44:00 +00:00
_lastComponentsFiles = instances.map((instance, i) => {
if (!instance) return '';
2017-07-09 21:41:04 +00:00
2016-12-20 13:11:51 +00:00
if (_lastPaths[i] === instance.constructor._path && typeof instance.constructor.options.data === 'function') {
2017-07-09 21:41:04 +00:00
const newData = instance.constructor.options.data.call(instance)
2016-11-22 23:27:07 +00:00
for (let key in newData) {
Vue.set(instance.$data, key, newData[key])
}
}
2017-07-09 21:41:04 +00:00
2016-12-20 12:44:00 +00:00
return instance.constructor.options.__file
2016-11-22 23:27:07 +00:00
})
2017-07-09 21:41:04 +00:00
// Hide error component if no error
2017-01-27 22:10:02 +00:00
if (this._hadError && this._dateLastError === this.$options._nuxt.dateErr) {
this.error()
}
2017-07-09 21:41:04 +00:00
2017-02-20 22:11:34 +00:00
// Set layout
2017-03-17 17:02:58 +00:00
let layout = this.$options._nuxt.err ? NuxtError.layout : to.matched[0].components.default.options.layout
if (typeof layout === 'function') {
layout = layout(this._context)
}
this.setLayout(layout)
2017-07-09 21:41:04 +00:00
2017-07-09 23:57:50 +00:00
<% if (isDev) { %>
2017-07-09 21:41:04 +00:00
// Hot reloading
2017-03-24 15:52:18 +00:00
setTimeout(() => hotReloadAPI(this), 100)
2017-07-09 23:57:50 +00:00
<% } %>
2016-11-22 23:27:07 +00:00
})
}
2017-07-09 21:41:04 +00:00
function nuxtReady (app) {
window._nuxtReadyCbs.forEach((cb) => {
if (typeof cb === 'function') {
cb(app)
}
})
// Special JSDOM
if (typeof window._onNuxtLoaded === 'function') {
window._onNuxtLoaded(app)
}
// Add router hooks
router.afterEach(function (to, from) {
app.$nuxt.$emit('routeChanged', to, from)
})
}
<% if (isDev) { %>
2017-03-16 17:52:06 +00:00
// Special hot reload with asyncData(context)
2016-11-07 01:34:58 +00:00
function hotReloadAPI (_app) {
2016-12-24 13:15:00 +00:00
if (!module.hot) return
2017-07-27 14:26:36 +00:00
2017-03-16 17:52:06 +00:00
let $components = []
let $nuxt = _app.$nuxt
2017-07-27 14:26:36 +00:00
2017-03-16 17:52:06 +00:00
while ($nuxt && $nuxt.$children && $nuxt.$children.length) {
2017-07-09 21:41:04 +00:00
$nuxt.$children.forEach((child, i) => {
2017-03-16 17:52:06 +00:00
if (child.$vnode.data.nuxtChild) {
let hasAlready = false
2017-07-09 21:41:04 +00:00
$components.forEach(component => {
2017-03-16 17:52:06 +00:00
if (component.$options.__file === child.$options.__file) {
hasAlready = true
}
})
if (!hasAlready) {
$components.push(child)
}
}
$nuxt = child
})
}
2017-07-27 14:26:36 +00:00
2017-03-16 17:52:06 +00:00
$components.forEach(addHotReload.bind(_app))
}
function addHotReload ($component, depth) {
if ($component.$vnode.data._hasHotReload) return
$component.$vnode.data._hasHotReload = true
2017-07-09 21:41:04 +00:00
2017-03-16 17:52:06 +00:00
var _forceUpdate = $component.$forceUpdate.bind($component.$parent)
2017-07-09 21:41:04 +00:00
2017-03-16 17:52:06 +00:00
$component.$vnode.context.$forceUpdate = () => {
2017-03-17 17:02:58 +00:00
let Components = getMatchedComponents(router.currentRoute)
let Component = Components[depth]
2016-11-07 01:34:58 +00:00
if (!Component) return _forceUpdate()
if (typeof Component === 'object' && !Component.options) {
// Updated via vue-router resolveAsyncComponents()
Component = Vue.extend(Component)
Component._Ctor = Component
}
2017-03-16 17:52:06 +00:00
this.error()
2016-11-07 01:34:58 +00:00
let promises = []
2016-11-10 23:01:36 +00:00
const next = function (path) {
<%= (loading ? 'this.$loading.finish && this.$loading.finish()' : '') %>
router.push(path)
}
2017-08-23 11:41:25 +00:00
let context = getContext({ route: router.currentRoute<%= (store ? ', store' : '') %>, isClient: true, isHMR: true, next: next.bind(this), error: this.error }, app)
2016-11-22 23:27:07 +00:00
<%= (loading ? 'this.$loading.start && this.$loading.start()' : '') %>
2017-03-17 17:02:58 +00:00
callMiddleware.call(this, Components, context)
.then(() => {
// If layout changed
if (depth !== 0) return Promise.resolve()
let layout = Component.options.layout || 'default'
if (typeof layout === 'function') {
layout = layout(context)
}
if (this.layoutName === layout) return Promise.resolve()
let promise = this.loadLayout(layout)
promise.then(() => {
this.setLayout(layout)
Vue.nextTick(() => hotReloadAPI(this))
})
return promise
})
.then(() => {
return callMiddleware.call(this, Components, context, this.layout)
})
.then(() => {
2017-04-14 14:31:14 +00:00
// Call asyncData(context)
2017-03-17 17:02:58 +00:00
let pAsyncData = promisify(Component.options.asyncData || noopData, context)
pAsyncData.then((asyncDataResult) => {
2017-04-14 14:31:14 +00:00
applyAsyncData(Component, asyncDataResult)
2017-03-17 17:02:58 +00:00
<%= (loading ? 'this.$loading.increase && this.$loading.increase(30)' : '') %>
})
promises.push(pAsyncData)
// Call fetch()
Component.options.fetch = Component.options.fetch || noopFetch
let pFetch = Component.options.fetch(context)
if (!pFetch || (!(pFetch instanceof Promise) && (typeof pFetch.then !== 'function'))) { pFetch = Promise.resolve(pFetch) }
2017-03-17 17:02:58 +00:00
<%= (loading ? 'pFetch.then(() => this.$loading.increase && this.$loading.increase(30))' : '') %>
promises.push(pFetch)
return Promise.all(promises)
})
.then(() => {
2016-11-10 23:01:36 +00:00
<%= (loading ? 'this.$loading.finish && this.$loading.finish()' : '') %>
2016-11-07 01:34:58 +00:00
_forceUpdate()
2017-03-16 17:52:06 +00:00
setTimeout(() => hotReloadAPI(this), 100)
2016-11-07 01:34:58 +00:00
})
}
}
2017-07-09 21:41:04 +00:00
<% } %>
2016-11-07 01:34:58 +00:00
2017-07-09 21:41:04 +00:00
async function mountApp(__app) {
// Set global variables
app = __app.app
router = __app.router
2017-07-09 21:41:04 +00:00
<% if (store) { %>store = __app.store <% } %>
// Resolve route components
const Components = await Promise.all(resolveComponents(router))
2017-07-09 21:41:04 +00:00
// Create Vue instance
2016-11-07 01:34:58 +00:00
const _app = new Vue(app)
2017-07-09 21:41:04 +00:00
// Load layout
const layout = NUXT.layout || 'default'
await _app.loadLayout(layout)
_app.setLayout(layout)
2017-07-09 21:41:04 +00:00
// Mounts Vue app to DOM element
const mountApp = () => {
_app.$mount('#__nuxt')
2017-07-09 21:41:04 +00:00
// Listen for first Vue update
2017-03-02 16:31:37 +00:00
Vue.nextTick(() => {
// Call window.onNuxtReady callbacks
nuxtReady(_app)
2017-07-09 21:41:04 +00:00
<% if (isDev) { %>
// Enable hot reloading
hotReloadAPI(_app)
<% } %>
2017-03-02 16:31:37 +00:00
})
}
2017-07-09 21:41:04 +00:00
// Enable transitions
_app.setTransitions = _app.$options._nuxt.setTransitions.bind(_app)
if (Components.length) {
_app.setTransitions(mapTransitions(Components, router.currentRoute))
2017-07-09 21:41:04 +00:00
_lastPaths = router.currentRoute.matched.map(route => compile(route.path)(router.currentRoute.params))
_lastComponentsFiles = Components.map(Component => Component.options.__file)
}
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
// Initialize error handler
_app.error = _app.$options._nuxt.error.bind(_app)
2017-07-09 21:41:04 +00:00
_app.$loading = {} // To avoid error while _app.$nuxt does not exist
2016-11-07 01:34:58 +00:00
if (NUXT.error) _app.error(NUXT.error)
2017-07-09 21:41:04 +00:00
2016-11-07 01:34:58 +00:00
// Add router hooks
router.beforeEach(loadAsyncComponents.bind(_app))
router.beforeEach(render.bind(_app))
2017-01-20 17:32:43 +00:00
router.afterEach(normalizeComponents)
2016-11-22 23:27:07 +00:00
router.afterEach(fixPrepatch.bind(_app))
2017-07-27 14:26:36 +00:00
2017-07-09 21:41:04 +00:00
// If page already is server rendered
if (NUXT.serverRendered) {
mountApp()
return
2016-11-07 01:34:58 +00:00
}
2017-07-09 21:41:04 +00:00
render.call(_app, router.currentRoute, router.currentRoute, path => {
if (!path) {
normalizeComponents(router.currentRoute, router.currentRoute)
fixPrepatch.call(_app, router.currentRoute, router.currentRoute)
mountApp()
return
}
2017-07-09 21:41:04 +00:00
// Push the path and then mount app
let mounted = false
router.afterEach(() => {
if (mounted) return
mounted = true
mountApp()
})
router.push(path)
})
2017-07-27 14:26:36 +00:00
}