Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions frontend/seo/facts.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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: 'יניר רוט',
Expand Down Expand Up @@ -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,
Expand All @@ -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'],
Expand Down
12 changes: 9 additions & 3 deletions frontend/seo/prerender.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -114,7 +115,12 @@ const twins = (route: string): Record<Lang, string> => ({

const DIR: Record<Lang, 'rtl' | 'ltr'> = { he: 'rtl', en: 'ltr' };

const SHARE_CARD: Record<Lang, string> = { 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<Lang, string> = {
he: versionedPublic('/og-image.png'),
en: versionedPublic('/og-image-en.png'),
};

// Copy the prerendered pages need beyond the documents themselves.
const COPY = {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
42 changes: 42 additions & 0 deletions frontend/seo/versioned.mts
Original file line number Diff line number Diff line change
@@ -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=<hash of what it shows>`: 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)}`;
};
7 changes: 7 additions & 0 deletions frontend/vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -428,6 +429,12 @@ export default defineConfig(({ mode, command }) => {
2
).replace(/</g, '\\u003c')}\n</script>`
)
// 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),
},
},
Expand Down
Loading