fix(nuxt3): trigger update dom when invoking usemeta (#650)

This commit is contained in:
Daniel Roe 2021-10-05 17:29:06 +02:00 committed by GitHub
parent 1aa44209b4
commit 3fa75ba8a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,5 @@
import { createHead, renderHeadToString } from '@vueuse/head'
import { ref } from 'vue'
import { ref, watchEffect, onBeforeUnmount } from 'vue'
import type { MetaObject } from '..'
import { defineNuxtPlugin } from '#app'
@ -8,7 +8,21 @@ export default defineNuxtPlugin((nuxt) => {
nuxt.app.use(head)
nuxt._useMeta = (meta: MetaObject) => head.addHeadObjs(ref(meta as any))
nuxt._useMeta = (meta: MetaObject) => {
const headObj = ref(meta as any)
head.addHeadObjs(headObj)
if (process.server) { return }
watchEffect(() => {
head.updateDOM()
})
onBeforeUnmount(() => {
head.removeHeadObjs(headObj)
head.updateDOM()
})
}
if (process.server) {
nuxt.ssrContext.renderMeta = () => renderHeadToString(head)