File tree Expand file tree Collapse file tree 2 files changed +39
-6
lines changed
Expand file tree Collapse file tree 2 files changed +39
-6
lines changed Original file line number Diff line number Diff line change @@ -46,22 +46,23 @@ export function InferSeoMetaPlugin(options: InferSeoMetaPluginOptions = {}) {
4646 key : 'infer-seo-meta' ,
4747 hooks : {
4848 'tags:beforeResolve' : ( { tagMap } ) => {
49- let title = head . _title || ''
50- const titleTemplate = head . _titleTemplate
49+ let title = head . _titleTemplate || head . _title
5150 // check if the current title is %infer
5251 const ogTitle = tagMap . get ( 'meta:og:title' )
5352 if ( typeof ogTitle ?. props [ 'data-infer' ] !== 'undefined' ) {
54- if ( titleTemplate ) {
55- // @ts -expect-error broken types
56- title = typeof titleTemplate === 'function' ? titleTemplate ( title ) : titleTemplate . replace ( '%s' , title )
53+ if ( typeof title === 'function' ) {
54+ // @ts -expect-error untyped
55+ title = title ( head . _title )
5756 }
5857 ogTitle . props ! . content = options . ogTitle ? options . ogTitle ( title ) : title || ''
58+ ogTitle . processTemplateParams = true
5959 }
6060
6161 const description = tagMap . get ( 'meta:description' ) ?. props ?. content
6262 const ogDescription = tagMap . get ( 'meta:og:description' )
6363 if ( typeof ogDescription ?. props [ 'data-infer' ] !== 'undefined' ) {
6464 ogDescription . props ! . content = options . ogDescription ? options . ogDescription ( description ) : description || ''
65+ ogDescription . processTemplateParams = true
6566 }
6667 } ,
6768 } ,
Original file line number Diff line number Diff line change @@ -189,7 +189,10 @@ describe('inferSeoMetaPlugin', () => {
189189 it ( 'handles title template' , async ( ) => {
190190 const head = createHead ( {
191191 disableDefaults : true ,
192- plugins : [ InferSeoMetaPlugin ( ) ] ,
192+ plugins : [
193+ InferSeoMetaPlugin ( ) ,
194+ TemplateParamsPlugin ,
195+ ] ,
193196 } )
194197 head . push ( {
195198 title : 'Title' ,
@@ -299,4 +302,33 @@ describe('inferSeoMetaPlugin', () => {
299302 expect ( result . headTags ) . toContain ( '<meta name="description" content="Description.">' )
300303 expect ( result . headTags ) . toContain ( '<meta name="theme-color" content="#007ed4">' )
301304 } )
305+ it ( 'template params Nuxt SEO #416' , async ( ) => {
306+ const head = createHead ( {
307+ disableDefaults : true ,
308+ plugins : [
309+ InferSeoMetaPlugin ( ) ,
310+ TemplateParamsPlugin ,
311+ ] ,
312+ } )
313+ head . push ( {
314+ titleTemplate : '%siteName %separator %s' ,
315+ templateParams : {
316+ separator : '–' ,
317+ siteName : 'Nuxt SEO' ,
318+ } ,
319+ } )
320+
321+ // Simulate the app.vue setup with a function titleTemplate
322+ head . push ( {
323+ title : 'Hello World' ,
324+ } )
325+
326+ const result = await renderSSRHead ( head )
327+
328+ expect ( result . headTags ) . toMatchInlineSnapshot ( `
329+ "<title>Nuxt SEO – Hello World</title>
330+ <meta name="twitter:card" content="summary_large_image">
331+ <meta property="og:title" data-infer="" content="Nuxt SEO – Hello World">"
332+ ` )
333+ } )
302334} )
You can’t perform that action at this time.
0 commit comments