Skip to content

Commit e68e662

Browse files
committed
fix(schema-org): avoid crashing from 3+ duplicate nodes
Fixes #669
1 parent 3478a99 commit e68e662

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

packages/schema-org/src/plugin.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ export function UnheadSchemaOrg(config: MetaInput = {} as MetaInput, meta: () =>
128128
const toRemove = new Set<number>()
129129
for (let i = 0; i < ctx.tags.length; i++) {
130130
const tag = ctx.tags[i]
131+
if (!tag)
132+
continue
131133
if ((tag.props.type === 'application/ld+json' && tag.props.nodes) || tag.key === 'schema-org-graph') {
132134
delete tag.props.nodes
133135
if (typeof firstNodeIdx === 'undefined') {

packages/schema-org/test/ssr/dupes.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,53 @@ describe('schema.org dupes', () => {
5252
}</script>"
5353
`)
5454
})
55+
56+
it('handles three or more duplicate schema-org entries without crashing', async () => {
57+
const ssrHead = createHead()
58+
59+
ssrHead.use(UnheadSchemaOrg())
60+
61+
useHead(ssrHead, {
62+
script: [
63+
{
64+
type: 'application/ld+json',
65+
key: 'schema-org-graph',
66+
// @ts-expect-error untyped
67+
nodes: [
68+
defineWebSite({
69+
url: '/',
70+
inLanguage: 'en',
71+
name: 'first',
72+
}),
73+
],
74+
},
75+
{
76+
type: 'application/ld+json',
77+
key: 'schema-org-graph',
78+
// @ts-expect-error untyped
79+
nodes: [
80+
// @ts-expect-error broken
81+
defineWebSite({
82+
'@type': 'AboutPage',
83+
}),
84+
],
85+
},
86+
{
87+
type: 'application/ld+json',
88+
key: 'schema-org-graph',
89+
// @ts-expect-error untyped
90+
nodes: [
91+
defineWebSite({
92+
name: 'third',
93+
}),
94+
],
95+
},
96+
],
97+
})
98+
99+
const data = await renderSSRHead(ssrHead)
100+
// should merge all three into a single script tag without throwing
101+
expect(data.bodyTags).toContain('"@context": "https://schema.org"')
102+
expect(data.bodyTags.match(/application\/ld\+json/g)?.length).toBe(1)
103+
})
55104
})

0 commit comments

Comments
 (0)