Skip to content

web: fix static-file caching & root redirects - #3656

Merged
GuillaumeGomez merged 2 commits into
rust-lang:mainfrom
syphar:static-files-hash
Nov 2, 2026
Merged

GuillaumeGomez merged 2 commits into
rust-lang:mainfrom
syphar:static-files-hash

Conversation

@syphar

@syphar syphar commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

When deploying #3647 I realized that there is a problem with caching certain static assets.

Generally (before this PR):

  • we used /-/static/asset.css?build-slug=12345 as URL
  • our build_static_router did set caching headers in a way that the CDN & the browser can cache forever.
  • means we get a new cache entry when the version changes

This worked, but only for all the places where we actually added the build-slug.

We forgot that in

  • /-/static/chartjs/chart.min.css
  • /-/static/chartjs/chart.min.js
  • /-/static/trigger-rebuild.png
  • /opensearch.xml and /-/static/opensearch.xml
  • /robots.txt and /-/static/robots.txt
  • /favicon.ico and /-/static/favicon.ico

that means when we would want to update any of these, we could only purge our CDN, but user browsers would have old versions.

new approach

The new approach here is:

  • /-/static/{path} will serve static files with CDN only caching. It uses a surrogate key we invalidate on each deploy automatically
  • we will generate a hash over all static assets. this keeps relative links in CSS files alive, and still improves cache ratio a little.
  • /-/static/v/{all-assets-hash}/{path} will serve the same files, but _with different caching headers (cache forever in CDN & browser)
  • the root redirects (like /robots.txt) will be changed to be temporary redirects, with cdn-only caching.

I tried to convert all places to using the assets-hash URL, but even when I forgot a place, it won't break. Worst case is cdn-only caching.

Nice side-effect: if we use relative @url or other references in CSS, the browser will request the versioned URL when the CSS file was referenced with version. In one CSS file I changed absolute paths to relative ones because of that.

alternatives

The classic alternative to this approach is to include content-hashes in the filenames. This would improve cache ratio even more, but is quite a bit of work, especially rewriting the CSS links.

@syphar syphar self-assigned this Oct 30, 2026
@github-actions github-actions Bot added the S-waiting-on-review Status: This pull request has been implemented and needs to be reviewed label Oct 30, 2026
@syphar
syphar marked this pull request as ready for review October 30, 2026 20:59
@syphar
syphar requested a review from a team as a code owner October 30, 2026 20:59
@syphar
syphar force-pushed the static-files-hash branch from fa2fc72 to 2d692c1 Compare October 30, 2026 21:58
@GuillaumeGomez
GuillaumeGomez merged commit 32ece19 into rust-lang:main Nov 2, 2026
12 checks passed
@GuillaumeGomez
GuillaumeGomez deleted the static-files-hash branch November 2, 2026 10:23
@github-actions github-actions Bot added S-waiting-on-deploy This PR is ready to be merged, but is waiting for an admin to have time to deploy it and removed S-waiting-on-review Status: This pull request has been implemented and needs to be reviewed labels Nov 2, 2026
@syphar syphar removed the S-waiting-on-deploy This PR is ready to be merged, but is waiting for an admin to have time to deploy it label Nov 2, 2026

This branch was previously deployed

1 inactive deployment
github-pages — 2d692c16 Deployed Nov 2, 2026 by GuillaumeGomez via deploy #876
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants