mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 21:55:11 +00:00
feat(nuxt): support experimental flag to render no client-side js (#7248)
This commit is contained in:
parent
33d9158715
commit
cc87d2a37a
@ -97,6 +97,7 @@ export async function initNitro (nuxt: Nuxt) {
|
|||||||
},
|
},
|
||||||
replace: {
|
replace: {
|
||||||
'process.env.NUXT_NO_SSR': nuxt.options.ssr === false,
|
'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.env.NUXT_INLINE_STYLES': !!nuxt.options.experimental.inlineSSRStyles,
|
||||||
'process.dev': nuxt.options.dev,
|
'process.dev': nuxt.options.dev,
|
||||||
__VUE_PROD_DEVTOOLS__: false
|
__VUE_PROD_DEVTOOLS__: false
|
||||||
|
@ -5,6 +5,8 @@ import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent,
|
|||||||
// Temporary until finding better placement
|
// Temporary until finding better placement
|
||||||
/* eslint-disable import/no-restricted-paths */
|
/* eslint-disable import/no-restricted-paths */
|
||||||
import escapeRE from 'escape-string-regexp'
|
import escapeRE from 'escape-string-regexp'
|
||||||
|
import fse from 'fs-extra'
|
||||||
|
import { withoutLeadingSlash } from 'ufo'
|
||||||
import pagesModule from '../pages/module'
|
import pagesModule from '../pages/module'
|
||||||
import metaModule from '../head/module'
|
import metaModule from '../head/module'
|
||||||
import componentsModule from '../components/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 })
|
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
|
// Transpile layers within node_modules
|
||||||
nuxt.options.build.transpile.push(
|
nuxt.options.build.transpile.push(
|
||||||
...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string)
|
...nuxt.options._layers.filter(i => i.cwd.includes('node_modules')).map(i => i.cwd as string)
|
||||||
|
@ -165,8 +165,8 @@ export default defineRenderHandler(async (event) => {
|
|||||||
_rendered.html
|
_rendered.html
|
||||||
],
|
],
|
||||||
bodyAppend: normalizeChunks([
|
bodyAppend: normalizeChunks([
|
||||||
`<script>window.__NUXT__=${devalue(ssrContext.payload)}</script>`,
|
process.env.NUXT_NO_SCRIPTS ? '' : `<script>window.__NUXT__=${devalue(ssrContext.payload)}</script>`,
|
||||||
_rendered.renderScripts(),
|
process.env.NUXT_NO_SCRIPTS ? '' : _rendered.renderScripts(),
|
||||||
// Note: bodyScripts may contain tags other than <script>
|
// Note: bodyScripts may contain tags other than <script>
|
||||||
renderedMeta.bodyScripts
|
renderedMeta.bodyScripts
|
||||||
])
|
])
|
||||||
|
@ -61,7 +61,7 @@ export default defineUntypedSchema({
|
|||||||
* @type {boolean | ((id?: string) => boolean)}
|
* @type {boolean | ((id?: string) => boolean)}
|
||||||
*/
|
*/
|
||||||
inlineSSRStyles: {
|
inlineSSRStyles: {
|
||||||
$resolve(val, get) {
|
$resolve (val, get) {
|
||||||
if (val === false || get('dev') || get('ssr') === false || get('builder') === '@nuxt/webpack-builder') {
|
if (val === false || get('dev') || get('ssr') === false || get('builder') === '@nuxt/webpack-builder') {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -69,5 +69,10 @@ export default defineUntypedSchema({
|
|||||||
return val ?? true
|
return val ?? true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn off rendering of Nuxt scripts and JS resource hints.
|
||||||
|
*/
|
||||||
|
noScripts: false,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user