Crate cache_buster[−][src]
What is cache busting?
To optimise network load time, browsers cache static files. Caching greatly improves performance but how do you inform browsers to invalidate cache when your files have changed?
Cache busting is a simple but effective solution for this issue. There are several ways to achieve this but the way this library does this is by changing file names to include the hash of the files’ contents.
So if you have bundle.js
, it will become
bundle.<long-sha256-hash>.js
. This lets you set a super long cache age
as, because of the file names changing, the path to the filename, too,
will change. So as far as the browser is concerned, you are trying to load
a file that it doesn’t have. Pretty neat, isn’t it?
Example:
build.rs
use cache_buster::BusterBuilder; fn main() { // note: add error checking yourself. // println!("cargo:rustc-env=GIT_process={}", git_process); let types = vec![ mime::IMAGE_PNG, mime::IMAGE_SVG, mime::IMAGE_JPEG, mime::IMAGE_GIF, ]; let config = BusterBuilder::default() .source("./dist") .result("./prod") .mime_types(types) .copy(true) .follow_links(true) .build() .unwrap(); config.process().unwrap(); }
main.rs
:
Module describing runtime compoenet for fetching modified filenames
Add the following tou your program to load the filemap during compiletime:
use cache_buster::Files; use cache_buster::CACHE_BUSTER_DATA_FILE; fn main(){ let files = Files::new(CACHE_BUSTER_DATA_FILE); // the path to the file before setting up for cache busting files.get("./dist/github.svg"); }
Re-exports
pub use processor::BusterBuilder; |
pub use filemap::Files; |
Modules
filemap | Module describing runtime compoenet for fetching modified filenames |
processor | Module describing file processor that changes filenames to setup cache-busting |
Constants
CACHE_BUSTER_DATA_FILE | file to which filemap is written during compilation
include this to |