Fail the build when a page's og:image file doesn't exist
For 14 days, three of Coparent Math’s state calculators pointed at share images that didn’t exist, and CI stayed green the whole time. A link to those pages would have shared without a picture. The build now checks every og:image and twitter:image in the built HTML against the files in dist/, and fails when one is missing.
The cause was two lists. The script that draws the share cards kept its own list of states:
const STATES = [['virginia', 'Virginia'], ['texas', 'Texas'], ['new-york', 'New York']]
On 3 November I added Alabama, Florida and Georgia to the calculator. Every calculator page names its card from its own slug, /og/calculator-${slug}.png, so the three new pages pointed at three new cards. Nobody told the card script. It went on drawing the original three.
I found it on 17 November, while adding an SEO audit that happens to check share images. On the live site, Alabama, Florida and Georgia answered 404. Virginia answered 200.
One list
First, drop the second list. The card script now reads the states from the calculator’s engine, the same record the pages are built from. Drawing every card again left the existing ones byte for byte the same, so pages that already shared well didn’t change.
One check
Then make the build notice next time. check-site.mjs already walked every page in dist/ for links and titles. It now collects each page’s share images on the site’s own host and looks for the file:
const cards = new Set([...s.matchAll(/<meta (?:property="og:image"|name="twitter:image") content="https:\/\/(?:www\.)?coparentmath\.com(\/[^"]*)"/g)].map((m) => m[1]))
for (const card of cards) if (!existsSync(join(dist, decodeURIComponent(card)))) problems.push(`${url}: share image ${card} is not in the build (run node scripts/og.mjs)`)
The Set is there because each page names its card twice, once for Open Graph and once for X. My first version reported every missing card twice: six problems for three cards.
Run on the build from before the fix, it found exactly the three:
/calculator/alabama/: share image /og/calculator-alabama.png is not in the build (run node scripts/og.mjs)
/calculator/florida/: share image /og/calculator-florida.png is not in the build (run node scripts/og.mjs)
/calculator/georgia/: share image /og/calculator-georgia.png is not in the build (run node scripts/og.mjs)
3 problem(s) in 100 pages.
After the fix the same check passed on all 100 pages, and so did CI. About 90 seconds after the deploy, the Alabama, Florida and Georgia cards answered 200, next to the three that always had.
It only looks at images on the site’s own host. A card on another domain would need a network request, and I’d rather a build check didn’t depend on one.