2016-11-10 02:38:11 +00:00
|
|
|
'use strict'
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
const debug = require('debug')('nuxt:render')
|
|
|
|
import Vue from 'vue'
|
2016-11-10 23:01:36 +00:00
|
|
|
import { stringify } from 'querystring'
|
2016-11-17 13:02:09 +00:00
|
|
|
import { omit } from 'lodash'
|
2016-11-07 01:34:58 +00:00
|
|
|
import { app, router<%= (store ? ', store' : '') %> } from './index'
|
2016-11-19 21:16:26 +00:00
|
|
|
import { getMatchedComponents, getContext, promisify, urlJoin } from './utils'
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2016-11-09 22:59:41 +00:00
|
|
|
const isDev = <%= isDev %>
|
2016-11-07 01:34:58 +00:00
|
|
|
const _app = new Vue(app)
|
|
|
|
|
2016-11-21 13:14:35 +00:00
|
|
|
// Fix issue from vue-router Abstract mode with base (used for server-side rendering)
|
2016-11-10 16:16:37 +00:00
|
|
|
router.history.base = '<%= router.base %>'
|
|
|
|
|
2016-11-07 01:34:58 +00:00
|
|
|
// This exported function will be called by `bundleRenderer`.
|
|
|
|
// This is where we perform data-prefetching to determine the
|
|
|
|
// state of our application before actually rendering it.
|
|
|
|
// Since data fetching is async, this function is expected to
|
|
|
|
// return a Promise that resolves to the app instance.
|
|
|
|
export default context => {
|
2016-11-11 00:11:21 +00:00
|
|
|
// Add store to the context
|
|
|
|
<%= (store ? 'context.store = store' : '') %>
|
|
|
|
// Nuxt object
|
|
|
|
context.nuxt = { data: [], error: null<%= (store ? ', state: null' : '') %>, serverRendered: true }
|
2016-11-10 23:01:36 +00:00
|
|
|
// create context.next for simulate next() of beforeEach() when wanted to redirect
|
|
|
|
context.redirected = false
|
2016-11-11 00:35:05 +00:00
|
|
|
context.next = function (opts) {
|
2016-11-10 23:01:36 +00:00
|
|
|
if (!context.res) {
|
2016-11-11 00:35:05 +00:00
|
|
|
context.redirected = opts
|
2016-11-11 00:11:21 +00:00
|
|
|
context.nuxt.serverRendered = false
|
2016-11-10 23:01:36 +00:00
|
|
|
return
|
|
|
|
}
|
2016-11-11 00:35:05 +00:00
|
|
|
opts.query = stringify(opts.query)
|
|
|
|
opts.path = opts.path + (opts.query ? '?' + opts.query : '')
|
|
|
|
opts.path = urlJoin('<%= router.base %>', opts.path)
|
|
|
|
context.res.writeHead(opts.status, {
|
|
|
|
'Location': opts.path
|
2016-11-10 23:01:36 +00:00
|
|
|
})
|
|
|
|
context.res.end()
|
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
// set router's location
|
|
|
|
router.push(context.url)
|
|
|
|
// Add route to the context
|
|
|
|
context.route = router.currentRoute
|
|
|
|
// Add meta infos
|
|
|
|
context.meta = _app.$meta()
|
2016-11-17 10:57:02 +00:00
|
|
|
// Error function
|
2016-11-21 13:14:35 +00:00
|
|
|
context.error = _app.$options._nuxt.error.bind(_app)
|
2016-11-07 01:34:58 +00:00
|
|
|
|
|
|
|
<%= (isDev ? 'const s = isDev && Date.now()' : '') %>
|
2016-11-17 21:12:21 +00:00
|
|
|
let Components = getMatchedComponents(context.route)
|
2016-11-17 12:52:00 +00:00
|
|
|
<% if (store) { %>
|
2016-11-17 13:02:09 +00:00
|
|
|
let promise = (store._actions && store._actions.nuxtServerInit ? store.dispatch('nuxtServerInit', omit(getContext(context), 'redirect', 'error')) : null)
|
2016-11-17 12:52:00 +00:00
|
|
|
if (!(promise instanceof Promise)) promise = Promise.resolve()
|
|
|
|
<% } else { %>
|
|
|
|
let promise = Promise.resolve()
|
|
|
|
<% } %>
|
|
|
|
return promise
|
|
|
|
.then(() => {
|
2016-11-18 03:02:43 +00:00
|
|
|
// Call data & fetch hooks on components matched by the route.
|
2016-11-17 12:52:00 +00:00
|
|
|
return Promise.all(Components.map((Component) => {
|
|
|
|
let promises = []
|
|
|
|
if (!Component.options) {
|
|
|
|
Component = Vue.extend(Component)
|
|
|
|
Component._Ctor = Component
|
2016-11-18 13:45:25 +00:00
|
|
|
} else {
|
|
|
|
Component._Ctor = Component
|
|
|
|
Component.extendOptions = Component.options
|
2016-11-17 12:52:00 +00:00
|
|
|
}
|
2016-11-19 21:16:26 +00:00
|
|
|
const ctx = getContext(context)
|
2016-11-17 12:52:00 +00:00
|
|
|
if (Component.options.data && typeof Component.options.data === 'function') {
|
|
|
|
Component._data = Component.options.data
|
2016-11-19 21:16:26 +00:00
|
|
|
let promise = promisify(Component._data, ctx)
|
2016-11-17 12:52:00 +00:00
|
|
|
promise.then((data) => {
|
|
|
|
Component.options.data = () => data
|
|
|
|
Component._Ctor.options.data = Component.options.data
|
|
|
|
})
|
|
|
|
promises.push(promise)
|
|
|
|
} else {
|
|
|
|
promises.push(null)
|
|
|
|
}
|
|
|
|
if (Component.options.fetch) {
|
2016-11-19 21:16:26 +00:00
|
|
|
promises.push(Component.options.fetch(ctx))
|
2016-11-17 12:52:00 +00:00
|
|
|
}
|
|
|
|
return Promise.all(promises)
|
|
|
|
}))
|
|
|
|
})
|
2016-11-07 01:34:58 +00:00
|
|
|
.then((res) => {
|
2016-11-17 21:12:21 +00:00
|
|
|
if (!Components.length) {
|
2016-11-21 13:14:35 +00:00
|
|
|
context.nuxt.error = context.error({ statusCode: 404, message: 'This page could not be found.', url: context.route.path })
|
2016-11-17 21:12:21 +00:00
|
|
|
<%= (store ? 'context.nuxt.state = store.state' : '') %>
|
|
|
|
return _app
|
|
|
|
}
|
2016-11-07 01:34:58 +00:00
|
|
|
<% if (isDev) { %>
|
2016-11-09 22:59:41 +00:00
|
|
|
debug('Data fetching ' + context.req.url + ': ' + (Date.now() - s) + 'ms')
|
2016-11-07 01:34:58 +00:00
|
|
|
<% } %>
|
|
|
|
// datas are the first row of each
|
|
|
|
context.nuxt.data = res.map((tab) => tab[0])
|
2016-11-21 13:14:35 +00:00
|
|
|
context.nuxt.error = _app.$options._nuxt.err
|
2016-11-07 01:34:58 +00:00
|
|
|
<%= (store ? '// Add the state from the vuex store' : '') %>
|
|
|
|
<%= (store ? 'context.nuxt.state = store.state' : '') %>
|
|
|
|
return _app
|
|
|
|
})
|
|
|
|
.catch(function (error) {
|
2016-11-21 13:14:35 +00:00
|
|
|
context.nuxt.error = context.error(error)
|
2016-11-07 01:34:58 +00:00
|
|
|
<%= (store ? 'context.nuxt.state = store.state' : '') %>
|
|
|
|
return _app
|
|
|
|
})
|
|
|
|
}
|