Skip to content

Commit bf47803

Browse files
committed
fix(build) :: make frontend asset urls idempotent if nothing changed
Frontend assets were hashed via gzip's output which includes a nonidempotent/deterministc timestamp from libflate. This changes the hash on each build. Now we hash over the bundled file contents in frontend/dist
1 parent a2f7995 commit bf47803

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

build.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn build_served_asset(source: &str, libraries: &[&str]) {
7171

7272
std::fs::write(
7373
format!("{}.filename.txt", built.display()),
74-
hashed_filename(&built),
74+
hashed_filename(source, libraries),
7575
)
7676
.unwrap();
7777
}
@@ -87,27 +87,32 @@ fn build_icon_map() {
8787
icon_map.write_all(b"]").unwrap();
8888
}
8989

90-
// Given a filename, creates a new unique filename based on the file contents
91-
fn hashed_filename(path: &Path) -> String {
90+
fn hashed_filename(source: &str, libraries: &[&str]) -> String {
91+
let mut hasher = DefaultHasher::new();
92+
for path in libraries.iter().copied().chain([source]) {
93+
hash_contents(path, &mut hasher);
94+
}
95+
let name = Path::new(source);
96+
format!(
97+
"{}.{:x}.{}",
98+
name.file_stem().unwrap().to_str().unwrap(),
99+
hasher.finish(),
100+
name.extension().unwrap().to_str().unwrap()
101+
)
102+
}
103+
104+
fn hash_contents(path: &str, hasher: &mut DefaultHasher) {
92105
let mut file = File::open(path).unwrap();
93106
let mut buf = [0u8; 4096];
94-
let mut hasher = DefaultHasher::new();
95107
loop {
96108
let bytes_read = file
97109
.read(&mut buf)
98-
.unwrap_or_else(|e| panic!("error reading '{}': {}", path.display(), e));
110+
.unwrap_or_else(|e| panic!("error reading '{path}': {e}"));
99111
if bytes_read == 0 {
100112
break;
101113
}
102114
hasher.write(&buf[..bytes_read]);
103115
}
104-
let hash = hasher.finish();
105-
format!(
106-
"{}.{:x}.{}",
107-
path.file_stem().unwrap().to_str().unwrap(),
108-
hash,
109-
path.extension().unwrap().to_str().unwrap()
110-
)
111116
}
112117

113118
fn take_between<'a>(s: &mut &'a str, start: &str, end: &str) -> Option<&'a str> {

0 commit comments

Comments
 (0)