feat(nuxt): custom renderSSRHeadOptions config for unhead (#26989)

This commit is contained in:
Alex Liu 2024-05-07 20:37:02 +08:00 committed by GitHub
parent 8fcf893f8b
commit 9e8261a69c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 57 additions and 2 deletions

View File

@ -69,6 +69,11 @@ export default defineNuxtConfig({
deep: true
}
}
},
unhead: {
renderSSRHeadOptions: {
omitLineBreaks: false
}
}
})
```

View File

@ -26,6 +26,8 @@ import { useNitroApp } from '#internal/nitro/app'
// @ts-expect-error virtual file
import unheadPlugins from '#internal/unhead-plugins.mjs'
// @ts-expect-error virtual file
import { renderSSRHeadOptions } from '#internal/unhead.config.mjs'
import type { NuxtPayload, NuxtSSRContext } from '#app'
// @ts-expect-error virtual file
@ -459,7 +461,7 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
}
// remove certain tags for nuxt islands
const { headTags, bodyTags, bodyTagsOpen, htmlAttrs, bodyAttrs } = await renderSSRHead(head)
const { headTags, bodyTags, bodyTagsOpen, htmlAttrs, bodyAttrs } = await renderSSRHead(head, renderSSRHeadOptions)
// Create render context
const htmlContext: NuxtRenderHTMLContext = {

View File

@ -1,10 +1,11 @@
import { resolve } from 'pathe'
import { addComponent, addImportsSources, addPlugin, addTemplate, defineNuxtModule, tryResolveModule } from '@nuxt/kit'
import type { NuxtOptions } from '@nuxt/schema'
import { distDir } from '../dirs'
const components = ['NoScript', 'Link', 'Base', 'Title', 'Meta', 'Style', 'Head', 'Html', 'Body']
export default defineNuxtModule({
export default defineNuxtModule<NuxtOptions['unhead']>({
meta: {
name: 'meta',
},
@ -68,9 +69,19 @@ export default import.meta.server ? [CapoPlugin({ track: true })] : [];`
},
})
addTemplate({
filename: 'unhead.config.mjs',
getContents () {
return [
`export const renderSSRHeadOptions = ${JSON.stringify(options.renderSSRHeadOptions || {})}`,
].join('\n')
},
})
// template is only exposed in nuxt context, expose in nitro context as well
nuxt.hooks.hook('nitro:config', (config) => {
config.virtual!['#internal/unhead-plugins.mjs'] = () => nuxt.vfs['#build/unhead-plugins']
config.virtual!['#internal/unhead.config.mjs'] = () => nuxt.vfs['#build/unhead.config']
})
// Add library-specific plugin

View File

@ -346,4 +346,36 @@ export default defineUntypedSchema({
css: {
$resolve: (val: string[] | undefined) => (val ?? []).map((c: any) => c.src || c),
},
/**
* An object that allows us to configure the `unhead` nuxt module.
*/
unhead: {
/**
* An object that will be passed to `renderSSRHead` to customize the output.
*
* @see https://unhead.unjs.io/setup/ssr/installation#options
* @type {typeof import('@unhead/schema').RenderSSRHeadOptions}
*
* @example
* ```ts
* export default defineNuxtConfig({
* unhead: {
* renderSSRHeadOptions: {
* omitLineBreaks: true
* }
* })
* ```
*
*/
renderSSRHeadOptions: {
$resolve: async (val: Record<string, unknown> | undefined, get) => {
const isV4 = ((await get('future') as Record<string, unknown>).compatibilityVersion === 4)
return defu(val, {
omitLineBreaks: isV4,
})
},
},
},
})

View File

@ -34,6 +34,11 @@ export default defineUntypedSchema({
* deep: true
* }
* }
* },
* unhead: {
* renderSSRHeadOptions: {
* omitLineBreaks: false
* }
* }
* })
* ```