Skip to content

Commit

Permalink
fix: add v1 RC props back
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Nov 13, 2022
1 parent abe0fb1 commit 39e8dd2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
49 changes: 48 additions & 1 deletion src/createHead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,46 @@ import { createHead as createUnhead, debouncedRenderDOMHead, renderDOMHead } fro
import type { ActiveHeadEntry, Head, HeadEntryOptions, Unhead } from '@unhead/schema'
import type { Plugin } from 'vue'

export type HookBeforeDomUpdate = ((tags: Record<string, HeadTag[]>) => void | boolean)[]
export type HookTagsResolved = ((tags: HeadTag[]) => void)[]

export interface LegacyHeadClient<T> {
headTags: () => Promise<HeadTag[]>

/**
* @deprecated
*/
addEntry: (objs: Head, options?: HeadEntryOptions) => ActiveHeadEntry<Head>
/**
* @deprecated
*/
addReactiveEntry: (objs: MaybeComputedRef<ReactiveHead>, options?: HeadEntryOptions) => ActiveHeadEntry<MaybeComputedRef<ReactiveHead>>
/**
* @deprecated
*/
addHeadObjs: (objs: T, options?: HeadEntryOptions) => ActiveHeadEntry<T>

/**
* @deprecated
*/
updateDOM: (document?: Document, force?: boolean) => void


/**
* Array of user provided functions to hook into before the DOM is updated.
*
* When returning false from this function, it will block DOM updates, this can be useful when stopping dom updates
* between page transitions.
*
* You are able to modify the payload of hook using this.
*
* @deprecated
*/
hookBeforeDomUpdate: HookBeforeDomUpdate
/**
* Array of user provided functions to hook into after the tags have been resolved (deduped and sorted).
* @deprecated
*/
hookTagsResolved: HookTagsResolved
}

export type UseHeadInput<T extends MergeHead> = MaybeComputedRef<ReactiveHead<T>>
Expand All @@ -18,25 +52,38 @@ export type VueUseHead<T extends MergeHead = {}> = Unhead<UseHeadInput<T>> & Leg
export function createHead<T extends MergeHead = {}>(initHeadObject?: Head<T>): VueUseHead<T> {
const head = createUnhead() as VueUseHead<T>

const hookBeforeDomUpdate: HookBeforeDomUpdate = []
const hookTagsResolved: HookTagsResolved = []

// make migration easier
const legacyHead: LegacyHeadClient<ReactiveHead<T>> = {
headTags() {
return head.resolveTags()
},
addEntry(input, options) {
return head.push(input, options)
},
addHeadObjs(input, options) {
return head.push(input, options)
},
addReactiveEntry(input, options) {
return head.push(input, options)
},
updateDOM(document, force) {
if (force)
renderDOMHead(head, { document })
else
debouncedRenderDOMHead(head, { delayFn: fn => setTimeout(() => fn(), 50), document })
},
hookBeforeDomUpdate,
hookTagsResolved,
}

head.headTags = legacyHead.headTags
// @ts-expect-error untyped
head.addHeadObjs = legacyHead.addHeadObjs
head.addEntry = legacyHead.addEntry
head.addReactiveEntry = legacyHead.addReactiveEntry
head.updateDOM = legacyHead.updateDOM

if (initHeadObject)
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './createHead'
export * from './legacy'
export * from './components'
export type { Unhead } from '@unhead/vue'
export type { Unhead, HeadEntryOptions, MaybeComputedRef } from '@unhead/vue'
export { useHead, useServerHead, VueHeadMixin } from '@unhead/vue'

0 comments on commit 39e8dd2

Please sign in to comment.