feat(nuxt): support experimental flag to render no client-side js (#7248)

This commit is contained in:
Daniel Roe 2022-09-05 14:46:47 +01:00 committed by GitHub
parent 33d9158715
commit cc87d2a37a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 3 deletions

View File

@ -97,6 +97,7 @@ export async function initNitro (nuxt: Nuxt) {
},
replace: {
'process.env.NUXT_NO_SSR': nuxt.options.ssr === false,
'process.env.NUXT_NO_SCRIPTS': !!nuxt.options.experimental.noScripts,
'process.env.NUXT_INLINE_STYLES': !!nuxt.options.experimental.inlineSSRStyles,
'process.dev': nuxt.options.dev,
__VUE_PROD_DEVTOOLS__: false

View File

@ -5,6 +5,8 @@ import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent,
// Temporary until finding better placement
/* eslint-disable import/no-restricted-paths */
import escapeRE from 'escape-string-regexp'
import fse from 'fs-extra'
import { withoutLeadingSlash } from 'ufo'
import pagesModule from '../pages/module'
import metaModule from '../head/module'
import componentsModule from '../components/module'
@ -80,6 +82,18 @@ async function initNuxt (nuxt: Nuxt) {
addWebpackPlugin(TreeShakePlugin.webpack({ sourcemap: nuxt.options.sourcemap, treeShake: removeFromClient }), { server: false })
}
// TODO: [Experimental] Avoid emitting assets when flag is enabled
if (nuxt.options.experimental.noScripts) {
nuxt.hook('build:manifest', async (manifest) => {
for (const file in manifest) {
if (manifest[file].resourceType === 'script') {
await fse.rm(resolve(nuxt.options.buildDir, 'dist/client', withoutLeadingSlash(nuxt.options.app.buildAssetsDir), manifest[file].file), { force: true })
manifest[file].file = ''
}
}
})
}
// Transpile layers within node_modules
nuxt.options.build.transpile.push(
...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string)

View File

@ -165,8 +165,8 @@ export default defineRenderHandler(async (event) => {
_rendered.html
],
bodyAppend: normalizeChunks([
`<script>window.__NUXT__=${devalue(ssrContext.payload)}</script>`,
_rendered.renderScripts(),
process.env.NUXT_NO_SCRIPTS ? '' : `<script>window.__NUXT__=${devalue(ssrContext.payload)}</script>`,
process.env.NUXT_NO_SCRIPTS ? '' : _rendered.renderScripts(),
// Note: bodyScripts may contain tags other than <script>
renderedMeta.bodyScripts
])

View File

@ -61,7 +61,7 @@ export default defineUntypedSchema({
* @type {boolean | ((id?: string) => boolean)}
*/
inlineSSRStyles: {
$resolve(val, get) {
$resolve (val, get) {
if (val === false || get('dev') || get('ssr') === false || get('builder') === '@nuxt/webpack-builder') {
return false
}
@ -69,5 +69,10 @@ export default defineUntypedSchema({
return val ?? true
}
},
/**
* Turn off rendering of Nuxt scripts and JS resource hints.
*/
noScripts: false,
}
})