Include hash of static files in paths

This way, clients will re-fetch the files whenever their content changes
and not have outdated versions in their cache.
This commit is contained in:
Joscha 2023-08-14 01:35:50 +02:00
parent a36b188938
commit 123c27e5a2

View file

@ -1,5 +1,7 @@
use std::{ use std::{
collections::hash_map::DefaultHasher,
fs, fs,
hash::Hasher,
path::{Path, PathBuf}, path::{Path, PathBuf},
process::Command, process::Command,
}; };
@ -59,13 +61,17 @@ fn make_static_constants(static_out_dir: &Path, static_out_file: &Path) {
let relative_path = relative_path(&file); let relative_path = relative_path(&file);
let relative_path = relative_path.to_str().unwrap(); let relative_path = relative_path.to_str().unwrap();
let mut hasher = DefaultHasher::new();
hasher.write(&fs::read(file.path()).unwrap());
let hash = hasher.finish() & 0xffffffff;
let name = relative_path let name = relative_path
.split(|c: char| !c.is_ascii_alphabetic()) .split(|c: char| !c.is_ascii_alphabetic())
.filter(|s| !s.is_empty()) .filter(|s| !s.is_empty())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("_") .join("_")
.to_uppercase(); .to_uppercase();
let path = format!("/{relative_path}"); let path = format!("/{relative_path}?h={hash:x}");
definitions.push_str(&format!("pub const {name}: &str = {path:?};\n")); definitions.push_str(&format!("pub const {name}: &str = {path:?};\n"));
} }