feat: allow disabling payload extraction (#7588)

This commit is contained in:
pooya parsa 2022-09-16 12:14:41 +02:00 committed by GitHub
parent f0ad1b02d9
commit 5605f1ce18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 6 deletions

View File

@ -101,6 +101,7 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
'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_PAYLOAD_EXTRACTION': !!nuxt.options.experimental.payloadExtraction,
'process.dev': nuxt.options.dev,
__VUE_PROD_DEVTOOLS__: false
},

View File

@ -174,7 +174,7 @@ async function initNuxt (nuxt: Nuxt) {
})
// Add prerender payload support
if (!nuxt.options.dev) {
if (!nuxt.options.dev && nuxt.options.experimental.payloadExtraction) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/payload.client'))
}

View File

@ -103,7 +103,7 @@ const getSPARenderer = lazyCachedFunction(async () => {
return { renderToString }
})
const PAYLOAD_CACHE = process.env.prerender ? new Map() : null // TODO: Use LRU cache
const PAYLOAD_CACHE = (process.env.NUXT_PAYLOAD_EXTRACTION && process.env.prerender) ? new Map() : null // TODO: Use LRU cache
const PAYLOAD_URL_RE = /\/_payload(\.[a-zA-Z0-9]+)?.js(\?.*)?$/
const PRERENDER_NO_SSR_ROUTES = new Set(['/index.html', '/200.html', '/404.html'])
@ -142,7 +142,8 @@ export default defineRenderHandler(async (event) => {
}
// Whether we are prerendering route
const payloadURL = (process.env.prerender && !ssrContext.noSSR) ? joinURL(url, '_payload.js') : undefined
const _PAYLOAD_EXTRACTION = process.env.prerender && process.env.NUXT_PAYLOAD_EXTRACTION && !ssrContext.noSSR
const payloadURL = _PAYLOAD_EXTRACTION ? joinURL(url, '_payload.js') : undefined
if (process.env.prerender) {
ssrContext.payload.prerenderedAt = Date.now()
}
@ -174,7 +175,7 @@ export default defineRenderHandler(async (event) => {
return response
}
if (process.env.prerender) {
if (_PAYLOAD_EXTRACTION) {
// Hint nitro to prerender payload for this route
appendHeader(event, 'x-nitro-prerender', payloadURL!)
// Use same ssr context to generate payload for this route
@ -194,7 +195,7 @@ export default defineRenderHandler(async (event) => {
htmlAttrs: normalizeChunks([renderedMeta.htmlAttrs]),
head: normalizeChunks([
renderedMeta.headTags,
(process.env.prerender && !ssrContext.noSSR) ? `<link rel="modulepreload" href="${payloadURL}">` : null,
_PAYLOAD_EXTRACTION ? `<link rel="modulepreload" href="${payloadURL}">` : null,
_rendered.renderResourceHints(),
_rendered.renderStyles(),
inlinedStyles,
@ -212,7 +213,7 @@ export default defineRenderHandler(async (event) => {
bodyAppend: normalizeChunks([
process.env.NUXT_NO_SCRIPTS
? undefined
: ((process.env.prerender && !ssrContext.noSSR)
: (_PAYLOAD_EXTRACTION
? `<script type="module">import p from "${payloadURL}";window.__NUXT__={...p,...(${devalue(splitPayload(ssrContext).initial)})}</script>`
: `<script>window.__NUXT__=${devalue(ssrContext.payload)}</script>`
),

View File

@ -74,5 +74,10 @@ export default defineUntypedSchema({
* Turn off rendering of Nuxt scripts and JS resource hints.
*/
noScripts: false,
/**
* When this option is enabled (by default) payload of pages generated with `nuxt generate` are extracted
*/
payloadExtraction: true,
}
})