Skip to content

Commit

Permalink
Bust JS cache when JS changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Eijebong committed May 26, 2024
1 parent 240ce9f commit aa071b3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
19 changes: 15 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@ use sha2::{Digest, Sha256};
use walkdir::WalkDir;

fn main() -> anyhow::Result<()> {
let mut hasher = Sha256::new();
let mut css_hasher = Sha256::new();
for entry in WalkDir::new("static/css") {
let entry = entry.unwrap();
if entry.file_type().is_file() {
hasher.update(std::fs::read_to_string(entry.path())?);
css_hasher.update(std::fs::read_to_string(entry.path())?);
}
}

let hash = hasher.finalize();
println!("cargo:rustc-env=CSS_VERSION={:x}", hash);
let css_hash = css_hasher.finalize();
println!("cargo:rustc-env=CSS_VERSION={:x}", css_hash);

let mut js_hasher = Sha256::new();
for entry in WalkDir::new("static/js") {
let entry = entry.unwrap();
if entry.file_type().is_file() {
js_hasher.update(std::fs::read_to_string(entry.path())?);
}
}

let js_hash = js_hasher.finalize();
println!("cargo:rustc-env=JS_VERSION={:x}", js_hash);

Ok(())
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Context {
}

const CSS_VERSION: &str = std::env!("CSS_VERSION");
const JS_VERSION: &str = std::env!("JS_VERSION");

struct TplContext<'a> {
is_admin: bool,
Expand All @@ -42,6 +43,7 @@ struct TplContext<'a> {
err_msg: Vec<String>,
warning_msg: Vec<String>,
css_version: &'a str,
js_version: &'a str,
}

impl<'a> TplContext<'a> {
Expand All @@ -54,6 +56,7 @@ impl<'a> TplContext<'a> {
err_msg: session.err_msg.drain(..).collect(),
warning_msg: session.warning_msg.drain(..).collect(),
css_version: CSS_VERSION,
js_version: JS_VERSION,
};

session
Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
{% block main %}{% endblock main %}
</main>
</div>
<script src="/static/js/time.js" defer></script>
<script src="/static/js/time.js?{{ base.js_version}}" defer></script>
{% block scripts %}
{% endblock scripts %}
</body>
Expand Down
2 changes: 1 addition & 1 deletion templates/room.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
</script>
<script src="/static/contrib/highlight.js/highlight.min.js" defer></script>
<script src="/static/contrib/highlight.js/yaml.min.js" defer></script>
<script src="/static/js/viewer.js" defer></script>
<script src="/static/js/viewer.js?{{ base.js_version }}" defer></script>
{% endblock %}

{% block styles %}
Expand Down

0 comments on commit aa071b3

Please sign in to comment.