Nuxt/lib/app/client.js

241 lines
8.8 KiB
JavaScript
Raw Normal View History

'use strict'
2016-11-07 01:34:58 +00:00
require('es6-object-assign').polyfill()
2016-11-16 16:55:15 +00:00
import 'es6-promise/auto'
2016-11-07 01:34:58 +00:00
import Vue from 'vue'
import { app, router<%= (store ? ', store' : '') %> } from './index'
import { getMatchedComponents, flatMapComponents, getContext, promisify, getLocation } from './utils'
2016-11-07 01:34:58 +00:00
const noopData = () => { return {} }
const noopFetch = () => {}
function loadAsyncComponents (to, ___, next) {
2016-11-07 01:34:58 +00:00
const resolveComponents = flatMapComponents(to, (Component, _, match, key) => {
if (typeof Component === 'function' && !Component.options) {
return new Promise(function (resolve, reject) {
const _resolve = (Component) => {
// console.log('Component loaded', Component, match.path, key)
2016-11-18 13:45:25 +00:00
if (!Component.options) {
Component = Vue.extend(Component) // fix issue #6
Component._Ctor = Component
} else {
Component._Ctor = Component
Component.extendOptions = Component.options
}
2016-11-07 01:34:58 +00:00
match.components[key] = Component
resolve(Component)
}
Component().then(_resolve).catch(reject)
})
}
if (typeof Component === 'object' && !Component.options) {
// Updated via vue-router resolveAsyncComponents()
Component = Vue.extend(Component)
Component._Ctor = Component
match.components[key] = Component
}
2016-11-07 01:34:58 +00:00
return Component
})
<%= (loading ? 'this.$loading.start && this.$loading.start()' : '') %>
Promise.all(resolveComponents)
.then(() => next())
.catch((err) => {
this.error({ statusCode: 500, message: err.message })
next(false)
})
}
function render (to, ___, next) {
2016-11-07 01:34:58 +00:00
let Components = getMatchedComponents(to)
if (!Components.length) {
this.error({ statusCode: 404, message: 'This page could not be found.', url: to.path })
return next()
}
// console.log('Load components', Components, to.path)
// Update ._data and other properties if hot reloaded
Components.forEach(function (Component) {
if (!Component._data) {
Component._data = Component.options.data || noopData
2016-11-07 01:34:58 +00:00
}
if (Component._Ctor && Component._Ctor.options && Component._dataFn) {
Component.options.fetch = Component._Ctor.options.fetch
2016-11-07 01:34:58 +00:00
const originalDataFn = Component._data.toString().replace(/\s/g, '')
const dataFn = Component._dataFn
2016-11-07 01:34:58 +00:00
const newDataFn = (Component._Ctor.options.data || noopData).toString().replace(/\s/g, '')
// If component data method changed
if (newDataFn !== originalDataFn && newDataFn !== dataFn) {
Component._data = Component._Ctor.options.data || noopData
}
}
})
this.error()
2016-11-10 23:01:36 +00:00
let nextCalled = false
2016-11-07 01:34:58 +00:00
Promise.all(Components.map((Component) => {
let promises = []
2016-11-10 23:01:36 +00:00
const _next = function (path) {
<%= (loading ? 'this.$loading.finish && this.$loading.finish()' : '') %>
nextCalled = true
next(path)
}
const context = getContext({ to<%= (store ? ', store' : '') %>, isClient: true, next: _next.bind(this), error: this.error.bind(this) })
2016-11-07 01:34:58 +00:00
if (Component._data && typeof Component._data === 'function') {
var promise = promisify(Component._data, context)
2016-11-07 01:34:58 +00:00
promise.then((data) => {
Component.options.data = () => data || {}
Component._dataFn = Component.options.data.toString().replace(/\s/g, '')
2016-11-07 01:34:58 +00:00
if (Component._Ctor && Component._Ctor.options) {
Component._Ctor.options.data = Component.options.data
2016-11-07 01:34:58 +00:00
}
2016-11-11 14:30:11 +00:00
<%= (loading ? 'this.$loading.increase && this.$loading.increase(30)' : '') %>
2016-11-07 01:34:58 +00:00
})
promises.push(promise)
}
if (Component.options.fetch) {
var p = Component.options.fetch(context)
if (!(p instanceof Promise)) { p = Promise.resolve(p) }
2016-11-07 01:34:58 +00:00
<%= (loading ? 'p.then(() => this.$loading.increase && this.$loading.increase(30))' : '') %>
promises.push(p)
}
return Promise.all(promises)
}))
.then(() => {
<%= (loading ? 'this.$loading.finish && this.$loading.finish()' : '') %>
2016-11-10 23:01:36 +00:00
// If not redirected
if (!nextCalled) {
next()
}
2016-11-07 01:34:58 +00:00
})
.catch((error) => {
2016-11-07 01:34:58 +00:00
this.error(error)
next(false)
})
}
// Special hot reload with data(context)
function hotReloadAPI (_app) {
var _forceUpdate = _app.$forceUpdate.bind(_app)
_app.$forceUpdate = function () {
let Component = getMatchedComponents(router.currentRoute)[0]
if (!Component) return _forceUpdate()
if (typeof Component === 'object' && !Component.options) {
// Updated via vue-router resolveAsyncComponents()
Component = Vue.extend(Component)
Component._Ctor = Component
}
2016-11-07 01:34:58 +00:00
<%= (loading ? 'this.$loading.start && this.$loading.start()' : '') %>
let promises = []
2016-11-10 23:01:36 +00:00
const next = function (path) {
<%= (loading ? 'this.$loading.finish && this.$loading.finish()' : '') %>
router.push(path)
}
const context = getContext({ route: router.currentRoute<%= (store ? ', store' : '') %>, isClient: true, next: next.bind(this), error: _app.error.bind(_app) })
2016-11-07 01:34:58 +00:00
// Check if data has been updated
const originalDataFn = (Component._data || noopData).toString().replace(/\s/g, '')
const newDataFn = (Component._Ctor.options.data || noopData).toString().replace(/\s/g, '')
if (originalDataFn !== newDataFn) {
Component._data = Component._Ctor.options.data || noopData
let p = promisify(Component._data, context)
2016-11-07 01:34:58 +00:00
p.then((data) => {
Component.options.data = () => data || {}
Component._dataFn = Component.options.data.toString().replace(/\s/g, '')
Component._Ctor.options.data = Component.options.data
2016-11-07 01:34:58 +00:00
<%= (loading ? 'this.$loading.increase && this.$loading.increase(30)' : '') %>
})
promises.push(p)
}
// Check if fetch has been updated
const originalFetchFn = (Component.options.fetch || noopFetch).toString().replace(/\s/g, '')
2016-11-07 01:34:58 +00:00
const newFetchFn = (Component._Ctor.options.fetch || noopFetch).toString().replace(/\s/g, '')
// Fetch has been updated, we call it to update the store
if (originalFetchFn !== newFetchFn) {
Component.options.fetch = Component._Ctor.options.fetch || noopFetch
let p = Component.options.fetch(context)
2016-11-07 01:34:58 +00:00
if (!(p instanceof Promise)) { p = Promise.resolve(p) }
<%= (loading ? 'p.then(() => this.$loading.increase && this.$loading.increase(30))' : '') %>
promises.push(p)
}
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()
})
}
}
// Load vue app
const NUXT = window.__NUXT__ || {}
if (!NUXT) {
throw new Error('[nuxt.js] cannot find the global variable __NUXT__, make sure the server is working.')
}
<% if (store) { %>
// Replace store state
if (NUXT.state) {
store.replaceState(NUXT.state)
}
<% } %>
// Get matched components
const path = getLocation(router.options.base)
const resolveComponents = flatMapComponents(router.match(path), (Component, _, match, key, index) => {
if (typeof Component === 'function' && !Component.options) {
return new Promise(function (resolve, reject) {
const _resolve = (Component) => {
2016-11-18 13:45:25 +00:00
if (!Component.options) {
Component = Vue.extend(Component) // fix issue #6
Component._Ctor = Component
} else {
Component._Ctor = Component
Component.extendOptions = Component.options
}
if (Component.options.data && typeof Component.options.data === 'function') {
Component._data = Component.options.data
if (NUXT.serverRendered) {
Component.options.data = () => NUXT.data[index] || {}
Component._dataFn = Component.options.data.toString().replace(/\s/g, '')
}
2016-11-07 01:34:58 +00:00
if (Component._Ctor && Component._Ctor.options) {
Component._Ctor.options.data = Component.options.data
2016-11-07 01:34:58 +00:00
}
}
match.components[key] = Component
resolve(Component)
}
Component().then(_resolve).catch(reject)
})
}
return Component
})
Promise.all(resolveComponents)
.then((Components) => {
const _app = new Vue(app)
const mountApp = () => _app.$mount('#__nuxt')
const onNuxtReady = window.onNuxtReady || function () {}
2016-11-07 01:34:58 +00:00
if (NUXT.error) _app.error(NUXT.error)
if (module.hot) hotReloadAPI(_app)
// Add router hooks
router.beforeEach(loadAsyncComponents.bind(_app))
router.beforeEach(render.bind(_app))
if (NUXT.serverRendered) {
mountApp()
// Call window.onModulesLoaded for jsdom testing (https://github.com/tmpvar/jsdom#dealing-with-asynchronous-script-loading)
onNuxtReady(_app)
return
2016-11-07 01:34:58 +00:00
}
render.call(_app, router.currentRoute, router.currentRoute, function (path) {
if (path) {
let mounted = false
router.afterEach(function () {
if (mounted) return
mounted = true
mountApp()
onNuxtReady(_app)
})
router.push(path)
return
}
mountApp()
onNuxtReady(_app)
})
2016-11-07 01:34:58 +00:00
})
.catch((err) => {
console.error('[nuxt.js] Cannot load components', err)
2016-11-07 01:34:58 +00:00
})