50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import type { Metadata } from 'next'
|
|
|
|
import type { Media, Page, Post, Config } from '../payload-types'
|
|
|
|
import { mergeOpenGraph } from './mergeOpenGraph'
|
|
import { getServerSideURL } from './getURL'
|
|
|
|
const getImageURL = (image?: Media | Config['db']['defaultIDType'] | null) => {
|
|
const serverUrl = getServerSideURL()
|
|
|
|
let url = serverUrl + '/website-template-OG.webp'
|
|
|
|
if (image && typeof image === 'object' && 'url' in image) {
|
|
const ogUrl = (image as any).sizes?.og?.url
|
|
|
|
url = ogUrl ? serverUrl + ogUrl : serverUrl + image.url
|
|
}
|
|
|
|
return url
|
|
}
|
|
|
|
export const generateMeta = async (args: {
|
|
doc: Partial<Page> | Partial<Post> | null
|
|
}): Promise<Metadata> => {
|
|
const { doc } = args
|
|
|
|
const ogImage = getImageURL(doc?.meta?.image)
|
|
|
|
const title = doc?.meta?.title
|
|
? doc?.meta?.title + ' | Layer & Mesh'
|
|
: 'Layer & Mesh'
|
|
|
|
return {
|
|
description: doc?.meta?.description,
|
|
openGraph: mergeOpenGraph({
|
|
description: doc?.meta?.description || '',
|
|
images: ogImage
|
|
? [
|
|
{
|
|
url: ogImage,
|
|
},
|
|
]
|
|
: undefined,
|
|
title,
|
|
url: Array.isArray(doc?.slug) ? doc?.slug.join('/') : '/',
|
|
}),
|
|
title,
|
|
}
|
|
}
|