Skip to content

Commit e30346e

Browse files
committedFeb 20, 2025·
fix(schemaOrg): broken entry patching types
1 parent 838708c commit e30346e

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed
 

‎packages/schema-org/src/runtime.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { HeadEntryOptions, Unhead } from 'unhead/types'
1+
import type {ActiveHeadEntry, HeadEntryOptions, Unhead} from 'unhead/types'
22
import type {
33
AggregateOffer,
44
AggregateRating,
@@ -37,7 +37,6 @@ import type {
3737
WebSite,
3838
} from './nodes'
3939
import type { Arrayable, Thing } from './types'
40-
import { useHead } from 'unhead'
4140
import { UnheadSchemaOrg } from './plugin'
4241

4342
function provideResolver<T>(input?: T, resolver?: string) {
@@ -160,19 +159,22 @@ export function defineBookEdition<T extends Record<string, any>>(input?: BookEdi
160159

161160
export type UseSchemaOrgInput = Arrayable<Thing | Record<string, any>>
162161

163-
export function useSchemaOrg(head: Unhead<any>, input: UseSchemaOrgInput, options?: HeadEntryOptions) {
164-
// lazy initialise the plugin
165-
if ((Array.isArray(input) && input.length === 0) || !input) {
166-
return
167-
}
168-
head.use(UnheadSchemaOrg())
169-
return useHead(head, {
162+
export function normalizeSchemaOrgInput(input: UseSchemaOrgInput) {
163+
return {
170164
script: [
171165
{
172166
type: 'application/ld+json',
173167
key: 'schema-org-graph',
174168
nodes: input,
175169
},
176170
],
177-
}, options)
171+
}
172+
}
173+
174+
export function useSchemaOrg(unhead: Unhead<any>, input: UseSchemaOrgInput = [], options: HeadEntryOptions = {}): ActiveHeadEntry<UseSchemaOrgInput> {
175+
unhead.use(UnheadSchemaOrg())
176+
const entry = unhead.push(normalizeSchemaOrgInput(input), options)
177+
const corePatch = entry.patch
178+
entry.patch = input => corePatch(normalizeSchemaOrgInput(input))
179+
return entry
178180
}

‎packages/schema-org/src/vue/runtime/composables.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ResolvableProperties, UseHeadOptions } from '@unhead/vue'
2+
import type { ActiveHeadEntry } from 'unhead/types'
23
import type {
34
AggregateOffer,
45
AggregateRating,
@@ -39,6 +40,8 @@ import type {
3940
} from '../../'
4041
import type { Arrayable } from '../../types'
4142
import { injectHead, useHead } from '@unhead/vue'
43+
import { normalizeSchemaOrgInput,
44+
} from '../../'
4245
import { UnheadSchemaOrg } from '../../plugin'
4346

4447
function provideResolver<T>(input?: T, resolver?: string) {
@@ -158,19 +161,12 @@ export function defineBookEdition<T extends Record<string, any>>(input?: Resolva
158161

159162
export type UseSchemaOrgInput = Arrayable<ResolvableProperties<Thing | Record<string, any>>>
160163

161-
export function useSchemaOrg(input: UseSchemaOrgInput, options?: UseHeadOptions) {
164+
export function useSchemaOrg(input: UseSchemaOrgInput = [], options: UseHeadOptions = {}): ActiveHeadEntry<UseSchemaOrgInput> {
162165
// lazy initialise the plugin
163-
const head = options?.head || injectHead()
164-
head.use(UnheadSchemaOrg())
165-
return useHead<{ script: { nodes: UseSchemaOrgInput } }>({
166-
script: [
167-
{
168-
type: 'application/ld+json',
169-
key: 'schema-org-graph',
170-
nodes: input,
171-
},
172-
],
173-
}, {
174-
head,
175-
})
166+
const unhead = options.head || injectHead()
167+
unhead.use(UnheadSchemaOrg())
168+
const entry = useHead(normalizeSchemaOrgInput(input), options) as ActiveHeadEntry<UseSchemaOrgInput>
169+
const corePatch = entry.patch
170+
entry.patch = input => corePatch(normalizeSchemaOrgInput(input))
171+
return entry
176172
}

‎packages/schema-org/test/e2e/basic.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,9 @@ describe('schema.org e2e', () => {
314314
useSchemaOrg(ssrHead, ['test'])
315315
useSchemaOrg(ssrHead, [])
316316
// @ts-expect-error intentional
317-
useSchemaOrg('')
317+
useSchemaOrg(ssrHead, '')
318318
// @ts-expect-error intentional
319-
useSchemaOrg('test')
319+
useSchemaOrg(ssrHead, 'test')
320320

321321
const data = await renderSSRHead(ssrHead)
322322
expect(data.bodyTags).toMatchInlineSnapshot(`""`)

‎packages/schema-org/test/vue/vue.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,33 @@ describe('schema.org e2e', () => {
121121
</body></html>"
122122
`)
123123
})
124+
it('empty', async () => {
125+
const head = await ssrVueAppWithUnhead(() => {
126+
const schema = useSchemaOrg()
127+
schema.patch([
128+
defineWebPage({
129+
name: 'test',
130+
}),
131+
])
132+
})
133+
const data = await renderSSRHead(head)
134+
expect(data).toMatchInlineSnapshot(`
135+
{
136+
"bodyAttrs": "",
137+
"bodyTags": "<script type="application/ld+json" data-hid="schema-org-graph">{
138+
"@context": "https://schema.org",
139+
"@graph": [
140+
{
141+
"@id": "#webpage",
142+
"@type": "WebPage",
143+
"name": "test"
144+
}
145+
]
146+
}</script>",
147+
"bodyTagsOpen": "",
148+
"headTags": "",
149+
"htmlAttrs": "",
150+
}
151+
`)
152+
})
124153
})

0 commit comments

Comments
 (0)
Please sign in to comment.