Cache-bust app CSS/JS so CF's 4h browser cache can't serve stale code#47
Merged
Conversation
… code
The origin sends Cache-Control: no-cache on /assets/*, but Cloudflare
overrides it to max-age=14400 and caches the css/js — so browsers held
old code for up to 4h ("I deployed but still see the old UI"). The HTML
is no-store (always fresh), so append a content-version query
(?v=<newest mtime>) to the app's own css/js URLs: a fresh page now
always points at an asset URL the browser hasn't cached. Vendored
xterm.* are left cacheable. The asset route already ignores the query
(serves by filename), so no route change needed.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds cache-busting for dashboard-served CSS/JS by appending a version query param derived from asset mtimes, preventing stale CDN/browser caches after deploys.
Changes:
- Compute an asset version string from the newest mtime among a fixed list of “versioned” assets.
- Post-process the generated dashboard HTML to append
?v=...to matching/assets/...URLs. - Refactor
dashboard()to build HTML, then apply cache-busting.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+35
to
+46
| fn asset_ver(config: &Config) -> String { | ||
| let dir = config.root_dir.join("public"); | ||
| let mut newest: u64 = 0; | ||
| for name in VERSIONED_ASSETS { | ||
| if let Ok(t) = std::fs::metadata(dir.join(name)).and_then(|m| m.modified()) { | ||
| if let Ok(d) = t.duration_since(std::time::UNIX_EPOCH) { | ||
| newest = newest.max(d.as_secs()); | ||
| } | ||
| } | ||
| } | ||
| newest.to_string() | ||
| } |
Comment on lines
+19
to
+20
| let v = asset_ver(config); | ||
| let html = format!( |
Comment on lines
+48
to
54
| fn bust_assets(html: &str, v: &str) -> String { | ||
| let mut out = html.to_string(); | ||
| for name in VERSIONED_ASSETS { | ||
| out = out.replace(&format!("/assets/{name}\""), &format!("/assets/{name}?v={v}\"")); | ||
| } | ||
| out | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug (why "I deployed but still see the old UI")
Cache-Control: no-cacheon/assets/*(meant to be always-fresh for hot-reload).max-age=14400(4h) and caches the css/js at the edge → browsers hold the oldapp.css/render.jsfor up to 4 hours.no-store(always fresh) — confirmed via curl.Fix
Append a content-version query to the app's own css/js URLs:
app.css?v=<newest mtime>. Since the HTML is always fresh, a reload now points at an asset URL the browser has never cached → guaranteed fresh css/js. Version = newest mtime among the app's own files, so it auto-bumps on every deploy and every hot-reloaded CSS edit.xterm.*left cacheable (they never change).Verified live
/assets/app.css?v=1780690125; the versioned URL returns200with the new CSS.no-store.🤖 Generated with Claude Code