Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nuxt): enable renderSSRHeadOptions configuration for unhead #26989

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6ca5905
feat(nuxt): enable `omitLineBreaks` configuration for `@unhead`
Mini-ghost Apr 29, 2024
602ed0b
docs: update documentation
Mini-ghost Apr 29, 2024
93e9559
Merge branch 'main' into feature/support-configure-omitLineBreaks
Mini-ghost Apr 30, 2024
a8e9a67
feat(nuxt): enable `renderSSRHeadOptions` configuration for `unhead`
Mini-ghost May 1, 2024
d4e652d
Revert "docs: update documentation"
Mini-ghost May 1, 2024
36c1b73
Revert "feat(nuxt): enable `omitLineBreaks` configuration for `@unhead`"
Mini-ghost May 1, 2024
d60db7e
feat(nuxt): use `defu` to merge default options
Mini-ghost May 2, 2024
e81089f
refactor(nuxt): use `unhead` as key instead of `meta`
Mini-ghost May 3, 2024
026469c
docs: add more detail to the docs
Mini-ghost May 3, 2024
a8ef010
refactor(nuxt): use `defaults` function pre-merged `options`
Mini-ghost May 3, 2024
3232872
Merge branch 'main' into feature/support-configure-omitLineBreaks
danielroe May 3, 2024
14863f5
feat(nuxt): set default value of `omitLineBreaks` to false
Mini-ghost May 6, 2024
8170dd3
fix: set `omitLineBreaks` to true by default in v4 compatibility mode
danielroe May 7, 2024
75583e3
fix(nuxt): treat schema as source of truth
danielroe May 7, 2024
99664a9
Merge remote-tracking branch 'origin/main' into feature/support-confi…
danielroe May 7, 2024
096137f
docs: add defineNuxtConfig wrapper
danielroe May 7, 2024
48fffa3
chore: remove config key
danielroe May 7, 2024
a6bcd49
chore: provide backwards-compatible default
danielroe May 7, 2024
4550d21
docs: add explanation of how to disable
danielroe May 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/2.guide/3.going-further/1.features.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export default defineNuxtConfig({
deep: true
}
}
},
unhead: {
renderSSRHeadOptions: {
omitLineBreaks: false
}
}
})
```
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down
13 changes: 12 additions & 1 deletion packages/nuxt/src/head/module.ts
Original file line number Diff line number Diff line change
@@ -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',
},
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions packages/schema/src/config/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
},
},
},
})
5 changes: 5 additions & 0 deletions packages/schema/src/config/experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export default defineUntypedSchema({
* deep: true
* }
* }
* },
* unhead: {
* renderSSRHeadOptions: {
* omitLineBreaks: false
* }
* }
* })
* ```
Expand Down