2018-10-30 20:42:53 +00:00
|
|
|
import path from 'path'
|
2019-03-08 12:20:03 +00:00
|
|
|
import fs from 'fs-extra'
|
2018-10-30 20:42:53 +00:00
|
|
|
import consola from 'consola'
|
2018-10-31 15:52:35 +00:00
|
|
|
import template from 'lodash/template'
|
2019-04-23 09:16:56 +00:00
|
|
|
import { isModernRequest } from '@nuxt/utils'
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
import SPARenderer from './renderers/spa'
|
|
|
|
import SSRRenderer from './renderers/ssr'
|
|
|
|
import ModernRenderer from './renderers/modern'
|
2018-10-30 20:42:53 +00:00
|
|
|
|
|
|
|
export default class VueRenderer {
|
2019-07-10 10:45:49 +00:00
|
|
|
constructor (context) {
|
2019-04-20 12:02:51 +00:00
|
|
|
this.serverContext = context
|
|
|
|
this.options = this.serverContext.options
|
2019-03-12 15:52:15 +00:00
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Will be set by createRenderer
|
2018-10-31 15:52:35 +00:00
|
|
|
this.renderer = {
|
2018-12-01 10:13:28 +00:00
|
|
|
ssr: undefined,
|
|
|
|
modern: undefined,
|
|
|
|
spa: undefined
|
2018-10-31 15:52:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:42:53 +00:00
|
|
|
// Renderer runtime resources
|
2019-04-20 12:02:51 +00:00
|
|
|
Object.assign(this.serverContext.resources, {
|
2018-12-01 10:13:28 +00:00
|
|
|
clientManifest: undefined,
|
|
|
|
modernManifest: undefined,
|
|
|
|
serverManifest: undefined,
|
|
|
|
ssrTemplate: undefined,
|
|
|
|
spaTemplate: undefined,
|
|
|
|
errorTemplate: this.parseTemplate('Nuxt.js Internal Server Error')
|
2018-10-30 20:42:53 +00:00
|
|
|
})
|
2019-03-23 07:03:08 +00:00
|
|
|
|
|
|
|
// Default status
|
|
|
|
this._state = 'created'
|
|
|
|
this._error = null
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
ready () {
|
2019-03-23 07:03:08 +00:00
|
|
|
if (!this._readyPromise) {
|
|
|
|
this._state = 'loading'
|
|
|
|
this._readyPromise = this._ready()
|
|
|
|
.then(() => {
|
|
|
|
this._state = 'ready'
|
|
|
|
return this
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
this._state = 'error'
|
|
|
|
this._error = error
|
|
|
|
throw error
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return this._readyPromise
|
|
|
|
}
|
2019-03-08 20:43:23 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async _ready () {
|
2019-03-20 09:17:53 +00:00
|
|
|
// Resolve dist path
|
2019-04-20 12:02:51 +00:00
|
|
|
this.distPath = path.resolve(this.options.buildDir, 'dist', 'server')
|
2019-03-20 09:17:53 +00:00
|
|
|
|
2018-12-10 12:16:05 +00:00
|
|
|
// -- Development mode --
|
2019-04-20 12:02:51 +00:00
|
|
|
if (this.options.dev) {
|
|
|
|
this.serverContext.nuxt.hook('build:resources', mfs => this.loadResources(mfs))
|
2018-12-10 12:16:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Production mode --
|
|
|
|
|
|
|
|
// Try once to load SSR resources from fs
|
|
|
|
await this.loadResources(fs)
|
|
|
|
|
2019-03-23 07:02:55 +00:00
|
|
|
// Without using `nuxt start` (programmatic, tests and generate)
|
2019-04-20 12:02:51 +00:00
|
|
|
if (!this.options._start) {
|
|
|
|
this.serverContext.nuxt.hook('build:resources', () => this.loadResources(fs))
|
2019-02-08 16:25:11 +00:00
|
|
|
return
|
2018-12-10 12:16:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify resources
|
2019-04-20 12:02:51 +00:00
|
|
|
if (this.options.modern && !this.isModernReady) {
|
2019-02-08 16:25:11 +00:00
|
|
|
throw new Error(
|
2019-03-20 09:17:53 +00:00
|
|
|
`No modern build files found in ${this.distPath}.\nUse either \`nuxt build --modern\` or \`modern\` option to build modern files.`
|
2019-02-08 16:25:11 +00:00
|
|
|
)
|
2019-03-20 09:17:53 +00:00
|
|
|
} else if (!this.isReady) {
|
2019-02-08 16:25:11 +00:00
|
|
|
throw new Error(
|
2019-03-20 09:17:53 +00:00
|
|
|
`No build files found in ${this.distPath}.\nUse either \`nuxt build\` or \`builder.build()\` or start nuxt in development mode.`
|
2019-02-08 16:25:11 +00:00
|
|
|
)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async loadResources (_fs) {
|
2018-10-30 20:42:53 +00:00
|
|
|
const updated = []
|
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
const readResource = async (fileName, encoding) => {
|
2018-12-01 10:13:28 +00:00
|
|
|
try {
|
2019-03-20 09:17:53 +00:00
|
|
|
const fullPath = path.resolve(this.distPath, fileName)
|
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
if (!await _fs.exists(fullPath)) {
|
2018-12-01 10:13:28 +00:00
|
|
|
return
|
|
|
|
}
|
2019-03-08 12:20:03 +00:00
|
|
|
const contents = await _fs.readFile(fullPath, encoding)
|
2019-03-20 09:17:53 +00:00
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
return contents
|
|
|
|
} catch (err) {
|
|
|
|
consola.error('Unable to load resource:', fileName, err)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
}
|
2018-11-08 09:15:56 +00:00
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
for (const resourceName in this.resourceMap) {
|
|
|
|
const { fileName, transform, encoding } = this.resourceMap[resourceName]
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
// Load resource
|
2019-03-08 12:20:03 +00:00
|
|
|
let resource = await readResource(fileName, encoding)
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
// Skip unavailable resources
|
|
|
|
if (!resource) {
|
|
|
|
continue
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
// Apply transforms
|
|
|
|
if (typeof transform === 'function') {
|
2019-03-08 12:20:03 +00:00
|
|
|
resource = await transform(resource, { readResource })
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
// Update resource
|
2019-04-20 12:02:51 +00:00
|
|
|
this.serverContext.resources[resourceName] = resource
|
2018-12-01 10:13:28 +00:00
|
|
|
updated.push(resourceName)
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
// Load templates
|
|
|
|
await this.loadTemplates()
|
|
|
|
|
2019-08-12 20:55:21 +00:00
|
|
|
await this.serverContext.nuxt.callHook('render:resourcesLoaded', this.serverContext.resources)
|
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
// Detect if any resource updated
|
|
|
|
if (updated.length > 0) {
|
|
|
|
// Create new renderer
|
|
|
|
this.createRenderer()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async loadTemplates () {
|
2018-10-30 20:42:53 +00:00
|
|
|
// Reload error template
|
2019-04-20 12:02:51 +00:00
|
|
|
const errorTemplatePath = path.resolve(this.options.buildDir, 'views/error.html')
|
2019-03-20 09:17:53 +00:00
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
if (await fs.exists(errorTemplatePath)) {
|
|
|
|
const errorTemplate = await fs.readFile(errorTemplatePath, 'utf8')
|
2019-04-20 12:02:51 +00:00
|
|
|
this.serverContext.resources.errorTemplate = this.parseTemplate(errorTemplate)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// Reload loading template
|
2019-04-20 12:02:51 +00:00
|
|
|
const loadingHTMLPath = path.resolve(this.options.buildDir, 'loading.html')
|
2019-03-20 09:17:53 +00:00
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
if (await fs.exists(loadingHTMLPath)) {
|
2019-04-20 12:02:51 +00:00
|
|
|
this.serverContext.resources.loadingHTML = await fs.readFile(loadingHTMLPath, 'utf8')
|
|
|
|
this.serverContext.resources.loadingHTML = this.serverContext.resources.loadingHTML.replace(/\r|\n|[\t\s]{3,}/g, '')
|
2018-10-30 20:42:53 +00:00
|
|
|
} else {
|
2019-04-20 12:02:51 +00:00
|
|
|
this.serverContext.resources.loadingHTML = ''
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:25:11 +00:00
|
|
|
// TODO: Remove in Nuxt 3
|
2019-07-10 10:45:49 +00:00
|
|
|
get noSSR () { /* Backward compatibility */
|
2019-04-20 12:02:51 +00:00
|
|
|
return this.options.render.ssr === false
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
get SSR () {
|
2019-04-20 12:02:51 +00:00
|
|
|
return this.options.render.ssr === true
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
get isReady () {
|
2018-12-01 10:13:28 +00:00
|
|
|
// SPA
|
2019-04-20 12:02:51 +00:00
|
|
|
if (!this.serverContext.resources.spaTemplate || !this.renderer.spa) {
|
2018-10-30 20:42:53 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
// SSR
|
2019-04-20 12:02:51 +00:00
|
|
|
if (this.SSR && (!this.serverContext.resources.ssrTemplate || !this.renderer.ssr)) {
|
2018-12-01 10:13:28 +00:00
|
|
|
return false
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2018-12-01 10:13:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
get isModernReady () {
|
2019-04-20 12:02:51 +00:00
|
|
|
return this.isReady && this.serverContext.resources.modernManifest
|
2019-03-20 09:17:53 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 16:25:11 +00:00
|
|
|
// TODO: Remove in Nuxt 3
|
2019-07-10 10:45:49 +00:00
|
|
|
get isResourcesAvailable () { /* Backward compatibility */
|
2018-12-01 10:13:28 +00:00
|
|
|
return this.isReady
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
detectModernBuild () {
|
2019-04-20 12:02:51 +00:00
|
|
|
const { options, resources } = this.serverContext
|
|
|
|
if ([false, 'client', 'server'].includes(options.modern)) {
|
2018-10-30 20:42:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
if (!resources.modernManifest) {
|
|
|
|
options.modern = false
|
2018-10-30 20:42:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
options.modern = options.render.ssr ? 'server' : 'client'
|
2019-04-26 11:54:37 +00:00
|
|
|
consola.info(`Modern bundles are detected. Modern mode (\`${options.modern}\`) is enabled now.`)
|
2019-02-08 10:05:01 +00:00
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
createRenderer () {
|
2019-04-20 12:02:51 +00:00
|
|
|
// Resource clientManifest is always required
|
|
|
|
if (!this.serverContext.resources.clientManifest) {
|
|
|
|
return
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2019-02-08 10:05:01 +00:00
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
this.detectModernBuild()
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
// Create SPA renderer
|
|
|
|
if (this.serverContext.resources.spaTemplate) {
|
|
|
|
this.renderer.spa = new SPARenderer(this.serverContext)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
// Skip the rest if SSR resources are not available
|
|
|
|
if (this.serverContext.resources.ssrTemplate && this.serverContext.resources.serverManifest) {
|
|
|
|
// Create bundle renderer for SSR
|
|
|
|
this.renderer.ssr = new SSRRenderer(this.serverContext)
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
if (this.options.modern !== false) {
|
|
|
|
this.renderer.modern = new ModernRenderer(this.serverContext)
|
2019-04-01 19:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-20 12:02:51 +00:00
|
|
|
}
|
2019-02-08 10:05:01 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
renderSPA (renderContext) {
|
2019-04-20 12:02:51 +00:00
|
|
|
return this.renderer.spa.render(renderContext)
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
renderSSR (renderContext) {
|
2019-04-20 12:02:51 +00:00
|
|
|
// Call renderToString from the bundleRenderer and generate the HTML (will update the renderContext as well)
|
|
|
|
const renderer = renderContext.modern ? this.renderer.modern : this.renderer.ssr
|
|
|
|
return renderer.render(renderContext)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
async renderRoute (url, renderContext = {}, _retried) {
|
2019-03-20 09:17:53 +00:00
|
|
|
/* istanbul ignore if */
|
|
|
|
if (!this.isReady) {
|
2019-03-23 07:03:08 +00:00
|
|
|
// Production
|
2019-04-20 12:02:51 +00:00
|
|
|
if (!this.options.dev) {
|
2019-03-30 19:05:15 +00:00
|
|
|
if (!_retried && ['loading', 'created'].includes(this._state)) {
|
|
|
|
await this.ready()
|
2019-04-20 12:02:51 +00:00
|
|
|
return this.renderRoute(url, renderContext, true)
|
2019-03-30 19:05:15 +00:00
|
|
|
}
|
2019-03-23 07:03:08 +00:00
|
|
|
switch (this._state) {
|
|
|
|
case 'created':
|
|
|
|
throw new Error('Renderer ready() is not called! Please ensure `nuxt.ready()` is called and awaited.')
|
|
|
|
case 'loading':
|
|
|
|
throw new Error(`Renderer is loading.`)
|
|
|
|
case 'error':
|
|
|
|
throw this._error
|
|
|
|
case 'ready':
|
2019-06-26 15:20:43 +00:00
|
|
|
throw new Error(`Renderer is loaded but not all resources are available! Please check ${this.distPath} existence.`)
|
2019-03-23 07:03:08 +00:00
|
|
|
default:
|
|
|
|
throw new Error('Renderer is in unknown state!')
|
|
|
|
}
|
2019-02-08 10:05:01 +00:00
|
|
|
}
|
2019-03-20 09:17:53 +00:00
|
|
|
// Tell nuxt middleware to render UI
|
|
|
|
return false
|
2019-02-08 10:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Log rendered url
|
|
|
|
consola.debug(`Rendering url ${url}`)
|
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
// Add url to the renderContext
|
|
|
|
renderContext.url = url
|
2019-02-08 10:05:01 +00:00
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
const { req = {} } = renderContext
|
2019-03-03 07:52:59 +00:00
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
// renderContext.spa
|
|
|
|
if (renderContext.spa === undefined) {
|
|
|
|
// TODO: Remove reading from renderContext.res in Nuxt3
|
|
|
|
renderContext.spa = !this.SSR || req.spa || (renderContext.res && renderContext.res.spa)
|
2019-02-08 10:05:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
// renderContext.modern
|
|
|
|
if (renderContext.modern === undefined) {
|
2019-04-23 09:16:56 +00:00
|
|
|
const modernMode = this.options.modern
|
|
|
|
renderContext.modern = modernMode === 'client' || isModernRequest(req, modernMode)
|
2019-02-08 10:05:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 12:02:51 +00:00
|
|
|
// Call renderContext hook
|
|
|
|
await this.serverContext.nuxt.callHook('vue-renderer:context', renderContext)
|
2019-02-08 10:05:01 +00:00
|
|
|
|
|
|
|
// Render SPA or SSR
|
2019-04-20 12:02:51 +00:00
|
|
|
return renderContext.spa
|
|
|
|
? this.renderSPA(renderContext)
|
|
|
|
: this.renderSSR(renderContext)
|
2019-02-08 10:05:01 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
get resourceMap () {
|
2018-12-01 10:13:28 +00:00
|
|
|
return {
|
|
|
|
clientManifest: {
|
|
|
|
fileName: 'client.manifest.json',
|
|
|
|
transform: src => JSON.parse(src)
|
2018-10-30 20:42:53 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
modernManifest: {
|
|
|
|
fileName: 'modern.manifest.json',
|
|
|
|
transform: src => JSON.parse(src)
|
2018-10-31 15:52:35 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
serverManifest: {
|
|
|
|
fileName: 'server.manifest.json',
|
|
|
|
// BundleRenderer needs resolved contents
|
2019-03-08 12:20:03 +00:00
|
|
|
transform: async (src, { readResource }) => {
|
2018-12-01 10:13:28 +00:00
|
|
|
const serverManifest = JSON.parse(src)
|
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
const readResources = async (obj) => {
|
|
|
|
const _obj = {}
|
|
|
|
await Promise.all(Object.keys(obj).map(async (key) => {
|
|
|
|
_obj[key] = await readResource(obj[key])
|
|
|
|
}))
|
|
|
|
return _obj
|
2018-12-01 10:13:28 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 12:20:03 +00:00
|
|
|
const [files, maps] = await Promise.all([
|
|
|
|
readResources(serverManifest.files),
|
|
|
|
readResources(serverManifest.maps)
|
|
|
|
])
|
2018-12-01 10:51:10 +00:00
|
|
|
|
|
|
|
// Try to parse sourcemaps
|
|
|
|
for (const map in maps) {
|
|
|
|
if (maps[map] && maps[map].version) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
maps[map] = JSON.parse(maps[map])
|
|
|
|
} catch (e) {
|
|
|
|
maps[map] = { version: 3, sources: [], mappings: '' }
|
|
|
|
}
|
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...serverManifest,
|
|
|
|
files,
|
|
|
|
maps
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
ssrTemplate: {
|
2018-10-30 20:42:53 +00:00
|
|
|
fileName: 'index.ssr.html',
|
2018-12-01 10:13:28 +00:00
|
|
|
transform: src => this.parseTemplate(src)
|
2018-10-30 20:42:53 +00:00
|
|
|
},
|
2018-12-01 10:13:28 +00:00
|
|
|
spaTemplate: {
|
2018-10-30 20:42:53 +00:00
|
|
|
fileName: 'index.spa.html',
|
2018-12-01 10:13:28 +00:00
|
|
|
transform: src => this.parseTemplate(src)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-01 10:13:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
parseTemplate (templateStr) {
|
2018-12-01 10:13:28 +00:00
|
|
|
return template(templateStr, {
|
2019-11-24 12:31:21 +00:00
|
|
|
interpolate: /{{([\s\S]+?)}}/g,
|
|
|
|
evaluate: /{%([\s\S]+?)%}/g
|
2018-12-01 10:13:28 +00:00
|
|
|
})
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
close () {
|
2018-12-09 22:00:48 +00:00
|
|
|
if (this.__closed) {
|
|
|
|
return
|
|
|
|
}
|
2018-12-09 10:42:22 +00:00
|
|
|
this.__closed = true
|
|
|
|
|
|
|
|
for (const key in this.renderer) {
|
|
|
|
delete this.renderer[key]
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|