Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import type { MergeHead } from '@zhead/schema'
import {
PROVIDE_KEY,
} from './constants'
import { resolveUnrefHeadInput } from './utils'
import { resolveHeadEntriesToTags, resolveUnrefHeadInput } from './utils'
import type {
HeadEntry,
HeadEntryOptions, HeadObjectApi,
HeadEntryOptions, HeadObjectApi, HeadTag,
HookBeforeDomUpdate, HookEntriesResolved,
HookTagsResolved, ResolvedUseHeadInput, UseHeadInput,
UseHeadRawInput,
} from './types'
import { updateDOM } from './dom/update-dom'
import { resolveHeadEntries } from './ssr'

export * from './types'

Expand All @@ -27,6 +28,15 @@ export interface HeadClient<T extends MergeHead = {}> {

headEntries: HeadEntry<T>[]

/**
* Backwards compatibility function to fetch the headTags.
*
* This function forces reactivity resolving and is not performant.
*
* @deprecated Use hooks.
*/
headTags: HeadTag[]

addEntry: (entry: UseHeadInput<T>, options?: HeadEntryOptions) => HeadObjectApi<T>
addReactiveEntry: (objs: UseHeadInput<T>, options?: HeadEntryOptions) => () => void

Expand Down Expand Up @@ -89,6 +99,15 @@ export const createHead = <T extends MergeHead = {}>(initHeadObject?: ResolvedUs
return entries
},

/**
* Backwards compatibility with < v1.
*/
get headTags() {
// Note: we don't call hooks here as this function is sync
const resolvedEntries = resolveHeadEntries(head.headEntries)
return resolveHeadEntriesToTags(resolvedEntries)
},

addEntry(input, options = {}) {
let resolved = false
if (options?.resolved) {
Expand Down
41 changes: 41 additions & 0 deletions tests/backwards-compatibility/head-tags.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createHead } from '../../src'

describe('head tags', () => {
test('calling them works', async () => {
const head = createHead()
head.addEntry({
title: 'test',
link: [
{
href: '/',
},
],
})

expect(head.headTags).toMatchInlineSnapshot(`
[
{
"children": "test",
"options": {},
"props": {},
"runtime": {
"entryId": 0,
"position": 0,
},
"tag": "title",
},
{
"options": {},
"props": {
"href": "/",
},
"runtime": {
"entryId": 0,
"position": 1,
},
"tag": "link",
},
]
`)
})
})