Skip to content

Commit 22ac9ad

Browse files
harlan-zwclaude
andcommitted
fix(schema-org): normalize target to array before merging potentialAction (#709)
The `merge()` function crashed with `TypeError: byType[type].target is not iterable` during SPA navigation when `potentialAction.target` was an object or string instead of an array. Normalize to array before spreading. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 9d2e1f4 commit 22ac9ad

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

packages/schema-org/src/core/util.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ export function merge(target: any, source: any): any {
3535
const type = action['@type']
3636
if (byType[type]) {
3737
// merge target arrays
38-
if (action.target && byType[type].target)
39-
byType[type].target = [...new Set([...byType[type].target, ...action.target])]
38+
if (action.target && byType[type].target) {
39+
const a = Array.isArray(byType[type].target) ? byType[type].target : [byType[type].target]
40+
const b = Array.isArray(action.target) ? action.target : [action.target]
41+
byType[type].target = [...new Set([...a, ...b])]
42+
}
4043
}
4144
else {
4245
byType[type] = { ...action }

packages/schema-org/src/nodes/WebSite/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ describe('defineWebSite', () => {
122122
})
123123
})
124124

125+
it('can merge duplicate search actions with object target (#709)', async () => {
126+
await useSetup(async (head) => {
127+
const schema = defineWebSite({
128+
name: 'test',
129+
potentialAction: defineSearchAction({
130+
target: '/search?query={search_term_string}',
131+
}),
132+
})
133+
134+
// simulate SPA navigation re-registering the same schema
135+
useSchemaOrg(head, [schema])
136+
useSchemaOrg(head, [schema])
137+
138+
const website = await findNode<WebSite>(head, PrimaryWebSiteId)
139+
expect(website?.potentialAction).toBeDefined()
140+
expect(website?.potentialAction).toHaveLength(1)
141+
})
142+
})
143+
125144
it('can handle multiple websites', async () => {
126145
await useSetup(async (head) => {
127146
useSchemaOrg(head, [

0 commit comments

Comments
 (0)