fix(nuxt3): re-enable computed metadata in useHead (#4260)

This commit is contained in:
Daniel Roe 2022-04-11 15:09:04 +01:00 committed by GitHub
parent b142666561
commit 38cd6f0f03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,22 +17,24 @@ export default defineNuxtPlugin((nuxtApp) => {
const titleTemplate = ref<MetaObject['titleTemplate']>() const titleTemplate = ref<MetaObject['titleTemplate']>()
nuxtApp._useHead = (meta: MetaObject | ComputedGetter<MetaObject>) => { nuxtApp._useHead = (_meta: MetaObject | ComputedGetter<MetaObject>) => {
titleTemplate.value = (unref(meta) as MetaObject).titleTemplate || titleTemplate.value const meta = ref<MetaObject>(_meta)
if ('titleTemplate' in meta.value) {
titleTemplate.value = meta.value.titleTemplate
}
const headObj = computed(() => { const headObj = computed(() => {
const overrides: MetaObject = { meta: [] } const overrides: MetaObject = { meta: [] }
const val = unref(meta) as MetaObject if (titleTemplate.value && 'title' in meta.value) {
if (titleTemplate.value && 'title' in val) { overrides.title = typeof titleTemplate.value === 'function' ? titleTemplate.value(meta.value.title) : titleTemplate.value.replace(/%s/g, meta.value.title)
overrides.title = typeof titleTemplate.value === 'function' ? titleTemplate.value(val.title) : titleTemplate.value.replace(/%s/g, val.title)
} }
if (val.charset) { if (meta.value.charset) {
overrides.meta!.push({ key: 'charset', charset: val.charset }) overrides.meta!.push({ key: 'charset', charset: meta.value.charset })
} }
if (val.viewport) { if (meta.value.viewport) {
overrides.meta!.push({ name: 'viewport', content: val.viewport }) overrides.meta!.push({ name: 'viewport', content: meta.value.viewport })
} }
return defu(overrides, val) return defu(overrides, meta.value)
}) })
head.addHeadObjs(headObj as any) head.addHeadObjs(headObj as any)