Skip to content

Commit 4c704e4

Browse files
committed
web: deterministic cache busting via commit-based version tag; generate pkg/env.js at build; update index.html and docs; remove dead env.js
1 parent 55ce01b commit 4c704e4

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ This repo is configured to deploy via Cloudflare Workers; cache-control headers
7979
- Build populates `dist/` with only production runtime files: `index.html` and `pkg/{app_web.js, app_web_bg.wasm, env.js}`
8080
- Worker sets cache-control headers for `.wasm`/`.js`/HTML
8181

82+
#### Build cache-busting
83+
84+
- The build generates `pkg/env.js` with a `version` derived from the current git commit (short SHA, with CI env fallbacks). `index.html` appends `?v=<version>` to the dynamic import of `app_web.js` to ensure deterministic cache busting across deploys.
85+
8286
Controls in browser:
8387

8488
- Click Start to initialize audio (canvas click also works)

env.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ <h3>Modes (1..7)</h3>
240240
Try Chrome 113+ or Edge 113+ with WebGPU enabled.
241241
</div>
242242
<script type="module">
243-
const v = Date.now();
243+
import { version as versionTag } from "env";
244+
const v = versionTag || Date.now();
244245
console.log("[index] dynamic import cache-bust tag:", v);
245246
if (!("gpu" in navigator)) {
246247
const el = document.getElementById("no-webgpu");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Web (WASM): uses `wasm-pack` to build and a static `index.html` to run",
55
"main": "index.js",
66
"scripts": {
7-
"build": "wasm-pack build --target web --out-dir pkg --out-name app_web --release && cp env.js pkg/env.js && rm -rf dist && mkdir -p dist/pkg && cp pkg/app_web.js dist/pkg/ && cp pkg/app_web_bg.wasm dist/pkg/ && cp env.js dist/pkg/env.js && cp index.html dist/index.html && cp favicon.svg dist/favicon.svg",
7+
"build": "wasm-pack build --target web --out-dir pkg --out-name app_web --release && node scripts/gen-env.js && rm -rf dist && mkdir -p dist/pkg && cp pkg/app_web.js dist/pkg/ && cp pkg/app_web_bg.wasm dist/pkg/ && cp pkg/env.js dist/pkg/env.js && cp index.html dist/index.html && cp favicon.svg dist/favicon.svg",
88
"serve": "http-server -p 8080 -c-1 .",
99
"dev": "npm run build && (http-server -p 8080 -c-1 . & SERVER_PID=$!; npm run open; wait $SERVER_PID)",
1010
"open": "open http://localhost:8080",

scripts/gen-env.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* eslint-disable no-console */
2+
const fs = require("fs");
3+
const path = require("path");
4+
const { execSync } = require("child_process");
5+
6+
function shortSha(sha) {
7+
if (!sha || typeof sha !== "string") return null;
8+
const cleaned = sha.trim();
9+
if (!cleaned) return null;
10+
return cleaned.slice(0, 8);
11+
}
12+
13+
function getVersionTag() {
14+
const envCandidates = [
15+
process.env.GIT_COMMIT,
16+
process.env.COMMIT,
17+
process.env.SOURCE_COMMIT,
18+
process.env.VERCEL_GIT_COMMIT_SHA,
19+
process.env.GITHUB_SHA,
20+
process.env.CF_PAGES_COMMIT_SHA,
21+
];
22+
for (const candidate of envCandidates) {
23+
const s = shortSha(candidate);
24+
if (s) return s;
25+
}
26+
27+
try {
28+
const sha = execSync("git rev-parse --short HEAD", {
29+
stdio: ["ignore", "pipe", "ignore"],
30+
})
31+
.toString()
32+
.trim();
33+
if (sha) return sha;
34+
} catch (_) {
35+
// ignore
36+
}
37+
38+
// Final fallback: time-based tag, stable within the build
39+
return `dev-${Math.floor(Date.now() / 1000).toString(36)}`;
40+
}
41+
42+
function ensureDir(dirPath) {
43+
if (!fs.existsSync(dirPath)) {
44+
fs.mkdirSync(dirPath, { recursive: true });
45+
}
46+
}
47+
48+
function writeEnvJs(targetPath, version) {
49+
const contents = `// Auto-generated by scripts/gen-env.js
50+
export const version = "${version}";
51+
export function now() {
52+
if (typeof performance !== "undefined" && typeof performance.now === "function") {
53+
return performance.now();
54+
}
55+
return Date.now();
56+
}
57+
export default { version, now };
58+
`;
59+
fs.writeFileSync(targetPath, contents, "utf8");
60+
}
61+
62+
function main() {
63+
const repoRoot = path.resolve(__dirname, "..");
64+
const pkgDir = path.join(repoRoot, "pkg");
65+
ensureDir(pkgDir);
66+
67+
const version = getVersionTag();
68+
const target = path.join(pkgDir, "env.js");
69+
writeEnvJs(target, version);
70+
console.log(
71+
`[gen-env] wrote ${path.relative(repoRoot, target)} with version: ${version}`
72+
);
73+
}
74+
75+
main();

0 commit comments

Comments
 (0)