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
1 change: 1 addition & 0 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
<link rel="preload" href="assets/fonts/inter-latin.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="assets/fonts/space-grotesk-bold.woff2" as="font" type="font/woff2" crossorigin>
<link rel="preload" href="assets/fonts/ibm-plex-mono-latin-400-normal.woff2" as="font" type="font/woff2" crossorigin>
<script src="theme.js?v=2"></script>
<link rel="stylesheet" href="styles.css?v=6">
<script src="stack.js" defer></script>
<script src="tools.js" defer></script>
<script src="theme.js?v=c4917c18"></script>
<link rel="stylesheet" href="styles.css?v=e58c43af">
<script src="stack.js?v=8b37b17e" defer></script>
<script src="tools.js?v=6083c786" defer></script>
</head>
<body>
<a class="skip-link" href="#tools">Skip to tools</a>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"type": "module",
"scripts": {
"stamp": "node scripts/stamp-assets.mjs",
"test": "playwright test"
},
"devDependencies": {
Expand Down
22 changes: 22 additions & 0 deletions scripts/stamp-assets.mjs
Original file line number Diff line number Diff line change
@@ -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);