mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
feat(nuxt): custom renderSSRHeadOptions
config for unhead
(#26989)
This commit is contained in:
parent
8fcf893f8b
commit
9e8261a69c
@ -69,6 +69,11 @@ export default defineNuxtConfig({
|
||||
deep: true
|
||||
}
|
||||
}
|
||||
},
|
||||
unhead: {
|
||||
renderSSRHeadOptions: {
|
||||
omitLineBreaks: false
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
@ -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 = {
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
})
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
@ -34,6 +34,11 @@ export default defineUntypedSchema({
|
||||
* deep: true
|
||||
* }
|
||||
* }
|
||||
* },
|
||||
* unhead: {
|
||||
* renderSSRHeadOptions: {
|
||||
* omitLineBreaks: false
|
||||
* }
|
||||
* }
|
||||
* })
|
||||
* ```
|
||||
|
Loading…
Reference in New Issue
Block a user