|
1 |
| -import type { ActiveHeadEntry, HeadEntryOptions, HeadSafe, MergeHead } from 'unhead/types' |
2 |
| -import type { UseHeadInput, UseSeoMetaInput } from './types' |
3 |
| -import { useContext, useEffect } from 'react' |
4 |
| -import { FlatMetaPlugin, SafeInputPlugin } from 'unhead/plugins' |
| 1 | +import type { |
| 2 | + ActiveHeadEntry, |
| 3 | + HeadEntryOptions, |
| 4 | + HeadSafe, |
| 5 | + Unhead, |
| 6 | + UseHeadInput, |
| 7 | + UseScriptInput, |
| 8 | + UseScriptOptions, |
| 9 | + UseScriptReturn, |
| 10 | + UseSeoMetaInput, |
| 11 | +} from 'unhead/types' |
| 12 | +import { useContext, useEffect, useState } from 'react' |
| 13 | +import { useHead as baseHead, useHeadSafe as baseHeadSafe, useSeoMeta as baseSeoMeta, useScript as baseUseScript } from 'unhead' |
5 | 14 | import { UnheadContext } from './context'
|
6 | 15 |
|
7 |
| -export function useUnhead() { |
| 16 | +export function useUnhead(): Unhead { |
8 | 17 | // fallback to react context
|
9 |
| - const instance = useContext(UnheadContext) |
| 18 | + const instance = useContext<Unhead | null>(UnheadContext) |
10 | 19 | if (!instance) {
|
11 | 20 | throw new Error('useHead() was called without provide context.')
|
12 | 21 | }
|
13 | 22 | return instance
|
14 | 23 | }
|
15 | 24 |
|
16 |
| -export function useHead<T extends MergeHead>(input: UseHeadInput<T>, options: HeadEntryOptions = {}): ActiveHeadEntry<UseHeadInput<T>> { |
17 |
| - const head = options.head || useUnhead() |
18 |
| - // @ts-expect-error untyped |
19 |
| - const entry = head.push(input, options) |
20 |
| - |
| 25 | +function withSideEffects<T extends ActiveHeadEntry<any>>(input: any, options: any, fn: any): T { |
| 26 | + const unhead = options.head || useUnhead() |
| 27 | + const [entry] = useState<T>(() => fn(unhead, input, options)) |
21 | 28 | useEffect(() => {
|
22 |
| - // @ts-expect-error untyped |
23 | 29 | entry.patch(input)
|
24 | 30 | }, [input])
|
25 |
| - |
26 | 31 | useEffect(() => {
|
27 | 32 | return () => {
|
28 |
| - entry?.dispose() |
| 33 | + // unmount |
| 34 | + entry.dispose() |
29 | 35 | }
|
30 | 36 | }, [])
|
31 |
| - |
32 |
| - // @ts-expect-error untyped |
33 | 37 | return entry
|
34 | 38 | }
|
35 | 39 |
|
36 |
| -export function useHeadSafe(input: HeadSafe, options: HeadEntryOptions = {}): ActiveHeadEntry<HeadSafe> { |
37 |
| - const head = options.head || useUnhead() |
38 |
| - head.use(SafeInputPlugin) |
39 |
| - options._safe = true |
40 |
| - // @ts-expect-error untyped |
41 |
| - return useHead(input, options) |
| 40 | +export function useHead(input: UseHeadInput = {}, options: HeadEntryOptions = {}): ActiveHeadEntry<UseHeadInput> { |
| 41 | + return withSideEffects(input, options, baseHead) |
42 | 42 | }
|
43 | 43 |
|
44 |
| -export function useSeoMeta(input: UseSeoMetaInput, options: HeadEntryOptions = {}): ActiveHeadEntry<any> { |
45 |
| - const head = options.head || useUnhead() |
46 |
| - head.use(FlatMetaPlugin) |
47 |
| - const { title, titleTemplate, ...meta } = input |
48 |
| - return useHead({ |
49 |
| - title, |
50 |
| - titleTemplate, |
51 |
| - _flatMeta: meta, |
52 |
| - }, options) |
| 44 | +export function useHeadSafe(input: HeadSafe = {}, options: HeadEntryOptions = {}): ActiveHeadEntry<HeadSafe> { |
| 45 | + return withSideEffects<ActiveHeadEntry<HeadSafe>>(input, options, baseHeadSafe) |
| 46 | +} |
| 47 | + |
| 48 | +export function useSeoMeta(input: UseSeoMetaInput = {}, options: HeadEntryOptions = {}): ActiveHeadEntry<UseSeoMetaInput> { |
| 49 | + return withSideEffects<ActiveHeadEntry<UseSeoMetaInput>>(input, options, baseSeoMeta) |
| 50 | +} |
| 51 | + |
| 52 | +export function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptReturn<T> { |
| 53 | + const input = (typeof _input === 'string' ? { src: _input } : _input) as UseScriptInput |
| 54 | + const options = _options || {} as UseScriptOptions<T> |
| 55 | + const head = options?.head || useUnhead() |
| 56 | + options.head = head |
| 57 | + |
| 58 | + const mountCbs: (() => void)[] = [] |
| 59 | + let isMounted = false |
| 60 | + useEffect(() => { |
| 61 | + isMounted = true |
| 62 | + mountCbs.forEach(i => i()) |
| 63 | + return () => { |
| 64 | + isMounted = false |
| 65 | + } |
| 66 | + }, []) |
| 67 | + |
| 68 | + if (typeof options.trigger === 'undefined') { |
| 69 | + options.trigger = (load) => { |
| 70 | + if (isMounted) { |
| 71 | + load() |
| 72 | + } |
| 73 | + else { |
| 74 | + mountCbs.push(load) |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + // @ts-expect-error untyped |
| 79 | + const script = baseUseScript(head, input as BaseUseScriptInput, options) |
| 80 | + // Note: we don't remove scripts on unmount as it's not a common use case and reloading the script may be expensive |
| 81 | + const sideEffects: (() => void)[] = [] |
| 82 | + useEffect(() => { |
| 83 | + return () => { |
| 84 | + script._triggerAbortController?.abort() |
| 85 | + sideEffects.forEach(i => i()) |
| 86 | + } |
| 87 | + }, []) |
| 88 | + const _registerCb = (key: 'loaded' | 'error', cb: any) => { |
| 89 | + let i: number | null |
| 90 | + const destroy = () => { |
| 91 | + // avoid removing the wrong callback |
| 92 | + if (i) { |
| 93 | + script._cbs[key]?.splice(i - 1, 1) |
| 94 | + i = null |
| 95 | + } |
| 96 | + } |
| 97 | + mountCbs.push(() => { |
| 98 | + if (!script._cbs[key]) { |
| 99 | + cb(script.instance) |
| 100 | + return () => {} |
| 101 | + } |
| 102 | + i = script._cbs[key].push(cb) |
| 103 | + sideEffects.push(destroy) |
| 104 | + return destroy |
| 105 | + }) |
| 106 | + } |
| 107 | + // if we have a scope we should make these callbacks reactive |
| 108 | + script.onLoaded = (cb: (instance: T) => void | Promise<void>) => _registerCb('loaded', cb) |
| 109 | + script.onError = (cb: (err?: Error) => void | Promise<void>) => _registerCb('error', cb) |
| 110 | + return script |
53 | 111 | }
|
0 commit comments