|
1 | | -import type { CreateClientHeadOptions, HeadTag, ResolvableHead } from '../types' |
2 | | -import { createUnhead } from '../unhead' |
| 1 | +import type { ActiveHeadEntry, CreateClientHeadOptions, HeadEntryOptions, HeadRenderer, HeadTag, ResolvableHead, Unhead } from '../types' |
| 2 | +import { createUnhead, registerPlugin } from '../unhead' |
3 | 3 | import { TagPriorityAliases } from '../utils/const' |
4 | | -import { renderDOMHead } from './renderDOMHead' |
| 4 | +import { createDomRenderer } from './renderDOMHead' |
5 | 5 |
|
6 | 6 | function tagWeight(tag: HeadTag) { |
7 | 7 | return typeof tag.tagPriority === 'number' |
8 | 8 | ? tag.tagPriority |
9 | 9 | : 100 + (TagPriorityAliases[tag.tagPriority as keyof typeof TagPriorityAliases] || 0) |
10 | 10 | } |
11 | 11 |
|
12 | | -export function createHead<T = ResolvableHead>(options: CreateClientHeadOptions = {}) { |
13 | | - const render = options.domOptions?.render || renderDOMHead |
| 12 | +export interface ClientUnhead<T = ResolvableHead> extends Unhead<T, boolean> { |
| 13 | + dirty: boolean |
| 14 | + invalidate: () => void |
| 15 | +} |
| 16 | + |
| 17 | +export function createHead<T = ResolvableHead>(options: CreateClientHeadOptions = {}): ClientUnhead<T> { |
14 | 18 | options.document = options.document || (typeof window !== 'undefined' ? document : undefined) |
| 19 | + const renderer = (options.render || createDomRenderer({ document: options.document })) as HeadRenderer<boolean> |
15 | 20 | const initialPayload = options.document?.head.querySelector('script[id="unhead:payload"]')?.innerHTML || false |
16 | | - // restore initial entry from payload (titleTemplate and templateParams) |
17 | | - return createUnhead<T>({ |
| 21 | + |
| 22 | + const core = createUnhead<T, boolean>(renderer, { |
18 | 23 | ...options, |
19 | 24 | _tagWeight: tagWeight, |
20 | | - plugins: [ |
21 | | - ...(options.plugins || []), |
22 | | - { |
23 | | - key: 'client', |
24 | | - hooks: { |
25 | | - 'entries:updated': (head) => { render(head) }, |
| 25 | + plugins: [], // register on wrapped head instead |
| 26 | + init: [], // push on wrapped head instead |
| 27 | + }) |
| 28 | + |
| 29 | + let dirty = false |
| 30 | + |
| 31 | + const head: ClientUnhead<T> = { |
| 32 | + ...core, |
| 33 | + get dirty() { return dirty }, |
| 34 | + set dirty(v) { dirty = v }, |
| 35 | + render() { |
| 36 | + return renderer(head) |
| 37 | + }, |
| 38 | + invalidate() { |
| 39 | + for (const entry of core.entries.values()) { |
| 40 | + entry._dirty = true |
| 41 | + } |
| 42 | + dirty = true |
| 43 | + core.hooks.callHook('entries:updated', head) |
| 44 | + }, |
| 45 | + push(input: T, _options?: HeadEntryOptions) { |
| 46 | + const active = core.push(input, _options) |
| 47 | + const entry = core.entries.get(active._i)! |
| 48 | + entry._dirty = true |
| 49 | + dirty = true |
| 50 | + core.hooks.callHook('entries:updated', head) |
| 51 | + |
| 52 | + const corePatch = active.patch |
| 53 | + const coreDispose = active.dispose |
| 54 | + |
| 55 | + const clientActive: ActiveHeadEntry<T> = { |
| 56 | + _i: active._i, |
| 57 | + patch(input) { |
| 58 | + corePatch(input) |
| 59 | + entry._dirty = true |
| 60 | + dirty = true |
| 61 | + core.hooks.callHook('entries:updated', head) |
| 62 | + }, |
| 63 | + dispose() { |
| 64 | + if (core.entries.has(active._i)) { |
| 65 | + coreDispose() |
| 66 | + head.invalidate() |
| 67 | + } |
26 | 68 | }, |
27 | | - }, |
28 | | - ], |
29 | | - init: [ |
30 | | - initialPayload ? JSON.parse(initialPayload) : false, |
31 | | - ...(options.init || []), |
32 | | - ], |
| 69 | + } |
| 70 | + return clientActive |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + // register plugins on wrapped head |
| 75 | + ;(options.plugins || []).forEach(p => registerPlugin(head, p)) |
| 76 | + |
| 77 | + // auto-render on entries:updated |
| 78 | + registerPlugin(head, { |
| 79 | + key: 'client', |
| 80 | + hooks: { |
| 81 | + 'entries:updated': () => { head.render() }, |
| 82 | + }, |
33 | 83 | }) |
| 84 | + |
| 85 | + // push init entries |
| 86 | + const initEntries = [ |
| 87 | + initialPayload ? JSON.parse(initialPayload) : false, |
| 88 | + ...(options.init || []), |
| 89 | + ] |
| 90 | + initEntries.forEach(e => e && head.push(e as T)) |
| 91 | + |
| 92 | + return head |
34 | 93 | } |
0 commit comments