Merge claude/admin-white-label-meta: admin link previews, agency option
adminMeta (default on): panel title suffix, description, og:site_name, preview image (/api/ipal/admin-og) and icon (/api/ipal/admin-icon) from Site settings, robots noindex,nofollow on /admin; per-piece overrides, adminMeta: false, the project's admin.meta wins. agency option (default Intecion.com, false = none). Merge resolution: - docs/CHANGELOG.md: one Unreleased section covering storage-keys and this branch (Changed: credit text; New: adminMeta, agency; upgrade step 4). - dev importMap.js: regenerated with IPAL_DEV_2FA=1, so it keeps the TOTP entries (2FA fix) and adds AdminIcon, AdminLogo, IntecionCredit (whiteLabel).
This commit is contained in:
@@ -3,12 +3,12 @@ import { defineTexts } from '../define.js'
|
||||
/** Admin component texts — settings. Placeholders use Payload's {{name}} syntax. */
|
||||
export const settingsTexts = defineTexts({
|
||||
en: {
|
||||
brandingCredit: 'Technology supported by Intecion.com',
|
||||
brandingCredit: 'Technology support: {{agency}}',
|
||||
faviconNotSquare: 'The icon is not square ({{width}}×{{height}} px), so it may be cropped in tabs and search results.',
|
||||
faviconTooSmall: 'The icon is {{width}}×{{height}} px. Google shows icons of at least 48×48 px; upload a larger one.',
|
||||
},
|
||||
pl: {
|
||||
brandingCredit: 'Technologię wspiera Intecion.com',
|
||||
brandingCredit: 'Wsparcie technologiczne: {{agency}}',
|
||||
faviconNotSquare: 'Ikona nie jest kwadratowa ({{width}}×{{height}} px), więc może być przycięta na kartach i w wynikach wyszukiwania.',
|
||||
faviconTooSmall: 'Ikona ma {{width}}×{{height}} px. Google pokazuje ikony od 48×48 px; wgraj większą.',
|
||||
},
|
||||
|
||||
@@ -116,7 +116,7 @@ export async function AdminIcon(props: BrandingProps = {}) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Discreet vendor credit: "Technology supported by Intecion.com". This is the
|
||||
* Discreet vendor credit: "Technology support: Intecion.com". This is the
|
||||
* PLUGIN VENDOR's credit (Intecion ships ipal-kit, the framework), not client
|
||||
* data — a sensible default here is fine and A0-safe. The client's own brand
|
||||
* still owns the logo/name; this is a small line stating who supports the tech.
|
||||
@@ -130,11 +130,20 @@ export async function AdminIcon(props: BrandingProps = {}) {
|
||||
* - 'login' → centered line below the login form
|
||||
*/
|
||||
export function IntecionCredit(
|
||||
props: { i18n?: { t: (key: never, vars?: Record<string, unknown>) => string }; text?: string; url?: string; variant?: 'login' | 'nav' } = {},
|
||||
props: {
|
||||
/** Agency name in the default text (`agency` option). Default "Intecion.com". */
|
||||
agency?: string
|
||||
i18n?: { t: (key: never, vars?: Record<string, unknown>) => string }
|
||||
text?: string
|
||||
url?: string
|
||||
variant?: 'login' | 'nav'
|
||||
} = {},
|
||||
) {
|
||||
const { i18n, url = 'https://intecion.com', variant = 'nav' } = props
|
||||
const { agency = 'Intecion.com', i18n, url = 'https://intecion.com', variant = 'nav' } = props
|
||||
// A project's own text wins; otherwise the plugin's text in the admin language.
|
||||
const text = props.text ?? (i18n ? ipalT(i18n.t, 'brandingCredit') : settingsTexts.en.brandingCredit)
|
||||
const text =
|
||||
props.text ??
|
||||
(i18n ? ipalT(i18n.t, 'brandingCredit', { agency }) : settingsTexts.en.brandingCredit.replace('{{agency}}', agency))
|
||||
const style =
|
||||
variant === 'login'
|
||||
? {
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
import type { Endpoint, PayloadRequest } from 'payload'
|
||||
|
||||
import type { AgencyOption, ResolvedAdminMeta } from './adminMeta.js'
|
||||
|
||||
import { ADMIN_ICON_PATH, ADMIN_META_TEXTS, ADMIN_OG_PATH } from './adminMeta.js'
|
||||
|
||||
/**
|
||||
* The panel's link-preview image (1200×630) and icon, drawn from SiteSettings:
|
||||
* logo (or the site name), "Administration panel" and a small technology
|
||||
* support line. Rendered on the server with next/og — no external service.
|
||||
* Public (link previews fetch without a session); they show only what the site
|
||||
* already shows publicly. Never throw: a missing logo or setting falls back to
|
||||
* the site name, then to initials.
|
||||
*/
|
||||
|
||||
type Fields = { iconField?: string; logoField?: string; nameField?: string; settingsSlug?: string }
|
||||
|
||||
type Branding = { icon: null | string; logo: null | string; name: string }
|
||||
|
||||
const CACHE = 'public, max-age=3600, s-maxage=3600'
|
||||
|
||||
async function loadBranding(req: PayloadRequest, f: Required<Fields>): Promise<Branding> {
|
||||
try {
|
||||
const locale = req.payload.config.localization ? req.payload.config.localization.defaultLocale : undefined
|
||||
const s = (await req.payload.findGlobal({ slug: f.settingsSlug as never, depth: 1, ...(locale ? { locale: locale as never } : {}), req })) as Record<string, unknown>
|
||||
const url = (v: unknown) => (v && typeof v === 'object' && typeof (v as { url?: unknown }).url === 'string' ? (v as { url: string }).url : null)
|
||||
const name = typeof s[f.nameField] === 'string' ? (s[f.nameField] as string).trim() : ''
|
||||
return { name, icon: url(s[f.iconField]), logo: url(s[f.logoField]) }
|
||||
} catch {
|
||||
return { name: '', icon: null, logo: null }
|
||||
}
|
||||
}
|
||||
|
||||
/** Absolute URL of an upload, resolved against the request (uploads are often relative). */
|
||||
const absolute = (url: string, req: PayloadRequest) => new URL(url, req.url ?? `http://localhost`).toString()
|
||||
|
||||
/** The image as a data URI, so the renderer needs no network access of its own. */
|
||||
async function inline(url: null | string, req: PayloadRequest): Promise<null | string> {
|
||||
if (!url) {return null}
|
||||
try {
|
||||
const res = await fetch(absolute(url, req))
|
||||
if (!res.ok) {return null}
|
||||
const type = res.headers.get('content-type') ?? 'image/png'
|
||||
// The renderer can't draw WebP/AVIF; the site name is shown instead.
|
||||
if (!/png|jpe?g|gif|svg/.test(type)) {return null}
|
||||
return `data:${type};base64,${Buffer.from(await res.arrayBuffer()).toString('base64')}`
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// Loaded on first use, so the Payload CLI (which reads this config outside Next) never loads it;
|
||||
// '.js' because next has no exports map (plain Node ESM can't resolve 'next/og').
|
||||
const imageResponse = async () => (await import('next/og.js')).ImageResponse
|
||||
|
||||
const initials = (name: string) =>
|
||||
name
|
||||
.split(/\s+/)
|
||||
.filter(Boolean)
|
||||
.slice(0, 2)
|
||||
.map((w) => w[0].toUpperCase())
|
||||
.join('') || 'A'
|
||||
|
||||
export function buildAdminImageEndpoints(resolved: ResolvedAdminMeta, fields: Fields = {}): Endpoint[] {
|
||||
const f: Required<Fields> = {
|
||||
iconField: fields.iconField ?? 'favicon',
|
||||
logoField: fields.logoField ?? 'logo',
|
||||
nameField: fields.nameField ?? 'siteName',
|
||||
settingsSlug: fields.settingsSlug ?? 'site-settings',
|
||||
}
|
||||
const texts = ADMIN_META_TEXTS[resolved.language]
|
||||
const agency: AgencyOption | false = resolved.agency
|
||||
|
||||
const og: Endpoint = {
|
||||
handler: async (req) => {
|
||||
const brand = await loadBranding(req, f)
|
||||
const logo = await inline(brand.logo, req)
|
||||
const ImageResponse = await imageResponse()
|
||||
const image = new ImageResponse(
|
||||
(
|
||||
<div style={{ alignItems: 'center', background: '#ffffff', color: '#111111', display: 'flex', flexDirection: 'column', height: '100%', justifyContent: 'center', padding: 80, width: '100%' }}>
|
||||
{logo ? (
|
||||
// Rendered by next/og, not the browser.
|
||||
<img alt="" src={logo} style={{ maxHeight: 220, maxWidth: 760, objectFit: 'contain' }} />
|
||||
) : (
|
||||
<div style={{ display: 'flex', fontSize: 84, fontWeight: 700, textAlign: 'center' }}>{brand.name || texts.imageTitle}</div>
|
||||
)}
|
||||
<div style={{ display: 'flex', fontSize: 40, marginTop: 48, opacity: 0.75 }}>{texts.imageTitle}</div>
|
||||
{agency ? (
|
||||
<div style={{ bottom: 40, display: 'flex', fontSize: 24, opacity: 0.5, position: 'absolute' }}>
|
||||
{texts.imageLine.replace('{agency}', agency.name)}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
),
|
||||
{ headers: { 'Cache-Control': CACHE }, height: 630, width: 1200 },
|
||||
)
|
||||
return image
|
||||
},
|
||||
method: 'get',
|
||||
path: ADMIN_OG_PATH,
|
||||
}
|
||||
|
||||
const icon: Endpoint = {
|
||||
handler: async (req) => {
|
||||
const brand = await loadBranding(req, f)
|
||||
if (brand.icon) {
|
||||
return new Response(null, { headers: { 'Cache-Control': CACHE, Location: absolute(brand.icon, req) }, status: 302 })
|
||||
}
|
||||
const ImageResponse = await imageResponse()
|
||||
return new ImageResponse(
|
||||
(
|
||||
<div style={{ alignItems: 'center', background: '#111111', borderRadius: 12, color: '#ffffff', display: 'flex', fontSize: 30, fontWeight: 700, height: '100%', justifyContent: 'center', width: '100%' }}>
|
||||
{initials(brand.name)}
|
||||
</div>
|
||||
),
|
||||
{ headers: { 'Cache-Control': CACHE }, height: 64, width: 64 },
|
||||
)
|
||||
},
|
||||
method: 'get',
|
||||
path: ADMIN_ICON_PATH,
|
||||
}
|
||||
|
||||
return [og, icon]
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
import type { Config, Payload } from 'payload'
|
||||
|
||||
import type { AdminText } from '../admin-i18n/index.js'
|
||||
|
||||
/**
|
||||
* Admin panel <head> for link previews and search engines: what Slack,
|
||||
* Messenger and similar show for a pasted /admin link, and noindex for robots.
|
||||
*
|
||||
* Payload builds every admin page's metadata from `config.admin.meta` on each
|
||||
* request, so the plugin
|
||||
* - sets static defaults at config time (robots, preview image and icon
|
||||
* pointing at the plugin's own endpoints), and
|
||||
* - fills the site-dependent parts (title suffix, description, siteName)
|
||||
* from SiteSettings on start and after every Site settings save.
|
||||
* Next may load the config more than once per process (dev recompiles, route
|
||||
* bundles), so the site name lives in a process-wide store and the meta reads
|
||||
* it through getters: `openGraph` keeps its object reference through Payload's
|
||||
* sanitize, so its getters run on every request; the top-level `titleSuffix`
|
||||
* and `description` are read when a config is loaded, and the live config is
|
||||
* updated in place as well.
|
||||
* A key the project set in its own `admin.meta` is never touched.
|
||||
*/
|
||||
|
||||
/** Who supports the technology: named in the credit, the description and the preview image. */
|
||||
export type AgencyOption = { name: string; url: string }
|
||||
|
||||
export const DEFAULT_AGENCY: AgencyOption = { name: 'Intecion.com', url: 'https://intecion.com' }
|
||||
|
||||
export type AdminMetaOptions = {
|
||||
/** Description for link previews; `{site}` and `{agency}` are replaced. `false` = Payload's own. */
|
||||
description?: AdminText | false | string
|
||||
/** Favicon of the panel from SiteSettings (`/api/ipal/admin-icon`). `false` = Payload's own. */
|
||||
icons?: boolean
|
||||
/** Preview image: generated from the site's logo (`/api/ipal/admin-og`), your own URL, or `false` = Payload's own. */
|
||||
image?: false | string
|
||||
/** Language of the description and suffix. Default: the site's default locale when it is `pl`, else `en`. */
|
||||
language?: 'en' | 'pl'
|
||||
/** Robots meta. Default noindex, nofollow. `false` = none. */
|
||||
robots?: { follow?: boolean; index?: boolean } | false
|
||||
/** Browser-tab suffix; `{site}` is replaced. Default "— {site} | Panel". `false` = none. */
|
||||
titleSuffix?: false | string
|
||||
}
|
||||
|
||||
export const ADMIN_META_TEXTS = {
|
||||
en: {
|
||||
description: 'Administration panel of {site}. Built on Payload CMS, technology support: {agency}.',
|
||||
descriptionNoAgency: 'Administration panel of {site}. Built on Payload CMS.',
|
||||
imageLine: 'Technology support: {agency}',
|
||||
imageTitle: 'Administration panel',
|
||||
titleSuffix: '— {site} | Panel',
|
||||
titleSuffixNoSite: '| Panel',
|
||||
},
|
||||
pl: {
|
||||
description: 'Panel administracyjny witryny {site}. Zbudowany na Payload CMS, wsparcie technologiczne: {agency}.',
|
||||
descriptionNoAgency: 'Panel administracyjny witryny {site}. Zbudowany na Payload CMS.',
|
||||
imageLine: 'Wsparcie technologiczne: {agency}',
|
||||
imageTitle: 'Panel administracyjny',
|
||||
titleSuffix: '— {site} | Panel',
|
||||
titleSuffixNoSite: '| Panel',
|
||||
},
|
||||
}
|
||||
|
||||
type Store = { site?: string }
|
||||
const STORE_KEY = Symbol.for('ipal.adminMeta')
|
||||
/** Process-wide: the site name the meta getters read. */
|
||||
export const adminMetaStore = (): Store => {
|
||||
const g = globalThis as unknown as Record<symbol, Store | undefined>
|
||||
return (g[STORE_KEY] ??= {})
|
||||
}
|
||||
|
||||
export const ADMIN_OG_PATH = '/ipal/admin-og'
|
||||
export const ADMIN_ICON_PATH = '/ipal/admin-icon'
|
||||
|
||||
type Meta = NonNullable<NonNullable<Config['admin']>['meta']>
|
||||
|
||||
const fill = (template: string, site: string, agency: AgencyOption | false) =>
|
||||
template.replaceAll('{site}', site).replaceAll('{agency}', agency ? agency.name : '').trim()
|
||||
|
||||
export type ResolvedAdminMeta = {
|
||||
agency: AgencyOption | false
|
||||
language: 'en' | 'pl'
|
||||
options: AdminMetaOptions
|
||||
/** Keys the project set itself in admin.meta — never overwritten. */
|
||||
own: { description: boolean; icons: boolean; images: boolean; robots: boolean; siteName: boolean; titleSuffix: boolean }
|
||||
}
|
||||
|
||||
/** Static defaults at config time. Returns the new meta and what the runtime sync may fill. */
|
||||
export function buildAdminMeta({
|
||||
agency,
|
||||
apiRoute = '/api',
|
||||
defaultLocale,
|
||||
meta = {},
|
||||
options = {},
|
||||
}: {
|
||||
agency: AgencyOption | false
|
||||
apiRoute?: string
|
||||
defaultLocale?: string
|
||||
meta?: Meta
|
||||
options?: AdminMetaOptions
|
||||
}): { meta: Meta; resolved: ResolvedAdminMeta } {
|
||||
const og = (meta.openGraph ?? {}) as Record<string, unknown>
|
||||
const own = {
|
||||
description: meta.description !== undefined || og.description !== undefined,
|
||||
icons: meta.icons !== undefined,
|
||||
images: og.images !== undefined,
|
||||
robots: meta.robots !== undefined,
|
||||
siteName: og.siteName !== undefined,
|
||||
titleSuffix: meta.titleSuffix !== undefined,
|
||||
}
|
||||
const language = options.language ?? (defaultLocale?.toLowerCase().startsWith('pl') ? 'pl' : 'en')
|
||||
const api = apiRoute.replace(/\/$/, '')
|
||||
const next: Meta = { ...meta }
|
||||
const openGraph: Record<string, unknown> = { ...og }
|
||||
const resolved: ResolvedAdminMeta = { agency, language, options, own }
|
||||
const current = () => {
|
||||
const site = adminMetaStore().site
|
||||
return site ? adminMetaForSite(site, resolved) : null
|
||||
}
|
||||
const getter = (target: object, key: string, read: () => unknown) =>
|
||||
Object.defineProperty(target, key, { configurable: true, enumerable: true, get: read })
|
||||
|
||||
if (!own.titleSuffix && options.titleSuffix !== false) {
|
||||
getter(next, 'titleSuffix', () => current()?.titleSuffix ?? ADMIN_META_TEXTS[language].titleSuffixNoSite)
|
||||
}
|
||||
if (!own.description && options.description !== false) {
|
||||
getter(next, 'description', () => current()?.description)
|
||||
getter(openGraph, 'description', () => current()?.description)
|
||||
}
|
||||
if (!own.siteName) {getter(openGraph, 'siteName', () => current()?.siteName)}
|
||||
|
||||
if (!own.robots && options.robots !== false) {
|
||||
next.robots = { follow: false, index: false, ...(options.robots ?? {}) } as Meta['robots']
|
||||
}
|
||||
if (!own.images && options.image !== false) {
|
||||
openGraph.images = [{ height: 630, url: options.image ?? `${api}${ADMIN_OG_PATH}`, width: 1200 }]
|
||||
}
|
||||
if (!own.icons && options.icons !== false) {
|
||||
next.icons = [{ rel: 'icon', url: `${api}${ADMIN_ICON_PATH}` }] as Meta['icons']
|
||||
}
|
||||
if (Object.keys(openGraph).length) {next.openGraph = openGraph as Meta['openGraph']}
|
||||
return { meta: next, resolved }
|
||||
}
|
||||
|
||||
/** Site-dependent parts of the meta for a given site name. */
|
||||
export function adminMetaForSite(site: string, { agency, language, options }: ResolvedAdminMeta) {
|
||||
const texts = ADMIN_META_TEXTS[language]
|
||||
const description =
|
||||
options.description === false
|
||||
? undefined
|
||||
: fill(
|
||||
typeof options.description === 'string'
|
||||
? options.description
|
||||
: options.description
|
||||
? options.description[language]
|
||||
: agency
|
||||
? texts.description
|
||||
: texts.descriptionNoAgency,
|
||||
site,
|
||||
agency,
|
||||
)
|
||||
const titleSuffix = options.titleSuffix === false ? undefined : fill(options.titleSuffix ?? texts.titleSuffix, site, agency)
|
||||
return { description, siteName: site, titleSuffix }
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the site-dependent parts into the live config Payload reads for every
|
||||
* admin page. Never throws; skips keys the project set itself.
|
||||
*/
|
||||
export async function syncAdminMeta(
|
||||
payload: Payload,
|
||||
resolved: ResolvedAdminMeta,
|
||||
{ nameField = 'siteName', settingsSlug = 'site-settings' }: { nameField?: string; settingsSlug?: string } = {},
|
||||
): Promise<void> {
|
||||
try {
|
||||
const locale = payload.config.localization ? payload.config.localization.defaultLocale : undefined
|
||||
const settings = (await payload.findGlobal({ slug: settingsSlug as never, depth: 0, ...(locale ? { locale: locale as never } : {}) })) as Record<string, unknown>
|
||||
const raw = settings[nameField]
|
||||
const site = typeof raw === 'string' ? raw.trim() : ''
|
||||
if (!site) {return}
|
||||
adminMetaStore().site = site
|
||||
// The live config was sanitized before this ran: its top-level values are plain copies.
|
||||
const { description, titleSuffix } = adminMetaForSite(site, resolved)
|
||||
const meta = payload.config.admin.meta as (Meta & Record<string, unknown>) | undefined
|
||||
if (!meta) {return}
|
||||
if (!resolved.own.description && resolved.options.description !== false && description) {meta.description = description}
|
||||
if (!resolved.own.titleSuffix && resolved.options.titleSuffix !== false && titleSuffix) {meta.titleSuffix = titleSuffix}
|
||||
} catch (error) {
|
||||
payload.logger.warn({ err: error, msg: '[ipal] admin meta: could not read Site settings; Payload defaults stay.' })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user