diff --git a/frontend/seo/facts.mts b/frontend/seo/facts.mts index 4579cf7..a127b30 100644 --- a/frontend/seo/facts.mts +++ b/frontend/seo/facts.mts @@ -17,6 +17,8 @@ * - no email address here: see the comment in index.html */ +import { versionedPublic } from './versioned.mjs'; + export const PERSON = { name: 'Yanir Rot', nameHe: 'יניר רוט', @@ -107,7 +109,7 @@ export const siteGraph = (siteUrl: string) => { name: PERSON.name, alternateName: PERSON.nameHe, url: `${siteUrl}/`, - image: `${siteUrl}/og-image.png`, + image: `${siteUrl}${versionedPublic('/og-image.png')}`, jobTitle: PERSON.jobTitle, description: PERSON.description, knowsAbout: PERSON.knowsAbout, @@ -124,7 +126,7 @@ export const siteGraph = (siteUrl: string) => { name: `${PERSON.name} - ${PERSON.tagline}`, alternateName: `${PERSON.nameHe} - ${PERSON.taglineHe}`, url: `${siteUrl}/`, - image: `${siteUrl}/og-image.png`, + image: `${siteUrl}${versionedPublic('/og-image.png')}`, founder: { '@id': person }, areaServed: { '@type': 'Country', name: 'Israel' }, availableLanguage: ['Hebrew', 'English'], diff --git a/frontend/seo/prerender.mts b/frontend/seo/prerender.mts index 5db8d44..68a3f82 100644 --- a/frontend/seo/prerender.mts +++ b/frontend/seo/prerender.mts @@ -49,6 +49,7 @@ import { writingCard, type ShareCard, } from './share.mjs'; +import { versionedCard, versionedPublic } from './versioned.mjs'; // Section headings that are metadata rather than prose, per docs_helper.py. // PROJECT_METADATA_SECTIONS and WRITING_SECTIONS there; keep in step. @@ -114,7 +115,12 @@ const twins = (route: string): Record => ({ const DIR: Record = { he: 'rtl', en: 'ltr' }; -const SHARE_CARD: Record = { he: '/og-image.png', en: '/og-image-en.png' }; +// The home card in each language, linked by content so a redrawn card is +// fetched again; see versioned.mts. +const SHARE_CARD: Record = { + he: versionedPublic('/og-image.png'), + en: versionedPublic('/og-image-en.png'), +}; // Copy the prerendered pages need beyond the documents themselves. const COPY = { @@ -354,7 +360,7 @@ export const buildPages = (docsRoot: string, siteUrl: string): Page[] => { page(route, { title: w.title, description, - image: `/${share.file}`, + image: versionedCard(`/${share.file}`, share, share.image), card: true, share, ogType: 'article', @@ -487,7 +493,7 @@ export const buildPages = (docsRoot: string, siteUrl: string): Page[] => { page(route, { title: p.title, description, - image: `/${share.file}`, + image: versionedCard(`/${share.file}`, share, share.image), card: true, share, ogType: 'article', diff --git a/frontend/seo/versioned.mts b/frontend/seo/versioned.mts new file mode 100644 index 0000000..4c218ea --- /dev/null +++ b/frontend/seo/versioned.mts @@ -0,0 +1,42 @@ +/** + * Share images with their content in the address. + * + * LinkedIn, Facebook and WhatsApp keep an image they fetched under its URL + * for days, and a page they re-read does not make them fetch the image again. + * When the home card was redrawn under the same /og-image.png, LinkedIn went + * on showing the old drawing next to the new title. So every share image is + * linked as `/file.png?v=`: a changed image is a new + * address, and an unchanged one keeps its address and its cache. + */ + +import { createHash } from 'node:crypto'; +import { existsSync, readFileSync } from 'node:fs'; +import { fileURLToPath } from 'node:url'; +import path from 'node:path'; + +const FRONTEND = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); +const PUBLIC = path.join(FRONTEND, 'public'); + +const hash = (...parts: (string | Buffer)[]) => { + const h = createHash('sha256'); + for (const p of parts) h.update(p); + return h.digest('hex').slice(0, 10); +}; + +/** A file in public/, by its site path, with a version from its bytes. */ +export const versionedPublic = (sitePath: string) => { + const file = path.join(PUBLIC, sitePath); + return existsSync(file) ? `${sitePath}?v=${hash(readFileSync(file))}` : sitePath; +}; + +/** + * A share card that is drawn after the build (scripts/share-cards.mjs), so + * its bytes do not exist yet: the version comes from everything that decides + * them - what the card says, the picture on it and the script that draws it. + */ +export const versionedCard = (sitePath: string, card: object, image?: string) => { + const script = readFileSync(path.join(FRONTEND, 'scripts', 'share-cards.mjs')); + const picture = + image && existsSync(path.join(PUBLIC, image)) ? readFileSync(path.join(PUBLIC, image)) : ''; + return `${sitePath}?v=${hash(JSON.stringify(card), picture, script)}`; +}; diff --git a/frontend/vite.config.mts b/frontend/vite.config.mts index 60afbf7..f41df98 100644 --- a/frontend/vite.config.mts +++ b/frontend/vite.config.mts @@ -12,6 +12,7 @@ import { llmsTxt, } from './seo/prerender.mjs'; import { siteGraph } from './seo/facts.mjs'; +import { versionedPublic } from './seo/versioned.mjs'; import { mkdirSync, readFileSync, writeFileSync } from 'fs'; const __dirname = path.dirname(fileURLToPath(import.meta.url)); @@ -428,6 +429,12 @@ export default defineConfig(({ mode, command }) => { 2 ).replace(/` ) + // The home card by content, so a redrawn card is a new + // address to LinkedIn and WhatsApp; see seo/versioned.mts. + .replaceAll( + '%VITE_SITE_URL%/og-image.png', + `%VITE_SITE_URL%${versionedPublic('/og-image.png')}` + ) .replaceAll('%VITE_SITE_URL%', resolvedSiteUrl), }, },