From 123c27e5a2960d29aec26cc61649bcbfd915fef9 Mon Sep 17 00:00:00 2001 From: Joscha Date: Mon, 14 Aug 2023 01:35:50 +0200 Subject: [PATCH] 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. --- build.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 13f3fc0..113b5e6 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,7 @@ use std::{ + collections::hash_map::DefaultHasher, fs, + hash::Hasher, path::{Path, PathBuf}, 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.to_str().unwrap(); + let mut hasher = DefaultHasher::new(); + hasher.write(&fs::read(file.path()).unwrap()); + let hash = hasher.finish() & 0xffffffff; + let name = relative_path .split(|c: char| !c.is_ascii_alphabetic()) .filter(|s| !s.is_empty()) .collect::>() .join("_") .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")); }