This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
vueuse-head.plugin.ts
51 lines (44 loc) · 1.73 KB
/
vueuse-head.plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { createHead, useHead } from '@vueuse/head'
import { defineNuxtPlugin } from '#app'
// @ts-expect-error untyped
import { appHead } from '#build/nuxt.config.mjs'
export default defineNuxtPlugin((nuxtApp) => {
const head = createHead()
head.push(appHead, {
// when SSR we don't need to hydrate this entry
mode: nuxtApp.ssrContext?.noSSR ? 'all' : 'server'
})
nuxtApp.vueApp.use(head)
if (process.client) {
// pause dom updates until page is ready and between page transitions
let pauseDOMUpdates = true
const unpauseDom = () => {
pauseDOMUpdates = false
// triggers dom update
head.internalHooks.callHook('entries:updated', head.unhead)
}
head.internalHooks.hook('dom:beforeRender', (context) => {
context.shouldRender = !pauseDOMUpdates
})
nuxtApp.hooks.hook('page:start', () => { pauseDOMUpdates = true })
// watch for new route before unpausing dom updates (triggered after suspense resolved)
nuxtApp.hooks.hook('page:finish', unpauseDom)
nuxtApp.hooks.hook('app:mounted', unpauseDom)
}
// basic support for users to modify tags before render
head.internalHooks.hook('tags:resolve', ctx => nuxtApp.hooks.callHook('head:tags:resolve', ctx))
// useHead does not depend on a vue component context, we keep it on the nuxtApp for backwards compatibility
nuxtApp._useHead = useHead
if (process.server) {
nuxtApp.ssrContext!.renderMeta = async () => {
const { renderSSRHead } = await import('@unhead/ssr')
const meta = await renderSSRHead(head.unhead)
return {
...meta,
bodyScriptsPrepend: meta.bodyTagsOpen,
// resolves naming difference with NuxtMeta and @vueuse/head
bodyScripts: meta.bodyTags
}
}
}
})