Skip to content

Automatic static file cache busting #3832

Closed

Description

@SandPod

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:

  1. Generate a hash (e.g., SHA-1) for each file registered by a static route.
  2. 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.
  3. Load manifest or compute hash map on server start.
  4. Integrate middleware that rewrites hashed asset requests to original paths.
  5. 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. Provide getStaticAsset(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

Activity

  1. added
    enhancement
    area: web server
    on Jul 31, 2025
  2. moved this from Inbox 📫 to Backlog 🗂️ in Serverpod Frameworkson Jul 31, 2025
  3. changed the title [-]Automatic content hash cache busting[/-] [+]Automatic static file cache busting[/+] on Jul 31, 2025
  4. SandPod commented on Aug 21, 2025

    @SandPod
    ContributorAuthor

    One possible solution that could be explores is to use the Etag header for all static content served. This will not limit the amount of requests but could work as a poor mans content hashing mechanism.

  5. SandPod commented on Aug 21, 2025

    @SandPod
    ContributorAuthor
  6. SandPod commented on Sep 9, 2025

    @SandPod
    ContributorAuthor

    @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:

    1. Compute a hash of the static file if not already computed.
      • Results are cached and reused for performance.
      • Cache entries must be invalidated when the file changes, so updates appear without a server restart.
    2. Append the hash to the filename, for example: logo.png-> logo.abcdef123.png.
    3. Return the new URL that can be used in templates.

    The assetUrl can be passed in as a variable to the templates.

    Additional considerations

    • Static directories
      The assetUrl method should only operate on directories that the server is configured to serve. This configuration should be set at server startup (similar to Django’s STATICFILES_DIRS) and integrated with static_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

    1. Strip the hash portion of the filename to resolve the original file on disk.
      Example:

      /static/images/logo.abcdef123.png → /static/images/logo.png
      
    2. Serve the corresponding file from the configured static directories.

    Additional consideration

    • The stripping of the static file hash could be wrong, if no file is found when the hash is striped we should probably retry to find the file without stripping the hash.
  7. nicowalter256 commented on Sep 9, 2025

    @nicowalter256
    Contributor

    Sure, I’ll give this one a try. If I hit any roadblocks, I’ll reach out for guidance.

  8. moved this from Backlog 🗂️ to In Progress 🔥 in Serverpod Frameworkson Sep 9, 2025
  9. nicowalter256 commented on Sep 10, 2025

    @nicowalter256
    Contributor

    check this PR #3969 @SandPod

  10. SandPod commented on Sep 10, 2025

    @SandPod
    ContributorAuthor

    Awesome 🎉

    @nielsenko will take a first look; he is currently improving that area of Serverpod.

  11. moved this from In Progress 🔥 to Planned 🔭 in Serverpod Frameworkson Sep 12, 2025
  12. moved this from Planned 🔭 to Backlog 🗂️ in Serverpod Frameworkson Sep 12, 2025
  13. moved this from Backlog 🗂️ to Done 🚀 in Serverpod Frameworkson Oct 24, 2025
  14. self-assigned this
    on Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: web serverenhancement

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions