Skip to content

Commit ed1e442

Browse files
committed
feat: refactor page meta handling and introduce usePageTitle composable
1 parent c1cf0dc commit ed1e442

7 files changed

Lines changed: 44 additions & 37 deletions

File tree

app/composables/use-page-meta.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ReactiveHead } from '@unhead/vue'
22
import type { MaybeRefOrGetter } from 'vue'
33
import { truncate } from '~/utils/string/truncate'
4+
import { joinURL } from 'ufo'
45

56
export 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)

app/composables/use-page-title.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

app/composables/use-roadiz-meta.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff 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.

app/composables/use-roadiz-page-title.ts

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function useRoadizSiteName() {
2+
const commonContent = useCommonContent()
3+
4+
return computed(() => commonContent.data.value?.head?.siteName || undefined)
5+
}

app/error.vue

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@ const blocks = computed(() => {
4343
return errorPage.value?.children || []
4444
})
4545
46-
useHead({
47-
title: title.value,
48-
})
49-
5046
function 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>

app/pages/[...slug].vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const {
5656
title: pageMetaTitle,
5757
truncatedDescription: pageMetaDescription,
5858
} = usePageMeta(roadizMeta)
59+
5960
useHead(pageMetaHead)
6061
6162
// Schema.org structured data

0 commit comments

Comments
 (0)