|
| 1 | +import type { ActiveHeadEntry, HeadEntryOptions, MergeHead } from '@unhead/schema' |
| 2 | +import type { |
| 3 | + Ref, |
| 4 | +} from 'vue' |
| 5 | +import type { |
| 6 | + ReactiveHead, |
| 7 | + UseHeadInput, |
| 8 | + UseHeadOptions, |
| 9 | + UseHeadSafeInput, |
| 10 | + UseSeoMetaInput, |
| 11 | + VueHeadClient, |
| 12 | +} from './types' |
| 13 | +import { unpackMeta, whitelistSafeInput } from '@unhead/shared' |
| 14 | +import { |
| 15 | + getCurrentInstance, |
| 16 | + hasInjectionContext, |
| 17 | + inject, |
| 18 | + onActivated, |
| 19 | + onBeforeUnmount, |
| 20 | + onDeactivated, |
| 21 | + ref, |
| 22 | + watch, |
| 23 | + watchEffect, |
| 24 | +} from 'vue' |
| 25 | +import { headSymbol } from './install' |
| 26 | +import { resolveUnrefHeadInput } from './utils' |
| 27 | + |
| 28 | +export function injectHead() { |
| 29 | + if (hasInjectionContext()) { |
| 30 | + // fallback to vue context |
| 31 | + const instance = inject<VueHeadClient<MergeHead>>(headSymbol) |
| 32 | + if (!instance) { |
| 33 | + throw new Error('useHead() was called without provide context, ensure you call it through the setup() function.') |
| 34 | + } |
| 35 | + return instance |
| 36 | + } |
| 37 | + throw new Error('useHead() was called without provide context, ensure you call it through the setup() function.') |
| 38 | +} |
| 39 | + |
| 40 | +export function useHead<T extends MergeHead>(input: UseHeadInput<T>, options: UseHeadOptions = {}): ActiveHeadEntry<UseHeadInput<T>> { |
| 41 | + const head = options.head || injectHead() |
| 42 | + return head.ssr ? head.push(input, options as HeadEntryOptions) : clientUseHead(head, input, options as HeadEntryOptions) |
| 43 | +} |
| 44 | + |
| 45 | +function clientUseHead<T extends MergeHead>(head: VueHeadClient<T>, input: UseHeadInput<T>, options: HeadEntryOptions = {}): ActiveHeadEntry<UseHeadInput<T>> { |
| 46 | + const deactivated = ref(false) |
| 47 | + |
| 48 | + const resolvedInput: Ref<ReactiveHead> = ref({}) |
| 49 | + watchEffect(() => { |
| 50 | + resolvedInput.value = deactivated.value |
| 51 | + ? {} |
| 52 | + : resolveUnrefHeadInput(input) |
| 53 | + }) |
| 54 | + const entry: ActiveHeadEntry<UseHeadInput<T>> = head.push(resolvedInput.value, options) |
| 55 | + watch(resolvedInput, (e) => { |
| 56 | + entry.patch(e) |
| 57 | + }) |
| 58 | + |
| 59 | + const vm = getCurrentInstance() |
| 60 | + if (vm) { |
| 61 | + onBeforeUnmount(() => { |
| 62 | + entry.dispose() |
| 63 | + }) |
| 64 | + onDeactivated(() => { |
| 65 | + deactivated.value = true |
| 66 | + }) |
| 67 | + onActivated(() => { |
| 68 | + deactivated.value = false |
| 69 | + }) |
| 70 | + } |
| 71 | + return entry |
| 72 | +} |
| 73 | + |
| 74 | +export function useHeadSafe(input: UseHeadSafeInput, options: UseHeadOptions = {}): ActiveHeadEntry<any> { |
| 75 | + // @ts-expect-error untyped |
| 76 | + return useHead(input, { ...options, transform: whitelistSafeInput }) |
| 77 | +} |
| 78 | + |
| 79 | +export function useSeoMeta(input: UseSeoMetaInput, options?: UseHeadOptions): ActiveHeadEntry<any> { |
| 80 | + const { title, titleTemplate, ...meta } = input |
| 81 | + return useHead({ |
| 82 | + title, |
| 83 | + titleTemplate, |
| 84 | + // @ts-expect-error runtime type |
| 85 | + _flatMeta: meta, |
| 86 | + }, { |
| 87 | + ...options, |
| 88 | + transform(t) { |
| 89 | + // @ts-expect-error runtime type |
| 90 | + const meta = unpackMeta({ ...t._flatMeta }) |
| 91 | + // @ts-expect-error runtime type |
| 92 | + delete t._flatMeta |
| 93 | + return { |
| 94 | + // @ts-expect-error runtime type |
| 95 | + ...t, |
| 96 | + meta, |
| 97 | + } |
| 98 | + }, |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +export function useServerHead<T extends MergeHead>(input: UseHeadInput<T>, options: UseHeadOptions = {}): ActiveHeadEntry<any> { |
| 103 | + return useHead<T>(input, { ...options, mode: 'server' }) |
| 104 | +} |
| 105 | + |
| 106 | +export function useServerHeadSafe(input: UseHeadSafeInput, options: UseHeadOptions = {}): ActiveHeadEntry<any> { |
| 107 | + return useHeadSafe(input, { ...options, mode: 'server' }) |
| 108 | +} |
| 109 | + |
| 110 | +export function useServerSeoMeta(input: UseSeoMetaInput, options?: UseHeadOptions): ActiveHeadEntry<UseSeoMetaInput> { |
| 111 | + return useSeoMeta(input, { ...options, mode: 'server' }) |
| 112 | +} |
0 commit comments