init commit for iPAL-kit plugin
This commit is contained in:
@@ -3,12 +3,12 @@ import type { BasePayload } from 'payload'
|
||||
import type { ContentOption } from '../content/index.js'
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
import type { PageMetadata } from './buildMetadata.js'
|
||||
import type { TitleOrder } from './composeTitle.js'
|
||||
import type { SeoMeta } from './types.js'
|
||||
|
||||
import { resolveRoute } from '../content/index.js'
|
||||
import { getLocalizedSlugs } from '../i18n/index.js'
|
||||
import { buildMetadata } from './buildMetadata.js'
|
||||
import { readSiteMetaConfig } from './readSiteMetaConfig.js'
|
||||
import { slugsAcrossLocales } from './slugsAcrossLocales.js'
|
||||
|
||||
type CreatePageMetadataArgs = {
|
||||
/** Absolute site origin, e.g. 'https://example.com'. */
|
||||
@@ -40,37 +40,18 @@ type PageMetadataContext = {
|
||||
slug?: string[]
|
||||
}
|
||||
|
||||
type SettingsShape = {
|
||||
[key: string]: unknown
|
||||
homepage?: { id: number | string; slug?: unknown } | null | number | string
|
||||
titleOrder?: null | TitleOrder
|
||||
titleSeparator?: null | string
|
||||
}
|
||||
|
||||
type DocShape = {
|
||||
id: number | string
|
||||
meta?: null | SeoMeta
|
||||
/** A string when read in one locale, a locale→value map when read with 'all'. */
|
||||
slug?: unknown
|
||||
}
|
||||
|
||||
/** Reads a document again across locales — the slug map hreflang needs. */
|
||||
async function slugsAcrossLocales(
|
||||
payload: BasePayload,
|
||||
collection: string,
|
||||
id: number | string,
|
||||
config: I18nConfig,
|
||||
) {
|
||||
const doc = (await payload.findByID({
|
||||
id,
|
||||
collection: collection as never,
|
||||
depth: 0,
|
||||
locale: 'all',
|
||||
})) as DocShape
|
||||
|
||||
return doc.slug && typeof doc.slug === 'object'
|
||||
? getLocalizedSlugs({ config, slugField: doc.slug as Record<string, unknown> })
|
||||
: {}
|
||||
/** plugin-seo stores the OG image as an upload relationship. */
|
||||
function resolveOgImage(doc: DocShape): null | string {
|
||||
const image = (doc.meta as { image?: unknown } | null | undefined)?.image
|
||||
if (image && typeof image === 'object' && 'url' in image) {
|
||||
return (image as { url: string }).url ?? null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,35 +95,17 @@ export function createPageMetadata(args: CreatePageMetadataArgs) {
|
||||
page,
|
||||
payload,
|
||||
}: PageMetadataContext): Promise<PageMetadata> {
|
||||
const settings = (await payload.findGlobal({
|
||||
slug: settingsSlug,
|
||||
depth: 1,
|
||||
locale: locale as never,
|
||||
})) as SettingsShape
|
||||
const site = await readSiteMetaConfig({ locale, payload, settingsSlug, siteNameField })
|
||||
|
||||
const siteName = (settings[siteNameField] as string | undefined) ?? null
|
||||
|
||||
// The panel stores the bare character ('|'); titles need it padded.
|
||||
const separator = settings.titleSeparator ? ` ${settings.titleSeparator} ` : undefined
|
||||
const order = settings.titleOrder ?? undefined
|
||||
|
||||
const homepage =
|
||||
settings.homepage && typeof settings.homepage === 'object' ? settings.homepage : null
|
||||
// The home page's slug collapses to the locale root (/pl, not /pl/homepage).
|
||||
const homeSlug = typeof homepage?.slug === 'string' ? homepage.slug : undefined
|
||||
|
||||
const empty = () =>
|
||||
buildMetadata({
|
||||
baseUrl,
|
||||
config,
|
||||
homeSlug,
|
||||
locale,
|
||||
meta: null,
|
||||
order,
|
||||
separator,
|
||||
siteName,
|
||||
slugs: {},
|
||||
})
|
||||
const base = {
|
||||
baseUrl,
|
||||
config,
|
||||
homeSlug: site.homeSlug,
|
||||
locale,
|
||||
order: site.order,
|
||||
separator: site.separator,
|
||||
siteName: site.siteName,
|
||||
}
|
||||
|
||||
const route = await resolveRoute({
|
||||
content,
|
||||
@@ -156,7 +119,9 @@ export function createPageMetadata(args: CreatePageMetadataArgs) {
|
||||
|
||||
// Unknown route (the page component will 404) — still return something
|
||||
// coherent rather than throwing during metadata generation.
|
||||
if (!route) {return empty()}
|
||||
if (!route) {
|
||||
return buildMetadata({ ...base, meta: null, slugs: {} })
|
||||
}
|
||||
|
||||
const doc = route.doc as DocShape
|
||||
|
||||
@@ -164,34 +129,30 @@ export function createPageMetadata(args: CreatePageMetadataArgs) {
|
||||
// segment differs per locale, since it's the archive page's own slug.
|
||||
const prefix =
|
||||
route.type === 'entry'
|
||||
? await slugsAcrossLocales(payload, collection, (route.archive as DocShape).id, config)
|
||||
? await slugsAcrossLocales({
|
||||
id: (route.archive as DocShape).id,
|
||||
collection,
|
||||
config,
|
||||
payload,
|
||||
})
|
||||
: undefined
|
||||
|
||||
const docCollection = route.type === 'entry' ? route.collection : collection
|
||||
const slugs = await slugsAcrossLocales(payload, docCollection, doc.id, config)
|
||||
const slugs = await slugsAcrossLocales({
|
||||
id: doc.id,
|
||||
collection: route.type === 'entry' ? route.collection : collection,
|
||||
config,
|
||||
payload,
|
||||
})
|
||||
|
||||
// Page 2 of a listing is its own URL, not a variant of page 1.
|
||||
const query = route.type === 'archive' && route.page > 1 ? `?page=${route.page}` : undefined
|
||||
|
||||
// plugin-seo stores the OG image as an upload relationship.
|
||||
const image = (doc.meta as { image?: unknown } | null | undefined)?.image
|
||||
const imageUrl =
|
||||
image && typeof image === 'object' && 'url' in image
|
||||
? ((image as { url: string }).url ?? null)
|
||||
: null
|
||||
|
||||
return buildMetadata({
|
||||
baseUrl,
|
||||
config,
|
||||
homeSlug,
|
||||
imageUrl,
|
||||
locale,
|
||||
...base,
|
||||
imageUrl: resolveOgImage(doc),
|
||||
meta: doc.meta,
|
||||
order,
|
||||
prefix,
|
||||
query,
|
||||
separator,
|
||||
siteName,
|
||||
slugs,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user