Skip to content

Templates: Version asset links so updates bypass the browser cache - #64

Merged
whereareiam merged 1 commit into
devfrom
fix/template-asset-cache-busting
Oct 3, 2026
Merged

whereareiam merged 1 commit into
devfrom
fix/template-asset-cache-busting

Conversation

@whereareiam

Copy link
Copy Markdown
Owner

After a theme update, browsers keep running the previous build's scripts against the new templates. Forgejo serves custom assets with Cache-Control: private, max-age=21600, and the templates link them by a fixed path, so nothing tells the browser the file changed.

This showed up as "Could not load the file tree. Reload to try again." on file pages after moving from 0.2.0 to dev. The cached 0.2.0 repo-source-browser.js still read data-api-url, which #60 removed from source_tree.tmpl, so it requested …/src/branch/dev/undefined&page=1 and got a 404. A hard reload clears it; otherwise it lasts up to six hours per visitor.

Changes

  • The assemble step appends each linked stylesheet's and script's content hash as a ?v= query in dist/forgejo/templates. A link only changes when its file does. Source templates are unchanged.
  • The build fails when a template links a stylesheet or script it does not ship.
  • That check surfaced three stale links in base/head_navbar.tmpl (css/user-profile.css, css/profile-subpages.css, css/organization.css), which 404 on profile and organization pages. base/component_assets.tmpl already loads the same files from css/components/pages/, so they are removed.

Not covered

The theme-github-*.css files are linked by Forgejo's own head template with Forgejo's version as the query, so they can still be served stale for up to six hours after a theme update.

Checks

bun commit passes (383 tests), including a new test that every template asset link carries the hash of the shipped file. No visual change, so there are no before/after screenshots.

ref #60

🤖 Generated with Claude Code

Forgejo serves custom assets with a six-hour max-age and the templates
linked them by a fixed path, so after a theme update browsers kept
running the previous build's scripts against the new markup. The source
tree then failed with "Could not load the file tree" because the cached
script still requested the removed data-api-url.

The assemble step now appends each linked stylesheet and script's
content hash as a version query, and fails when a template links an
asset the build does not ship. That check surfaced three stale
stylesheet links in the navbar, which duplicated component_assets and
are removed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@whereareiam whereareiam added the bug Something isn't working label Oct 3, 2026
@whereareiam
whereareiam merged commit 0f7e0f8 into dev Oct 3, 2026
8 of 9 checks passed
@whereareiam
whereareiam deleted the fix/template-asset-cache-busting branch October 3, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant