From fd6d1464112cc59aed406d6da73e48e352e48245 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Thu, 3 Dec 2026 04:04:12 +0000 Subject: [PATCH] Stamp the stylesheet and scripts with their content, so a deploy never pairs new HTML with old files index.html linked styles.css and theme.js with hand-bumped versions and stack.js and tools.js with none, so after a deploy a browser could keep an old copy and show a broken page until a hard refresh. npm run stamp now writes a content hash into each link, and CI fails when one is out of date. --- .github/workflows/website.yml | 1 + README.md | 4 ++++ index.html | 8 ++++---- package.json | 1 + scripts/stamp-assets.mjs | 22 ++++++++++++++++++++++ 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 scripts/stamp-assets.mjs diff --git a/.github/workflows/website.yml b/.github/workflows/website.yml index 4aab48e..9b757b1 100644 --- a/.github/workflows/website.yml +++ b/.github/workflows/website.yml @@ -24,5 +24,6 @@ jobs: cache: npm - run: npm ci - run: node --check theme.js + - run: node scripts/stamp-assets.mjs --check - run: npx playwright install --with-deps chromium - run: npm test diff --git a/README.md b/README.md index 3b7d281..0630153 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,10 @@ npx playwright install chromium npm test ``` +After changing `styles.css` or a script, run `npm run stamp`. It gives the file a new content +stamp in `index.html`, so browsers fetch it again instead of reusing the copy from before the +deploy. CI fails when a stamp is out of date. + `CHROMIUM_EXECUTABLE_PATH` can point to an existing Chromium installation instead of downloading one. The test server starts and stops automatically. GitHub Actions runs the same checks without any publishing or deployment step. diff --git a/index.html b/index.html index 6237cc3..9b5849c 100644 --- a/index.html +++ b/index.html @@ -27,10 +27,10 @@ - - - - + + + +
Skip to tools diff --git a/package.json b/package.json index f941af3..74a48ad 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "private": true, "type": "module", "scripts": { + "stamp": "node scripts/stamp-assets.mjs", "test": "playwright test" }, "devDependencies": { diff --git a/scripts/stamp-assets.mjs b/scripts/stamp-assets.mjs new file mode 100644 index 0000000..82cffdc --- /dev/null +++ b/scripts/stamp-assets.mjs @@ -0,0 +1,22 @@ +// Stamps each stylesheet and script that index.html loads with a hash of its content, such as +// styles.css?v=3f2a9c1e. Their names don't change between deploys, so without it a browser can pair +// a new index.html with a stylesheet or script it cached before the deploy. +// +// node scripts/stamp-assets.mjs update the stamps in index.html +// node scripts/stamp-assets.mjs --check fail if a stamp is out of date (CI) +import { createHash } from 'node:crypto'; +import { readFileSync, writeFileSync } from 'node:fs'; + +const page = new URL('../index.html', import.meta.url); +const html = readFileSync(page, 'utf8'); +const stamped = html.replace(/(\s(?:href|src)=")([\w./-]+\.(?:css|js))(?:\?v=[^"]*)?(?=")/g, (_, before, file) => { + const hash = createHash('sha256').update(readFileSync(new URL(`../${file}`, import.meta.url))).digest('hex').slice(0, 8); + return `${before}${file}?v=${hash}`; +}); + +if (stamped === html) process.exit(0); +if (process.argv.includes('--check')) { + console.error('index.html links a stylesheet or script with an out-of-date stamp. Run: npm run stamp'); + process.exit(1); +} +writeFileSync(page, stamped);