2016-11-10 23:01:36 +00:00
|
|
|
import { stringify } from 'querystring'
|
2018-10-24 13:46:06 +00:00
|
|
|
import Vue from 'vue'
|
2018-12-28 16:27:03 +00:00
|
|
|
import middleware from './middleware.js'
|
2019-01-10 18:57:32 +00:00
|
|
|
import { applyAsyncData, getMatchedComponents, middlewareSeries, promisify, urlJoin, sanitizeComponent } from './utils.js'
|
2018-12-28 16:27:03 +00:00
|
|
|
import { createApp, NuxtError } from './index.js'
|
2019-01-06 07:56:59 +00:00
|
|
|
import NuxtLink from './components/nuxt-link.server.js' // should be included after ./index.js
|
2018-12-28 16:27:03 +00:00
|
|
|
|
|
|
|
// Component: <NuxtLink>
|
|
|
|
Vue.component(NuxtLink.name, NuxtLink)
|
|
|
|
Vue.component('NLink', NuxtLink)
|
2017-07-09 11:43:03 +00:00
|
|
|
|
2017-05-04 07:57:10 +00:00
|
|
|
const debug = require('debug')('nuxt:render')
|
|
|
|
debug.color = 4 // force blue color
|
2016-11-07 01:34:58 +00:00
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
const noopApp = () => new Vue({ render: h => h('div') })
|
2017-07-09 11:43:03 +00:00
|
|
|
|
2018-10-24 13:46:06 +00:00
|
|
|
const createNext = ssrContext => (opts) => {
|
2017-10-13 21:53:04 +00:00
|
|
|
ssrContext.redirected = opts
|
2017-07-09 11:43:03 +00:00
|
|
|
// If nuxt generate
|
2017-10-13 21:53:04 +00:00
|
|
|
if (!ssrContext.res) {
|
|
|
|
ssrContext.nuxt.serverRendered = false
|
2017-07-09 11:43:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
opts.query = stringify(opts.query)
|
|
|
|
opts.path = opts.path + (opts.query ? '?' + opts.query : '')
|
2018-10-24 13:46:06 +00:00
|
|
|
const routerBase = '<%= router.base %>'
|
|
|
|
if (!opts.path.startsWith('http') && (routerBase !== '/' && !opts.path.startsWith(routerBase))) {
|
|
|
|
opts.path = urlJoin(routerBase, opts.path)
|
2017-07-09 11:43:03 +00:00
|
|
|
}
|
|
|
|
// Avoid loop redirect
|
2017-10-13 21:53:04 +00:00
|
|
|
if (opts.path === ssrContext.url) {
|
|
|
|
ssrContext.redirected = false
|
2017-07-09 11:43:03 +00:00
|
|
|
return
|
|
|
|
}
|
2017-10-13 21:53:04 +00:00
|
|
|
ssrContext.res.writeHead(opts.status, {
|
2017-07-09 11:43:03 +00:00
|
|
|
'Location': opts.path
|
|
|
|
})
|
2017-10-13 21:53:04 +00:00
|
|
|
ssrContext.res.end()
|
2017-07-09 11:43:03 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2018-08-06 00:12:44 +00:00
|
|
|
export default async (ssrContext) => {
|
2017-10-13 21:53:04 +00:00
|
|
|
// Create ssrContext.next for simulate next() of beforeEach() when wanted to redirect
|
|
|
|
ssrContext.redirected = false
|
|
|
|
ssrContext.next = createNext(ssrContext)
|
|
|
|
// Used for beforeNuxtRender({ Components, nuxtState })
|
|
|
|
ssrContext.beforeRenderFns = []
|
2018-10-09 12:07:23 +00:00
|
|
|
// Nuxt object (window{{globals.context}}, defaults to window.__NUXT__)
|
2018-01-23 09:41:50 +00:00
|
|
|
ssrContext.nuxt = { layout: 'default', data: [], error: null<%= (store ? ', state: null' : '') %>, serverRendered: true }
|
2017-10-13 21:53:04 +00:00
|
|
|
// Create the app definition and the instance (created for each request)
|
|
|
|
const { app, router<%= (store ? ', store' : '') %> } = await createApp(ssrContext)
|
2017-06-06 12:51:49 +00:00
|
|
|
const _app = new Vue(app)
|
2017-07-27 14:26:59 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
// Add meta infos (used in renderer.js)
|
|
|
|
ssrContext.meta = _app.$meta()
|
|
|
|
// Keep asyncData for each matched component in ssrContext (used in app/utils.js via this.$ssrContext)
|
|
|
|
ssrContext.asyncData = {}
|
|
|
|
|
|
|
|
const beforeRender = async () => {
|
|
|
|
// Call beforeNuxtRender() methods
|
2018-10-24 13:46:06 +00:00
|
|
|
await Promise.all(ssrContext.beforeRenderFns.map(fn => promisify(fn, { Components, nuxtState: ssrContext.nuxt })))
|
2017-10-13 21:53:04 +00:00
|
|
|
<% if (store) { %>
|
|
|
|
// Add the state from the vuex store
|
|
|
|
ssrContext.nuxt.state = store.state
|
|
|
|
<% } %>
|
|
|
|
}
|
|
|
|
const renderErrorPage = async () => {
|
|
|
|
// Load layout for error page
|
2018-10-24 13:46:06 +00:00
|
|
|
const errLayout = (typeof NuxtError.layout === 'function' ? NuxtError.layout(app.context) : NuxtError.layout)
|
2017-11-06 17:30:37 +00:00
|
|
|
ssrContext.nuxt.layout = errLayout || 'default'
|
2017-10-13 21:53:04 +00:00
|
|
|
await _app.loadLayout(errLayout)
|
|
|
|
_app.setLayout(errLayout)
|
|
|
|
await beforeRender()
|
|
|
|
return _app
|
|
|
|
}
|
2017-11-24 08:33:47 +00:00
|
|
|
const render404Page = () => {
|
2018-07-12 20:45:14 +00:00
|
|
|
app.context.error({ statusCode: 404, path: ssrContext.url, message: `<%= messages.error_404 %>` })
|
2017-11-24 08:33:47 +00:00
|
|
|
return renderErrorPage()
|
2017-10-13 21:53:04 +00:00
|
|
|
}
|
2017-07-09 11:43:03 +00:00
|
|
|
|
2019-01-24 14:01:33 +00:00
|
|
|
<% if (isDev) { %>const s = Date.now()<% } %>
|
2017-07-27 14:26:59 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
// Components are already resolved by setContext -> getRouteData (app/utils.js)
|
|
|
|
const Components = getMatchedComponents(router.match(ssrContext.url))
|
2017-07-09 11:43:03 +00:00
|
|
|
|
2017-07-09 12:02:26 +00:00
|
|
|
<% if (store) { %>
|
2017-10-13 21:53:04 +00:00
|
|
|
/*
|
|
|
|
** Dispatch store nuxtServerInit
|
|
|
|
*/
|
2017-07-09 11:43:03 +00:00
|
|
|
if (store._actions && store._actions.nuxtServerInit) {
|
2017-11-10 06:04:43 +00:00
|
|
|
try {
|
|
|
|
await store.dispatch('nuxtServerInit', app.context)
|
|
|
|
} catch (err) {
|
2017-11-10 21:51:10 +00:00
|
|
|
debug('error occurred when calling nuxtServerInit: ', err.message)
|
2017-11-10 06:04:43 +00:00
|
|
|
throw err
|
|
|
|
}
|
2017-07-09 11:43:03 +00:00
|
|
|
}
|
2017-10-13 21:53:04 +00:00
|
|
|
// ...If there is a redirect or an error, stop the process
|
|
|
|
if (ssrContext.redirected) return noopApp()
|
2017-11-24 08:33:47 +00:00
|
|
|
if (ssrContext.nuxt.error) return renderErrorPage()
|
2017-07-09 12:02:26 +00:00
|
|
|
<% } %>
|
2017-07-09 11:43:03 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
/*
|
|
|
|
** Call global middleware (nuxt.config.js)
|
|
|
|
*/
|
2018-10-24 13:46:06 +00:00
|
|
|
let midd = <%= serialize(router.middleware).replace('middleware(', 'function(') %><%= isTest ? '// eslint-disable-line' : '' %>
|
2017-05-04 07:57:10 +00:00
|
|
|
midd = midd.map((name) => {
|
2017-11-03 16:14:05 +00:00
|
|
|
if (typeof name === 'function') return name
|
2017-05-04 07:57:10 +00:00
|
|
|
if (typeof middleware[name] !== 'function') {
|
2018-01-16 11:01:03 +00:00
|
|
|
app.context.error({ statusCode: 500, message: 'Unknown middleware ' + name })
|
2017-05-04 07:57:10 +00:00
|
|
|
}
|
|
|
|
return middleware[name]
|
2017-03-17 17:02:58 +00:00
|
|
|
})
|
2017-10-13 21:53:04 +00:00
|
|
|
await middlewareSeries(midd, app.context)
|
|
|
|
// ...If there is a redirect or an error, stop the process
|
|
|
|
if (ssrContext.redirected) return noopApp()
|
2017-11-24 08:33:47 +00:00
|
|
|
if (ssrContext.nuxt.error) return renderErrorPage()
|
2017-10-13 21:53:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Set layout
|
|
|
|
*/
|
2017-05-04 07:57:10 +00:00
|
|
|
let layout = Components.length ? Components[0].options.layout : NuxtError.layout
|
2017-10-13 21:53:04 +00:00
|
|
|
if (typeof layout === 'function') layout = layout(app.context)
|
2017-05-04 07:57:10 +00:00
|
|
|
await _app.loadLayout(layout)
|
2018-08-21 14:50:31 +00:00
|
|
|
if (ssrContext.nuxt.error) return renderErrorPage()
|
2017-05-04 07:57:10 +00:00
|
|
|
layout = _app.setLayout(layout)
|
2017-10-13 21:53:04 +00:00
|
|
|
ssrContext.nuxt.layout = _app.layoutName
|
2017-07-27 14:26:59 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
/*
|
|
|
|
** Call middleware (layout + pages)
|
|
|
|
*/
|
|
|
|
midd = []
|
2019-01-10 18:57:32 +00:00
|
|
|
layout = sanitizeComponent(layout)
|
|
|
|
if (layout.options.middleware) midd = midd.concat(layout.options.middleware)
|
2017-10-13 21:53:04 +00:00
|
|
|
Components.forEach((Component) => {
|
|
|
|
if (Component.options.middleware) {
|
|
|
|
midd = midd.concat(Component.options.middleware)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
midd = midd.map((name) => {
|
2017-11-03 16:14:05 +00:00
|
|
|
if (typeof name === 'function') return name
|
2017-10-13 21:53:04 +00:00
|
|
|
if (typeof middleware[name] !== 'function') {
|
|
|
|
app.context.error({ statusCode: 500, message: 'Unknown middleware ' + name })
|
|
|
|
}
|
|
|
|
return middleware[name]
|
|
|
|
})
|
|
|
|
await middlewareSeries(midd, app.context)
|
|
|
|
// ...If there is a redirect or an error, stop the process
|
|
|
|
if (ssrContext.redirected) return noopApp()
|
2017-11-24 08:33:47 +00:00
|
|
|
if (ssrContext.nuxt.error) return renderErrorPage()
|
2017-10-13 21:53:04 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
** Call .validate()
|
|
|
|
*/
|
2017-05-04 07:57:10 +00:00
|
|
|
let isValid = true
|
2018-08-25 09:42:00 +00:00
|
|
|
try {
|
|
|
|
for (const Component of Components) {
|
|
|
|
if (typeof Component.options.validate !== 'function') {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
isValid = await Component.options.validate(app.context)
|
|
|
|
|
|
|
|
if (!isValid) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (validationError) {
|
|
|
|
// ...If .validate() threw an error
|
|
|
|
app.context.error({
|
|
|
|
statusCode: validationError.statusCode || '500',
|
|
|
|
message: validationError.message
|
|
|
|
})
|
|
|
|
return renderErrorPage()
|
|
|
|
}
|
|
|
|
|
2017-07-09 11:43:03 +00:00
|
|
|
// ...If .validate() returned false
|
2017-05-04 07:57:10 +00:00
|
|
|
if (!isValid) {
|
|
|
|
// Don't server-render the page in generate mode
|
2017-10-13 21:53:04 +00:00
|
|
|
if (ssrContext._generate) ssrContext.nuxt.serverRendered = false
|
|
|
|
// Render a 404 error page
|
|
|
|
return render404Page()
|
2017-05-04 07:57:10 +00:00
|
|
|
}
|
2017-07-27 14:26:59 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
// If no Components found, returns 404
|
|
|
|
if (!Components.length) return render404Page()
|
|
|
|
|
2017-05-04 07:57:10 +00:00
|
|
|
// Call asyncData & fetch hooks on components matched by the route.
|
2018-10-24 13:46:06 +00:00
|
|
|
const asyncDatas = await Promise.all(Components.map((Component) => {
|
|
|
|
const promises = []
|
2017-07-27 14:26:59 +00:00
|
|
|
|
2017-07-09 11:43:03 +00:00
|
|
|
// Call asyncData(context)
|
2017-05-04 07:57:10 +00:00
|
|
|
if (Component.options.asyncData && typeof Component.options.asyncData === 'function') {
|
2018-10-24 13:46:06 +00:00
|
|
|
const promise = promisify(Component.options.asyncData, app.context)
|
2018-08-06 00:12:44 +00:00
|
|
|
promise.then((asyncDataResult) => {
|
2017-10-13 21:53:04 +00:00
|
|
|
ssrContext.asyncData[Component.cid] = asyncDataResult
|
2017-07-06 21:33:11 +00:00
|
|
|
applyAsyncData(Component)
|
2017-05-04 07:57:10 +00:00
|
|
|
return asyncDataResult
|
|
|
|
})
|
|
|
|
promises.push(promise)
|
2017-07-09 11:43:03 +00:00
|
|
|
} else {
|
|
|
|
promises.push(null)
|
|
|
|
}
|
|
|
|
|
2017-07-02 18:47:01 +00:00
|
|
|
// Call fetch(context)
|
2017-07-09 11:43:03 +00:00
|
|
|
if (Component.options.fetch) {
|
2017-10-13 21:53:04 +00:00
|
|
|
promises.push(Component.options.fetch(app.context))
|
2018-10-24 13:46:06 +00:00
|
|
|
} else {
|
2017-07-09 11:43:03 +00:00
|
|
|
promises.push(null)
|
|
|
|
}
|
|
|
|
|
2017-07-09 21:06:17 +00:00
|
|
|
return Promise.all(promises)
|
2017-05-04 07:57:10 +00:00
|
|
|
}))
|
2017-07-27 14:26:59 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
<% if (isDev) { %>if (asyncDatas.length) debug('Data fetching ' + ssrContext.url + ': ' + (Date.now() - s) + 'ms')<% } %>
|
2017-07-09 11:43:03 +00:00
|
|
|
|
2017-05-04 07:57:10 +00:00
|
|
|
// datas are the first row of each
|
2017-10-13 21:53:04 +00:00
|
|
|
ssrContext.nuxt.data = asyncDatas.map(r => r[0] || {})
|
2017-07-09 11:43:03 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
// ...If there is a redirect or an error, stop the process
|
|
|
|
if (ssrContext.redirected) return noopApp()
|
2017-11-24 08:33:47 +00:00
|
|
|
if (ssrContext.nuxt.error) return renderErrorPage()
|
2017-07-27 14:26:59 +00:00
|
|
|
|
2017-10-13 21:53:04 +00:00
|
|
|
// Call beforeNuxtRender methods & add store state
|
|
|
|
await beforeRender()
|
2017-10-20 10:05:22 +00:00
|
|
|
|
2017-05-04 07:57:10 +00:00
|
|
|
return _app
|
2016-11-07 01:34:58 +00:00
|
|
|
}
|