File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import type { ReactiveHead } from '@unhead/vue'
22import type { MaybeRefOrGetter } from 'vue'
33import { truncate } from '~/utils/string/truncate'
4+ import { joinURL } from 'ufo'
45
56export interface PageMetaAlternateLink {
67 locale : string
@@ -29,7 +30,7 @@ export function usePageMeta(options: PageMetaOptions = {}) {
2930 const title = computed ( ( ) => toValue ( options . title ) || '' )
3031 const description = computed ( ( ) => toValue ( options . description ) || '' )
3132 const truncatedDescription = computed ( ( ) => truncate ( description . value , 160 ) )
32- const image = computed ( ( ) => toValue ( options . image ) )
33+ const image = computed ( ( ) => toValue ( options . image ) || joinURL ( runtimeConfig . app . cdnUrl || runtimeConfig . public . site . url , '/images/share.jpg' ) )
3334 const siteName = computed ( ( ) => toValue ( options . siteName ) || runtimeConfig . public . site . name || '' )
3435 const noindex = computed ( ( ) => ! ! toValue ( options . noindex ) )
3536 const canonicalUrl = computed ( ( ) => toValue ( options . canonicalUrl ) || undefined )
Original file line number Diff line number Diff line change 1+ // commonContent and currentPage need to be set before calling this composable
2+
3+ interface UsePageTitleOptions {
4+ title ?: MaybeRefOrGetter < string | undefined >
5+ siteName ?: MaybeRefOrGetter < string | undefined >
6+ }
7+
8+ export function usePageTitle ( options : UsePageTitleOptions = { } ) {
9+ const runtimeConfig = useRuntimeConfig ( )
10+ const internalSiteName = computed ( ( ) =>
11+ toValue ( options . siteName ) || runtimeConfig ?. public ?. site ?. name )
12+
13+ function getPageTitle ( title : string | undefined ) {
14+ if ( ! title ) {
15+ return internalSiteName . value || null
16+ }
17+
18+ return `${ title } — ${ internalSiteName . value } `
19+ }
20+
21+ return {
22+ getPageTitle,
23+ plainTitle : computed ( ( ) => getPageTitle ( toValue ( options . title ) ) ) ,
24+ }
25+ }
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ export async function useRoadizMeta(
2020 const noindex = computed ( ( ) => ( item . value as RoadizNodesSources ) ?. noIndex || previewIsActive . value )
2121
2222 // -------------------- Title -------------------
23- const { getPageTitle } = useRoadizPageTitle ( )
23+ const { getPageTitle } = usePageTitle ( )
2424 const title = computed ( ( ) => {
2525 // The API should always return a meta title.
2626 // The meta title is set in the Roadiz back office for each page. The logic is:
@@ -130,8 +130,6 @@ export async function useRoadizMeta(
130130 return commonContentData . value ?. head ?. shareImage
131131 } )
132132
133- const fallbackImageUrl = joinURL ( runtimeConfig . app . cdnUrl || runtimeConfig . public . site . url , '/images/share.jpg' )
134-
135133 async function resolveMetaImage ( document : RoadizDocument | null | undefined ) : Promise < string | undefined > {
136134 if ( ! document ?. processable || ! document ?. relativePath ) return undefined
137135
@@ -157,7 +155,7 @@ export async function useRoadizMeta(
157155 const resolvedImageUrl = ref < string | undefined > ( )
158156
159157 async function resolveImage ( document : RoadizDocument | null | undefined ) {
160- resolvedImageUrl . value = ( await resolveMetaImage ( document ) ) || fallbackImageUrl
158+ resolvedImageUrl . value = await resolveMetaImage ( document )
161159 }
162160
163161 // Resolve upfront so the first (SSR) render already has the URL available.
Load diff This file was deleted.
Original file line number Diff line number Diff line change 1+ export function useRoadizSiteName ( ) {
2+ const commonContent = useCommonContent ( )
3+
4+ return computed ( ( ) => commonContent . data . value ?. head ?. siteName || undefined )
5+ }
Original file line number Diff line number Diff line change @@ -43,13 +43,18 @@ const blocks = computed(() => {
4343 return errorPage .value ?.children || []
4444})
4545
46- useHead ({
47- title: title .value ,
48- })
49-
5046function reloadPage() {
5147 window .location .reload ()
5248}
49+
50+ // ------------------ Update meta data ----------------
51+ const siteName = useRoadizSiteName ()
52+ const { getPageTitle } = usePageTitle ({ siteName })
53+ const pageMeta = usePageMeta ({
54+ title: getPageTitle (title .value ),
55+ })
56+
57+ useHead (pageMeta )
5358 </script >
5459
5560<template >
Original file line number Diff line number Diff line change @@ -56,6 +56,7 @@ const {
5656 title : pageMetaTitle,
5757 truncatedDescription : pageMetaDescription,
5858} = usePageMeta (roadizMeta )
59+
5960useHead (pageMetaHead )
6061
6162// Schema.org structured data
You can’t perform that action at this time.
0 commit comments