feat(nuxt): environment-specific plugin execution for islands (#20726)

This commit is contained in:
Julien Huang 2023-07-30 23:36:11 +02:00 committed by GitHub
parent ffc4e798cd
commit 0f839dd723
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 2 deletions

View File

@ -155,6 +155,16 @@ export interface PluginMeta {
order?: number
}
export interface PluginEnvContext {
/**
* This enable the plugin for islands components.
* Require `experimental.componentsIslands`.
*
* @default true
*/
islands?: boolean
}
export interface ResolvedPluginMeta {
name?: string
parallel?: boolean
@ -169,6 +179,7 @@ export interface Plugin<Injections extends Record<string, unknown> = Record<stri
export interface ObjectPlugin<Injections extends Record<string, unknown> = Record<string, unknown>> extends PluginMeta {
hooks?: Partial<RuntimeNuxtHooks>
setup?: Plugin<Injections>
env?: PluginEnvContext
/**
* Execute plugin in parallel with other parallel plugins.
*
@ -318,6 +329,7 @@ export async function applyPlugins (nuxtApp: NuxtApp, plugins: Array<Plugin & Ob
const parallels: Promise<any>[] = []
const errors: Error[] = []
for (const plugin of plugins) {
if (process.server && nuxtApp.ssrContext?.islandContext && plugin.env?.islands === false) { continue }
const promise = applyPlugin(nuxtApp, plugin)
if (plugin.parallel) {
parallels.push(promise.catch(e => errors.push(e)))

View File

@ -32,7 +32,7 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM
const serverDir = join(rootDir, '.output/server')
const serverStats = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect.soft(roundToKilobytes(serverStats.totalBytes)).toMatchInlineSnapshot('"64.2k"')
expect.soft(roundToKilobytes(serverStats.totalBytes)).toMatchInlineSnapshot('"64.4k"')
const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect.soft(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2330k"')

View File

@ -8,6 +8,7 @@
<div id="long-async-component-count">
{{ count }}
</div>
{{ headers['custom-head'] }}
<slot name="test" :count="count" />
<p>hello world !!!</p>
<slot v-for="(t, index) in 3" name="hello" :t="t">
@ -28,8 +29,12 @@
</template>
<script setup lang="ts">
import { getResponseHeaders } from 'h3'
defineProps<{
count: number
}>()
const evt = useRequestEvent()
const headers = getResponseHeaders(evt)
const { data } = await useFetch('/api/very-long-request')
</script>

View File

@ -0,0 +1,12 @@
import { setHeader } from 'h3'
export default defineNuxtPlugin({
name: 'server-only-plugin',
setup () {
const evt = useRequestEvent()
setHeader(evt, 'custom-head', 'hello')
},
env: {
islands: false
}
})