Skip to content

Conversation

@eero-lehtinen
Copy link
Contributor

@eero-lehtinen eero-lehtinen commented Nov 3, 2025

Objective

Allow post processing wasm asset request URLs to make it possible to add query strings.

Solution

The first steps to fixing #21730

Add a new property request_mapper to HttpWasmAssetReader. I made it dyn to not add needless generics, but I guess static dispatch might work too.

Some mapping could be added to the default asset loader too, but I figured there is no consensus on what to do there. I just wanted to add this to allow motivated users to implement some kind of cache busting manually.

Testing

Added this to the custom_asset_reader example and ran it on a web browser.

impl Plugin for CustomAssetReaderPlugin {
    fn build(&self, app: &mut App) {
        let mut file_hashes = HashMap::new();
        file_hashes.insert("assets/branding/icon.png", "c09n2309jv");
        let file_hashes = Arc::new(file_hashes);
        app.register_asset_source(
            AssetSourceId::Default,
            AssetSource::build().with_reader(move || {
                Box::new(HttpWasmAssetReader::new("assets").with_request_mapper({
                    let file_hashes = file_hashes.clone();
                    move |path| {
                        let mapped = file_hashes.get(path).map_or_else(
                            || Cow::Borrowed(path),
                            |hash| Cow::Owned([path, "?", hash].concat()),
                        );
                        info!("Mapped asset path '{}' to '{}'", path, mapped);
                        mapped
                    }
                }))
            }),
        );
    }
}

The alternative, more straight forward, and slightly less efficient way to do cache busting would be to just add the current game version to the query string. Maybe we could add a premade helper function for that.

Showcase

@eero-lehtinen eero-lehtinen force-pushed the wasm-asset-request-mapper branch from 612daa8 to 00ede2d Compare November 3, 2025 20:51
@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-Assets Load files from disk to use for things like images, models, and sounds S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Nov 3, 2025
Copy link
Contributor

@kristoff3r kristoff3r left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, I need something like this for work.

Having a builtin cache busting solution would be great down the line, but that might be complicated/controversial, and this will allow experimenting in third party plugins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Assets Load files from disk to use for things like images, models, and sounds C-Feature A new feature, making something new possible S-Needs-Review Needs reviewer attention (from anyone!) to move forward

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants