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 }