Cache Busting and Images
[NOTE - This section needs to be tested since we migrated to using crates]
Cache busting is where we invalidate a cached file and force the browser to retrieve the file from the server. We can instruct the browser to bypass the cache by simply changing the filename. To the browser, this is a completely new resource so it will fetch the resource from the server. The most common way to do this is to add the hash of the file to the URL.
Using Ructe for Cache Busting
Let's turn our asset-pipeline
folder into a Rust crate. We do this because we can use Ructe
to generate code that allows to access assets in a typesafe way. Ructe also handles hashing so that we never have to worry about the browser deploying the wrong CSS or Javascript.
Run the following...
Create a crates/asset-pipeline/build.rs
so that the main
method looks like the following.
And add the following to crates/asset-pipeline/Cargo.toml
in the dependencies section.
# Used by ructe for image mime type detection
= "0.3.0"
Ructe will now take our assets and turn them into rust functions. It handles creating a hash for the assets so we get good browser cache busting.
Using the Assets
use super::statics::*;
dbg!(index_css.name) -> index.234532455.css
Configuring a route for our assets
In crates/axum-server/main.rs
create the following function.
async
And add the following route also in crates/axum-server/main.rs
.route
And change the use
section so it looks like the following.
use crate CustomError;
use ;
use ;
use Pool;
use SocketAddr;
use ;
use ;
use StaticFile;