Repository navigation
Description
Problem to Solve
Static assets (e.g., JS, CSS, images) are commonly cached aggressively by browsers to improve performance. However, without cache busting, updated assets may not be reloaded, leading to inconsistent or broken user experiences. Manual cache management is error-prone and not scalable.
Proposal
Add automatic cache busting to Serverpod's web server for static assets.
Key features:
- Generate a hash (e.g., SHA-1) for each file registered by a static route.
- Support two modes:
- Production: Load precomputed manifest (JSON) with filename-to-hash mappings.
- Development: Monitor
static/directory and compute hashes in memory on the fly.
- Load manifest or compute hash map on server start.
- Integrate middleware that rewrites hashed asset requests to original paths.
- Update templating engine to automatically replace asset references with hashed filenames (e.g.,
/style.css→/style.a5f234.css). Either by:
a. Require absolute paths in templates for reliable rewriting.
b. ProvidegetStaticAsset(path)or similar utility to abstract hash mapping.
Configuration should allow switching between manifest and runtime hashing.
Use Case
A developer builds and deploys a Flutter web app with Serverpod. The hashed filenames ensure users always receive the latest assets, while still benefiting from browser caching. In development, changes to static files are reflected immediately without requiring a manual build step.
Alternatives
- Manual versioning of asset paths in templates
- Query param-based cache busting (e.g.,
example.png?v=1) — less reliable and less compatible with CDNs - External asset pipeline and CDN upload for handling cache busting
Additional context
This is a common feature in modern web frameworks.
How experienced are you with this library?
None
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
[-]Automatic content hash cache busting[/-][+]Automatic static file cache busting[/+]SandPod commentedon Aug 21, 2025
One possible solution that could be explores is to use the
Etagheader for all static content served. This will not limit the amount of requests but could work as a poor mans content hashing mechanism.SandPod commentedon Aug 21, 2025
Cache busting recommendations: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#cache_busting
SandPod commentedon Sep 9, 2025
@nicowalter256 - Do you think you could solve this one?
A simplification can be made here so that we don't need to have all hashes computed at Server start.
Static Asset Cache Busting in Serverpod
To support cache busting of static assets (images, CSS, JS, etc.), we can introduce a helper function called
assetUrl(...). This function generates versioned URLs for static files based on their content hash, ensuring browsers always fetch the latest version after a change.Building Templates
The
assetUrl(...)function should:logo.png->logo.abcdef123.png.The
assetUrlcan be passed in as a variable to the templates.Additional considerations
Static directories
The
assetUrlmethod should only operate on directories that the server is configured to serve. This configuration should be set at server startup (similar to Django’sSTATICFILES_DIRS) and integrated withstatic_directory.dart.CDN support
It should be possible to configure a CDN URL prefix for serving static assets. If configured,
assetUrl(...)should return the CDN path instead of the local path. For example:logo.png->https://mycdn.com/static/logo.abcdef123.png.Serving static files
Strip the hash portion of the filename to resolve the original file on disk.
Example:
Serve the corresponding file from the configured static directories.
Additional consideration
nicowalter256 commentedon Sep 9, 2025
Sure, I’ll give this one a try. If I hit any roadblocks, I’ll reach out for guidance.
nicowalter256 commentedon Sep 10, 2025
check this PR #3969 @SandPod
SandPod commentedon Sep 10, 2025
Awesome 🎉
@nielsenko will take a first look; he is currently improving that area of Serverpod.