Files
ipal-kit/src/modules/seo/validateFavicon.ts
T
Radosław SmolińskiandClaude Fable 5.2 0a4f001797 IAPL rename, scripted: preferred names in src, bin, scripts, services, tests; bin files and skill folders moved
Replaced by an explicit list of forms only (ten exported names, runtime env names, project file
names and marks, commands, skill names, the log prefix, the brand in prose). Package name, import
paths, routes, CSS tokens, data attributes, custom keys, context flags, the cookie and kv keys are
not touched. package.json keeps the five old bin names beside the five new ones.
The old names stop working in this commit and come back as aliases in the next; docs follow.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 17:24:21 +01:00

39 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { FieldHook } from 'payload'
import { faviconProblems } from './faviconProblems.js'
/**
* Server-side check of the favicon upload: Google rejects favicons under 48×48,
* and a non-square icon gets cropped. Logs a warning on save; it never blocks
* the save. Editors see the same check under the field (`FaviconCheck`, /admin).
*
* Attach as a beforeValidate hook on your own favicon field; the plugin wires it
* on Site settings itself.
*
* Note: dimensions come from the related Media doc (Payload stores width/height
* for image uploads), so this checks the resolved upload, not the raw file.
*/
export const validateFaviconField: FieldHook = async ({ req, value }) => {
if (!value) {return value} // no favicon set → nothing to validate (optional field)
try {
const media = await req.payload.findByID({
id: typeof value === 'object' ? (value as { id: string }).id : value,
collection: 'media',
depth: 0,
})
for (const { height, key, width } of faviconProblems(media as { height?: number; mimeType?: string; width?: number })) {
req.payload.logger.warn(
key === 'faviconTooSmall'
? `[iapl] Favicon is ${width}×${height}px. Google requires ≥48×48 to display it in search results. Upload a larger square icon (96 or 192px).`
: `[iapl] Favicon is not square (${width}×${height}). Use a square icon so it isn't cropped in the browser tab or search results.`,
)
}
} catch {
// Media lookup failed — don't block the save over a validation warning.
}
return value
}