init commit for iPAL-kit plugin
This commit is contained in:
@@ -0,0 +1,198 @@
|
||||
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'
|
||||
|
||||
type CreatePageMetadataArgs = {
|
||||
/** Absolute site origin, e.g. 'https://example.com'. */
|
||||
baseUrl?: string
|
||||
/** Collection holding pages. Defaults to 'pages'. */
|
||||
collection?: string
|
||||
config: I18nConfig
|
||||
/**
|
||||
* Archive-backed collections, same value as the plugin option. Pass it and
|
||||
* entry URLs (/pl/artykuly/moj-post) get correct canonical and hreflang;
|
||||
* omit it and only pages are handled.
|
||||
*/
|
||||
content?: ContentOption
|
||||
/** SiteSettings global slug. Defaults to 'site-settings'. */
|
||||
settingsSlug?: string
|
||||
/** Field on SiteSettings holding the site name. Defaults to 'siteName'. */
|
||||
siteNameField?: string
|
||||
}
|
||||
|
||||
type PageMetadataContext = {
|
||||
locale: string
|
||||
/**
|
||||
* Page number from ?page= on an archive listing. Pass it and page 2 gets a
|
||||
* canonical to itself; leave it out and every page claims to be page 1.
|
||||
*/
|
||||
page?: number
|
||||
payload: BasePayload
|
||||
/** Route slug segments; empty/undefined means the locale root. */
|
||||
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> })
|
||||
: {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Metadata for the page route, with every resolver already wired.
|
||||
*
|
||||
* `createMetadataGenerator` asks the client for resolvers because it can't know
|
||||
* their collections. But for the plugin's own conventions it does know: pages
|
||||
* live in one collection, the site name sits on SiteSettings, the OG image sits
|
||||
* on the plugin-seo `meta` group, the home page is whatever System Pages points
|
||||
* at, and — with `content` — entries live under their collection's archive page.
|
||||
* Re-declaring all that in every project is copy-paste, so this resolves it.
|
||||
*
|
||||
* Reach for `createMetadataGenerator` instead when a route doesn't follow those
|
||||
* conventions — custom image logic, a different global, hand-rolled paths.
|
||||
*
|
||||
* The plugin never calls getPayload, and Next calls generateMetadata without a
|
||||
* payload, so the client keeps a small wrapper:
|
||||
*
|
||||
* ```ts
|
||||
* const pageMetadata = createPageMetadata({ config: i18nConfig, baseUrl, content })
|
||||
*
|
||||
* export async function generateMetadata({ params }) {
|
||||
* const { locale, slug } = await params
|
||||
* return pageMetadata({ payload: await getPayload({ config }), locale, slug })
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
export function createPageMetadata(args: CreatePageMetadataArgs) {
|
||||
const {
|
||||
baseUrl,
|
||||
collection = 'pages',
|
||||
config,
|
||||
content,
|
||||
settingsSlug = 'site-settings',
|
||||
siteNameField = 'siteName',
|
||||
} = args
|
||||
|
||||
return async function pageMetadata({
|
||||
slug,
|
||||
locale,
|
||||
page,
|
||||
payload,
|
||||
}: PageMetadataContext): Promise<PageMetadata> {
|
||||
const settings = (await payload.findGlobal({
|
||||
slug: settingsSlug,
|
||||
depth: 1,
|
||||
locale: locale as never,
|
||||
})) as SettingsShape
|
||||
|
||||
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 route = await resolveRoute({
|
||||
content,
|
||||
locale,
|
||||
page,
|
||||
pagesSlug: collection,
|
||||
payload,
|
||||
segments: slug,
|
||||
settingsSlug,
|
||||
})
|
||||
|
||||
// Unknown route (the page component will 404) — still return something
|
||||
// coherent rather than throwing during metadata generation.
|
||||
if (!route) {return empty()}
|
||||
|
||||
const doc = route.doc as DocShape
|
||||
|
||||
// An entry sits under its archive, so its URLs need that segment — and the
|
||||
// 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)
|
||||
: undefined
|
||||
|
||||
const docCollection = route.type === 'entry' ? route.collection : collection
|
||||
const slugs = await slugsAcrossLocales(payload, docCollection, doc.id, config)
|
||||
|
||||
// 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,
|
||||
meta: doc.meta,
|
||||
order,
|
||||
prefix,
|
||||
query,
|
||||
separator,
|
||||
siteName,
|
||||
slugs,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user