Crate libcachebust

source ·
Expand description

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 libcachebust::BusterBuilder;

// 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)
    .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 libcachebust::Files;
use libcachebust::CACHE_BUSTER_DATA_FILE;

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

Modules

  • Module describing runtime compoenet for fetching modified filenames
  • Module describing file processor that changes filenames to setup cache-busting

Constants