11import type { Head } from '@unhead/vue'
22import type { NuxtSSRContext } from 'nuxt/app'
3- import type { DefineOgImageInput , OgImageOptions , OgImagePrebuilt } from '../types'
3+ import type { OgImageOptions , OgImagePrebuilt } from '../types'
44import { componentNames } from '#build/nuxt-og-image/components.mjs'
55import { useHead } from '#imports'
66import { resolveUnrefHeadInput } from '@unhead/vue'
77import { defu } from 'defu'
88import { stringify } from 'devalue'
99import { withQuery } from 'ufo'
10- import { unref } from 'vue'
11- import { generateMeta , separateProps } from '../shared'
10+ import { generateMeta , separateProps , useOgImageRuntimeConfig } from '../shared'
1211
13- export function createOgImageMeta ( src : string | null , input : OgImageOptions | OgImagePrebuilt , resolvedOptions : OgImageOptions , ssrContext : NuxtSSRContext ) {
12+ export function setHeadOgImagePrebuilt ( input : OgImagePrebuilt ) {
1413 if ( import . meta. client ) {
1514 return
1615 }
17- const _input = separateProps ( defu ( input , ssrContext . _ogImagePayload ) )
18- let url = src || input . url || resolvedOptions . url
16+ const url = input . url
1917 if ( ! url )
2018 return
21- if ( input . _query && Object . keys ( input . _query ) . length && url )
22- url = withQuery ( url , { _query : input . _query } )
23- const meta = generateMeta ( url , resolvedOptions )
19+ const meta = generateMeta ( url , input )
20+ useHead ( { meta } , { tagPriority : 'high' } )
21+ }
22+
23+ export function createOgImageMeta ( src : string , input : OgImageOptions | OgImagePrebuilt , ssrContext : NuxtSSRContext ) {
24+ if ( import . meta. client ) {
25+ return
26+ }
27+ const { defaults } = useOgImageRuntimeConfig ( )
28+ const _input = separateProps ( defu ( input , ssrContext . _ogImagePayload ) )
29+ if ( input . _query && Object . keys ( input . _query ) . length )
30+ src = withQuery ( src , { _query : input . _query } )
31+ const meta = generateMeta ( src , input )
2432 ssrContext . _ogImageInstances = ssrContext . _ogImageInstances || [ ]
2533 const script : Head [ 'script' ] = [ ]
2634 if ( src ) {
@@ -32,12 +40,19 @@ export function createOgImageMeta(src: string | null, input: OgImageOptions | Og
3240 const payload = resolveUnrefHeadInput ( _input )
3341 if ( typeof payload . props . title === 'undefined' )
3442 payload . props . title = '%s'
43+ payload . component = resolveComponentName ( input . component , defaults . component )
3544 delete payload . url
3645 if ( payload . _query && Object . keys ( payload . _query ) . length === 0 ) {
3746 delete payload . _query
3847 }
48+ const final = { }
49+ for ( const k in payload ) {
50+ if ( payload [ k ] !== defaults [ k ] ) {
51+ final [ k ] = payload [ k ]
52+ }
53+ }
3954 // don't apply defaults
40- return stringify ( payload )
55+ return stringify ( final )
4156 } ,
4257 // we want this to be last in our head
4358 tagPosition : 'bodyClose' ,
@@ -54,23 +69,16 @@ export function createOgImageMeta(src: string | null, input: OgImageOptions | Og
5469 ssrContext . _ogImageInstances . push ( instance )
5570}
5671
57- export function normaliseOptions ( _options : DefineOgImageInput ) : OgImageOptions | OgImagePrebuilt {
58- const options = { ...unref ( _options ) } as OgImageOptions
59- if ( ! options )
60- return options
72+ export function resolveComponentName ( component : OgImageOptions [ 'component' ] , fallback : string ) : OgImageOptions [ 'component' ] {
73+ component = component || fallback || componentNames ?. [ 0 ] ?. pascalName
6174 // try and fix component name if we're using a shorthand (i.e Banner instead of OgImageBanner)
62- if ( options . component && componentNames ) {
63- const originalName = options . component
75+ if ( component && componentNames ) {
76+ const originalName = component
6477 for ( const component of componentNames ) {
6578 if ( component . pascalName . endsWith ( originalName ) || component . kebabName . endsWith ( originalName ) ) {
66- options . component = component . pascalName
67- break
79+ return component . pascalName
6880 }
6981 }
7082 }
71- else if ( ! options . component ) {
72- // just pick first component
73- options . component = componentNames [ 0 ] ?. pascalName
74- }
75- return options
83+ return component
7684}
0 commit comments