Skip to content

Commit

Permalink
fix(addons): honour titleTemplate tagPriority
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jul 14, 2024
1 parent 4aefeaa commit 1080823
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/addons/src/plugins/inferSeoMetaPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ export interface InferSeoMetaPluginOptions {
resolve({ entries }) {
// need to find the last titleTemplate entry
let titleTemplate = null
let lastWeight = 999
for (const entry of entries) {
const inputKey = entry.resolvedInput ? 'resolvedInput' : 'input'
const input = entry[inputKey]
if (typeof input.titleTemplate !== 'undefined')
const weight = (typeof input.titleTemplate === 'object' ? input.titleTemplate?.tagPriority : false) || entry.tagPriority || 100
if (typeof input.titleTemplate !== 'undefined' && weight <= lastWeight) {
titleTemplate = input.titleTemplate
lastWeight = weight
}
}

for (const entry of entries) {
Expand Down
44 changes: 44 additions & 0 deletions test/unhead/hooks/infer-seo-meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,48 @@ describe('hooks', () => {
</body></html>"
`)
})

it('infer-seo-meta multiple titleTemplates', async () => {
const head = useDOMHead({
plugins: [
InferSeoMetaPlugin(),
],
})

head.push({
titleTemplate: {
textContent: '%s | 1',
tagPriority: 50,
},
title: 'Hello World',
meta: [
{ name: 'description', content: 'description' },
],
})

head.push({
titleTemplate: '%s | 2',
title: 'Hello World',
meta: [
{ name: 'description', content: 'description' },
],
}, {
tagPriority: -5,
})

head.push({
titleTemplate: '%s | 3',
title: 'Hello World',
meta: [
{ name: 'description', content: 'description' },
],
}, {
tagPriority: 103,
})
const dom = await useDelayedSerializedDom()
const title = dom.match(/<title>Hello World \| (\d)<\/title>/)?.[1]
const ogTitle = dom.match(/<meta property="og:title" content="Hello World \| (\d)">/)?.[1]
expect(title).toEqual(ogTitle)
expect(title).toEqual(`2`)
})
})

0 comments on commit 1080823

Please sign in to comment.