feat(vue-renderer): support render.ssrLog for controlling SSR logs (#5784)

This commit is contained in:
Xin Du (Clark) 2019-05-23 10:49:16 +01:00 committed by Pooya Parsa
parent fd3ee629fa
commit 21aaef3b48
8 changed files with 29 additions and 8 deletions

View File

@ -6,5 +6,8 @@ export default {
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: 'Meta description' }
]
},
render: {
ssrLog: 'collapsed'
}
}

View File

@ -8,6 +8,7 @@ export default () => ({
},
resourceHints: true,
ssr: undefined,
ssrLog: undefined,
http2: {
push: false,
shouldPush: null,

View File

@ -299,6 +299,10 @@ export function getNuxtConfig(_options) {
options.pageTransition.appear = true
}
options.render.ssrLog = options.dev
? options.render.ssrLog === undefined || options.render.ssrLog
: false
// We assume the SPA fallback path is 404.html (for GitHub Pages, Surge, etc.)
if (options.generate.fallback === true) {
options.generate.fallback = '404.html'

View File

@ -308,6 +308,7 @@ Object {
},
"resourceHints": true,
"ssr": true,
"ssrLog": false,
"static": Object {
"prefix": true,
},

View File

@ -275,6 +275,7 @@ Object {
},
"resourceHints": true,
"ssr": undefined,
"ssrLog": undefined,
"static": Object {
"prefix": true,
},
@ -609,6 +610,7 @@ Object {
},
"resourceHints": true,
"ssr": undefined,
"ssrLog": undefined,
"static": Object {
"prefix": true,
},

View File

@ -23,5 +23,6 @@ export interface NuxtConfigurationRender {
http2?: any // TBD
resourceHints?: boolean
ssr?: boolean
ssrLog?: boolean | 'collapsed'
static?: ServeStaticOptions
}

View File

@ -37,10 +37,10 @@ const NUXT = window.<%= globals.context %> || {}
Object.assign(Vue.config, <%= serialize(vue.config) %>)<%= isTest ? '// eslint-disable-line' : '' %>
<% if (isDev) { %>
<% if (nuxtOptions.render.ssrLog) { %>
const logs = NUXT.logs || []
if (logs.length > 0) {
console.group("%c🚀 Nuxt.js SSR Logs", 'font-size: 110%')
console.group<%= nuxtOptions.render.ssrLog === 'collapsed' ? 'Collapsed' : '' %>("%c🚀 Nuxt.js SSR Logs", 'font-size: 110%')
const logger = consola.withScope('nuxt:ssr')
logs.forEach(logObj => logger[logObj.type](logObj))
delete NUXT.logs

View File

@ -39,7 +39,10 @@ export default class SSRRenderer extends BaseRenderer {
)
}
async devRenderToString(renderContext) {
useSSRLog() {
if (!this.options.render.ssrLog) {
return
}
const logs = []
const devReporter = {
log(logObj) {
@ -50,19 +53,25 @@ export default class SSRRenderer extends BaseRenderer {
}
}
consola.addReporter(devReporter)
const APP = await this.vueRenderer.renderToString(renderContext)
consola.removeReporter(devReporter)
renderContext.nuxt.logs = logs
return APP
return () => {
consola.removeReporter(devReporter)
return logs
}
}
async render(renderContext) {
// Call ssr:context hook to extend context from modules
await this.serverContext.nuxt.callHook('vue-renderer:ssr:prepareContext', renderContext)
const getSSRLog = this.useSSRLog()
// Call Vue renderer renderToString
let APP = await (this.options.dev ? this.devRenderToString(renderContext) : this.vueRenderer.renderToString(renderContext))
let APP = await this.vueRenderer.renderToString(renderContext)
if (typeof getSSRLog === 'function') {
renderContext.nuxt.logs = getSSRLog()
}
// Call ssr:context hook
await this.serverContext.nuxt.callHook('vue-renderer:ssr:context', renderContext)