fix(nuxt): disable x-nuxt-no-ssr header by default (#20024)

This commit is contained in:
Daniel Roe 2023-04-03 11:39:01 +01:00 committed by GitHub
parent 61699f864e
commit c660b39447
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 2 deletions

View File

@ -189,6 +189,15 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
} }
} }
// Add backward-compatible middleware to respect `x-nuxt-no-ssr` header
if (nuxt.options.experimental.respectNoSSRHeader) {
nitroConfig.handlers = nitroConfig.handlers || []
nitroConfig.handlers.push({
handler: resolve(distDir, 'core/runtime/nitro/no-ssr'),
middleware: true
})
}
// Register nuxt protection patterns // Register nuxt protection patterns
nitroConfig.rollupConfig!.plugins = await nitroConfig.rollupConfig!.plugins || [] nitroConfig.rollupConfig!.plugins = await nitroConfig.rollupConfig!.plugins || []
nitroConfig.rollupConfig!.plugins = Array.isArray(nitroConfig.rollupConfig!.plugins) ? nitroConfig.rollupConfig!.plugins : [nitroConfig.rollupConfig!.plugins] nitroConfig.rollupConfig!.plugins = Array.isArray(nitroConfig.rollupConfig!.plugins) ? nitroConfig.rollupConfig!.plugins : [nitroConfig.rollupConfig!.plugins]

View File

@ -0,0 +1,8 @@
import { defineEventHandler, getRequestHeader } from 'h3'
export default defineEventHandler((event) => {
if (getRequestHeader(event, 'x-nuxt-no-ssr')) {
event.context.nuxt = event.context.nuxt || {}
event.context.nuxt.noSSR = true
}
})

View File

@ -213,7 +213,7 @@ export default defineRenderHandler(async (event) => {
runtimeConfig: useRuntimeConfig() as NuxtSSRContext['runtimeConfig'], runtimeConfig: useRuntimeConfig() as NuxtSSRContext['runtimeConfig'],
noSSR: noSSR:
!!(process.env.NUXT_NO_SSR) || !!(process.env.NUXT_NO_SSR) ||
!!(event.node.req.headers['x-nuxt-no-ssr']) || event.context.nuxt?.noSSR ||
routeOptions.ssr === false || routeOptions.ssr === false ||
(process.env.prerender ? PRERENDER_NO_SSR_ROUTES.has(url) : false), (process.env.prerender ? PRERENDER_NO_SSR_ROUTES.has(url) : false),
error: !!ssrError, error: !!ssrError,

View File

@ -158,6 +158,9 @@ export default defineUntypedSchema({
* *
* This can be disabled for most Nuxt sites to reduce the client-side bundle by ~0.5kb. * This can be disabled for most Nuxt sites to reduce the client-side bundle by ~0.5kb.
*/ */
polyfillVueUseHead: true polyfillVueUseHead: true,
/** Allow disabling Nuxt SSR responses by setting the `x-nuxt-no-ssr` header. */
respectNoSSRHeader: false,
} }
}) })

View File

@ -184,6 +184,7 @@ export default defineNuxtConfig({
} }
}, },
experimental: { experimental: {
respectNoSSRHeader: true,
clientFallback: true, clientFallback: true,
restoreState: true, restoreState: true,
inlineSSRStyles: id => !!id && !id.includes('assets.vue'), inlineSSRStyles: id => !!id && !id.includes('assets.vue'),