Skip to content

Commit

Permalink
fix(schema-org): allow @id override (#275)
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Lamant <tom@tmlmt.com>
  • Loading branch information
harlan-zw and tmlmt authored Nov 21, 2023
1 parent cc4f7df commit 2cc88b7
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/schema-org/src/core/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export function resolveNodeId<T extends Thing>(node: T, ctx: SchemaOrgGraph, res
return node
}

const rootId = Array.isArray(resolver.idPrefix) ? resolver.idPrefix?.[1] : undefined
const rootId = node['@id'] || (Array.isArray(resolver.idPrefix) ? resolver.idPrefix?.[1] : undefined)
// transform ['host', PrimaryWebPageId] to https://host.com/#webpage
if (resolveAsRoot && rootId) {
// allow overriding root ids
Expand Down
42 changes: 42 additions & 0 deletions packages/schema-org/src/nodes/LocalBusiness/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,46 @@ describe('defineLocalBusiness', () => {
`)
})
})

it('can have custom id', async () => {
await useSetup(async () => {
useSchemaOrg([
defineLocalBusiness({
'@type': 'Dentist',
'name': 'test',
'address': {
addressCountry: 'Australia',
postalCode: '2000',
streetAddress: '123 st',
},
'@id': 'https://example.com/place/123#identity',
'url': 'https://www.test.com',
}),
])

const graphNodes = await injectSchemaOrg()

expect(graphNodes).toMatchInlineSnapshot(`
[
{
"@id": "https://example.com/place/123#identity",
"@type": [
"Organization",
"LocalBusiness",
"Dentist",
],
"address": {
"@type": "PostalAddress",
"addressCountry": "Australia",
"postalCode": "2000",
"streetAddress": "123 st",
},
"currenciesAccepted": "AUD",
"name": "test",
"url": "https://www.test.com",
},
]
`)
})
})
})
2 changes: 1 addition & 1 deletion packages/schema-org/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function dedupeMerge<T extends Thing>(node: T, field: keyof T, value: any
export function prefixId(url: string, id: Id | string) {
// already prefixed
if (hasProtocol(id))
return url as Id
return id as Id
if (!id.startsWith('#'))
id = `#${id}`
return joinURL(url, id) as Id
Expand Down
51 changes: 51 additions & 0 deletions test/schema.org/ssr/ids.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, it } from 'vitest'
import { createHead, useHead } from 'unhead'
import { defineWebPage, useSchemaOrg } from '@unhead/schema-org'

describe('schema.org ssr ids', () => {
it('adds host prefix to custom id without host', async () => {
const ssrHead = createHead()

useHead({
templateParams: {
schemaOrg: {
host: 'https://example.com',
},
},
})

useSchemaOrg([
defineWebPage({
'@id': '#foo',
'name': 'foo',
}),
])


const tags = await ssrHead.resolveTags()
const id = JSON.parse(tags[0].innerHTML!)['@graph'][0]['@id']
expect(id).toMatchInlineSnapshot('"https://example.com/#foo"')
})
it('allows ids with custom domains', async () => {
const ssrHead = createHead()

useHead({
templateParams: {
schemaOrg: {
host: 'https://example.com',
},
},
})

useSchemaOrg([
defineWebPage({
'@id': 'https://custom-domain.com/#foo',
'name': 'foo',
}),
])

const tags = await ssrHead.resolveTags()
const id = JSON.parse(tags[0].innerHTML!)['@graph'][0]['@id']
expect(id).toMatchInlineSnapshot('"https://custom-domain.com/#foo"')
})
})

0 comments on commit 2cc88b7

Please sign in to comment.