Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 77ace21

Browse files
authored
fix(addons): respect user-provided twitter:card in InferSeoMetaPlugin (#681)
Make twitter:card push conditional so that `twitterCard: false` option properly suppresses output. User-provided values already take precedence via `tagPriority: 'low'` on the plugin entry. Adds regression tests for both override scenarios. Closes #555
1 parent c27077d commit 77ace21

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

packages/unhead/src/plugins/inferSeoMetaPlugin.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ export interface InferSeoMetaPluginOptions {
2323

2424
export function InferSeoMetaPlugin(options: InferSeoMetaPluginOptions = {}) {
2525
return defineHeadPlugin((head) => {
26+
if (options.twitterCard !== false) {
27+
head.push({
28+
meta: [
29+
{
30+
name: 'twitter:card',
31+
content: options.twitterCard || 'summary_large_image',
32+
tagPriority: 'low',
33+
},
34+
],
35+
})
36+
}
2637
head.push({
2738
meta: [
28-
{
29-
name: 'twitter:card',
30-
content: options.twitterCard || 'summary_large_image',
31-
tagPriority: 'low',
32-
},
3339
{
3440
'property': 'og:title',
3541
'tagPriority': 'low',

packages/unhead/test/unit/plugins/infer-seo-meta.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { renderSSRHead } from '@unhead/ssr'
22
import { describe, it } from 'vitest'
3+
import { useHead, useSeoMeta } from '../../../src'
34
import { InferSeoMetaPlugin, TemplateParamsPlugin } from '../../../src/plugins'
45
import { createHead } from '../../../src/server'
56

@@ -331,4 +332,44 @@ describe('inferSeoMetaPlugin', () => {
331332
<meta property="og:title" data-infer="" content="Nuxt SEO – Hello World">"
332333
`)
333334
})
335+
336+
it('user twitterCard via useSeoMeta overrides plugin default (#555)', () => {
337+
const head = createHead({
338+
disableDefaults: true,
339+
plugins: [InferSeoMetaPlugin()],
340+
})
341+
342+
useSeoMeta(head, { twitterCard: 'summary' })
343+
344+
const result = renderSSRHead(head)
345+
expect(result.headTags).toMatchInlineSnapshot(`"<meta name="twitter:card" content="summary">"`)
346+
})
347+
348+
it('user twitter:card via useHead overrides plugin default (#555)', () => {
349+
const head = createHead({
350+
disableDefaults: true,
351+
plugins: [InferSeoMetaPlugin()],
352+
})
353+
354+
useHead(head, { meta: [{ name: 'twitter:card', content: 'summary' }] })
355+
356+
const result = renderSSRHead(head)
357+
expect(result.headTags).toMatchInlineSnapshot(`"<meta name="twitter:card" content="summary">"`)
358+
})
359+
360+
it('plugin twitterCard: false disables twitter:card output (#555)', () => {
361+
const head = createHead({
362+
disableDefaults: true,
363+
plugins: [InferSeoMetaPlugin({ twitterCard: false })],
364+
})
365+
366+
head.push({ title: 'My Title' })
367+
368+
const result = renderSSRHead(head)
369+
expect(result.headTags).not.toContain('twitter:card')
370+
expect(result.headTags).toMatchInlineSnapshot(`
371+
"<title>My Title</title>
372+
<meta property="og:title" data-infer="" content="My Title">"
373+
`)
374+
})
334375
})

0 commit comments

Comments
 (0)