fix(generator): minify spa fallback (#5791)

This commit is contained in:
Pim 2019-05-23 11:45:22 +02:00 committed by Pooya Parsa
parent b2a8b9f3f2
commit fd3ee629fa

View File

@ -153,7 +153,14 @@ export default class Generator {
} }
// Render and write the SPA template to the fallback path // Render and write the SPA template to the fallback path
const { html } = await this.nuxt.server.renderRoute('/', { spa: true }) let { html } = await this.nuxt.server.renderRoute('/', { spa: true })
try {
html = this.minifyHtml(html)
} catch (error) {
consola.warn(`HTML minification failed for SPA fallback`)
}
await fsExtra.writeFile(fallbackPath, html, 'utf8') await fsExtra.writeFile(fallbackPath, html, 'utf8')
} }
@ -221,26 +228,14 @@ export default class Generator {
return false return false
} }
let minificationOptions = this.options.build.html.minify
// Legacy: Override minification options with generate.minify if present
// TODO: Remove in Nuxt version 3
if (typeof this.options.generate.minify !== 'undefined') {
minificationOptions = this.options.generate.minify
consola.warn('generate.minify has been deprecated and will be removed in the next major version.' +
' Use build.html.minify instead!')
}
if (minificationOptions) {
try { try {
html = htmlMinifier.minify(html, minificationOptions) html = this.minifyHtml(html)
} catch (err) { } catch (err) {
const minifyErr = new Error( const minifyErr = new Error(
`HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}` `HTML minification failed. Make sure the route generates valid HTML. Failed HTML:\n ${html}`
) )
pageErrors.push({ type: 'unhandled', route, error: minifyErr }) pageErrors.push({ type: 'unhandled', route, error: minifyErr })
} }
}
let fileName let fileName
@ -276,4 +271,22 @@ export default class Generator {
return true return true
} }
minifyHtml(html) {
let minificationOptions = this.options.build.html.minify
// Legacy: Override minification options with generate.minify if present
// TODO: Remove in Nuxt version 3
if (typeof this.options.generate.minify !== 'undefined') {
minificationOptions = this.options.generate.minify
consola.warn('generate.minify has been deprecated and will be removed in the next major version.' +
' Use build.html.minify instead!')
}
if (!minificationOptions) {
return html
}
return htmlMinifier.minify(html, minificationOptions)
}
} }