Skip to content

Commit aa071b3

Browse files
committed
Bust JS cache when JS changes
1 parent 240ce9f commit aa071b3

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

build.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@ use sha2::{Digest, Sha256};
22
use walkdir::WalkDir;
33

44
fn main() -> anyhow::Result<()> {
5-
let mut hasher = Sha256::new();
5+
let mut css_hasher = Sha256::new();
66
for entry in WalkDir::new("static/css") {
77
let entry = entry.unwrap();
88
if entry.file_type().is_file() {
9-
hasher.update(std::fs::read_to_string(entry.path())?);
9+
css_hasher.update(std::fs::read_to_string(entry.path())?);
1010
}
1111
}
1212

13-
let hash = hasher.finalize();
14-
println!("cargo:rustc-env=CSS_VERSION={:x}", hash);
13+
let css_hash = css_hasher.finalize();
14+
println!("cargo:rustc-env=CSS_VERSION={:x}", css_hash);
15+
16+
let mut js_hasher = Sha256::new();
17+
for entry in WalkDir::new("static/js") {
18+
let entry = entry.unwrap();
19+
if entry.file_type().is_file() {
20+
js_hasher.update(std::fs::read_to_string(entry.path())?);
21+
}
22+
}
23+
24+
let js_hash = js_hasher.finalize();
25+
println!("cargo:rustc-env=JS_VERSION={:x}", js_hash);
1526

1627
Ok(())
1728
}

src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Context {
3333
}
3434

3535
const CSS_VERSION: &str = std::env!("CSS_VERSION");
36+
const JS_VERSION: &str = std::env!("JS_VERSION");
3637

3738
struct TplContext<'a> {
3839
is_admin: bool,
@@ -42,6 +43,7 @@ struct TplContext<'a> {
4243
err_msg: Vec<String>,
4344
warning_msg: Vec<String>,
4445
css_version: &'a str,
46+
js_version: &'a str,
4547
}
4648

4749
impl<'a> TplContext<'a> {
@@ -54,6 +56,7 @@ impl<'a> TplContext<'a> {
5456
err_msg: session.err_msg.drain(..).collect(),
5557
warning_msg: session.warning_msg.drain(..).collect(),
5658
css_version: CSS_VERSION,
59+
js_version: JS_VERSION,
5760
};
5861

5962
session

templates/base.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
{% block main %}{% endblock main %}
5858
</main>
5959
</div>
60-
<script src="/static/js/time.js" defer></script>
60+
<script src="/static/js/time.js?{{ base.js_version}}" defer></script>
6161
{% block scripts %}
6262
{% endblock scripts %}
6363
</body>

templates/room.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
</script>
132132
<script src="/static/contrib/highlight.js/highlight.min.js" defer></script>
133133
<script src="/static/contrib/highlight.js/yaml.min.js" defer></script>
134-
<script src="/static/js/viewer.js" defer></script>
134+
<script src="/static/js/viewer.js?{{ base.js_version }}" defer></script>
135135
{% endblock %}
136136

137137
{% block styles %}

0 commit comments

Comments
 (0)