Skip to content

Commit a855f0a

Browse files
committedApr 2, 2025
fix(InferSeoMetaPlugin): function title templates not respected
Fixes harlan-zw/nuxt-seo-utils#53
1 parent e1cea62 commit a855f0a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
 

Diff for: ‎packages/unhead/src/unhead.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export function createUnhead<T = ResolvableHead>(resolvedOptions: CreateHeadOpti
153153

154154
if (titleTemplate) {
155155
const titleTemplateFn = titleTemplate?.textContent
156-
head._titleTemplate = typeof titleTemplateFn === 'string' ? titleTemplateFn : undefined
156+
head._titleTemplate = titleTemplateFn
157157
if (titleTemplateFn) {
158158
// @ts-expect-error todo
159159
let newTitle = (typeof titleTemplateFn === 'function' ? titleTemplateFn(title?.textContent) : titleTemplateFn)

Diff for: ‎packages/unhead/test/unit/plugins/infer-seo-meta.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -268,4 +268,35 @@ describe('inferSeoMetaPlugin', () => {
268268
}
269269
`)
270270
})
271+
it('infers og:title from function titleTemplate with default value', async () => {
272+
const head = createHead({
273+
disableDefaults: true,
274+
plugins: [InferSeoMetaPlugin()],
275+
})
276+
277+
// Simulate the app.vue setup with a function titleTemplate
278+
head.push({
279+
title: null,
280+
titleTemplate: (titleChunk) => {
281+
return titleChunk ? `${titleChunk} - Website` : 'Welcome to Website'
282+
},
283+
meta: [
284+
{
285+
name: 'description',
286+
content: 'Description.',
287+
},
288+
{
289+
name: 'theme-color',
290+
content: '#007ed4',
291+
},
292+
],
293+
})
294+
295+
const result = await renderSSRHead(head)
296+
297+
expect(result.headTags).toContain('<title>Welcome to Website</title>')
298+
expect(result.headTags).toContain('<meta property="og:title" data-infer="" content="Welcome to Website">')
299+
expect(result.headTags).toContain('<meta name="description" content="Description.">')
300+
expect(result.headTags).toContain('<meta name="theme-color" content="#007ed4">')
301+
})
271302
})

0 commit comments

Comments
 (0)
Please sign in to comment.