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' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: 'Meta description' } { hid: 'description', name: 'description', content: 'Meta description' }
] ]
},
render: {
ssrLog: 'collapsed'
} }
} }

View File

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

View File

@ -299,6 +299,10 @@ export function getNuxtConfig(_options) {
options.pageTransition.appear = true 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.) // We assume the SPA fallback path is 404.html (for GitHub Pages, Surge, etc.)
if (options.generate.fallback === true) { if (options.generate.fallback === true) {
options.generate.fallback = '404.html' options.generate.fallback = '404.html'

View File

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

View File

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

View File

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

View File

@ -37,10 +37,10 @@ const NUXT = window.<%= globals.context %> || {}
Object.assign(Vue.config, <%= serialize(vue.config) %>)<%= isTest ? '// eslint-disable-line' : '' %> Object.assign(Vue.config, <%= serialize(vue.config) %>)<%= isTest ? '// eslint-disable-line' : '' %>
<% if (isDev) { %> <% if (nuxtOptions.render.ssrLog) { %>
const logs = NUXT.logs || [] const logs = NUXT.logs || []
if (logs.length > 0) { 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') const logger = consola.withScope('nuxt:ssr')
logs.forEach(logObj => logger[logObj.type](logObj)) logs.forEach(logObj => logger[logObj.type](logObj))
delete NUXT.logs 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 logs = []
const devReporter = { const devReporter = {
log(logObj) { log(logObj) {
@ -50,19 +53,25 @@ export default class SSRRenderer extends BaseRenderer {
} }
} }
consola.addReporter(devReporter) 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) { async render(renderContext) {
// Call ssr:context hook to extend context from modules // Call ssr:context hook to extend context from modules
await this.serverContext.nuxt.callHook('vue-renderer:ssr:prepareContext', renderContext) await this.serverContext.nuxt.callHook('vue-renderer:ssr:prepareContext', renderContext)
const getSSRLog = this.useSSRLog()
// Call Vue renderer renderToString // 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 // Call ssr:context hook
await this.serverContext.nuxt.callHook('vue-renderer:ssr:context', renderContext) await this.serverContext.nuxt.callHook('vue-renderer:ssr:context', renderContext)