Skip to content

Asset cache busting for web distribution #21730

@eero-lehtinen

Description

@eero-lehtinen

What problem does this solve or what need does it fill?

Currently there seems to be no cache busting (for the browser internal cache) implemented for web asset fetching, which can cause outdated assets to be used after updating them on a hosting service. This might not be critical for textures, but if assets are used for configuration and such, it can be really bad if they are outdated.

What solution would you like?

File fingerprinting

The canonical way seems to be to add a hash fingerprint of the contents of each file to its filename. E.g. SvelteKit distribution build transforms index.js to index.C2oOKq9x.js. This allows hosting services to set a really long max-age for the asset while still never serving an outdated file. The hash could also alternatively be added as a query string to the fetch URL, e.g. https://bevy.com/assets/logo.png?C2oOKq9x. Then files wouldn't need to be renamed, but the asset reader would just need to add the correct hash to the query string for each file. The browser would then understand when it can and cannot rely on its cache.

This solution can be somewhat complicated, as the hash generation would probably need to be a build script. The most simple solution would probably be to make the HttpWasmAssetReader accept a mapping of filenames to hashes (which would be added to the query string). The developer would then need to generate and supply the mapping themselves.

Versioning

Another simpler, but less efficient way would be to add a game build version string to the query. E.g. https://bevy.com/assets/logo.png?v=0.9.2. Then the latest version of the file would be fetched every time the main package version of the game is updated. This is worse than fingerprinting, because all assets are re-fetched even if they didn't change, but really easy to implement.

What alternative(s) have you considered?

There are hosting tricks that don't require any changes to the engine, but are nonoptimal:

  • Disable caching (very inefficient)
  • Set a low max-age, e.g. 1 hour and setup ETag headers to use fingerprints
    • Avoids re-downloading assets, but still needs a network round trip to check each file for changes hourly
    • Has an hour window where the assets can be outdated (we want to prevent this)
    • This is what itch.io does

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AssetsLoad files from disk to use for things like images, models, and soundsC-FeatureA new feature, making something new possibleD-ComplexQuite challenging from either a design or technical perspective. Ask for help!O-WebSpecific to web (WASM) buildsS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions