Asset timestamping

This commit is contained in:
2022-07-20 21:58:36 +02:00
parent 9b4a60ce40
commit 48f14cb8a7
3 changed files with 32 additions and 2 deletions
+3 -1
View File
@@ -2,4 +2,6 @@ SALT=64_byte_string
DATABASE_URL=postgres://xxx:xxx@localhost/xxx
METALS_API_KEY=xxx
NUMISTA_API_KEY=xxx
NUMISTA_API_KEY=xxx
ASSET_DIR=/opt/melt/public
+27
View File
@@ -82,6 +82,33 @@ impl ClientCtx {
format!("{:.2}", AU_TO_USD.load(Ordering::Relaxed) as f32 / 1000.0)
}
pub fn get_asset_timestamp(&self) -> u64 {
let asset = "assets/style.css";
match std::fs::metadata(format!(
"{}/{}",
std::env::var("ASSET_DIR").unwrap_or_else(|_| ".".to_string()),
asset
)) {
Ok(metadata) => match metadata.modified() {
Ok(time) => match time.duration_since(std::time::UNIX_EPOCH) {
Ok(distance) => distance.as_secs(),
Err(_) => {
log::warn!("Unable to do math on webpack chat.js modified at timestamp");
0
}
},
Err(_) => {
log::warn!("Unable to read metadata on webpack chat.js");
0
}
},
Err(_) => {
log::warn!("Unable to open webpack chat.js for timestamp");
0
}
}
}
/// Returns Duration representing request time.
pub fn request_time(&self) -> Duration {
Instant::now() - self.0.request_start
+2 -1
View File
@@ -15,7 +15,8 @@
</script>
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="/assets/style.css" nonce="{{ client.get_nonce() }}" />
<link rel="stylesheet" type="text/css" href="/assets/style.css?{{ client.get_asset_timestamp()}}"
nonce="{{ client.get_nonce() }}" />
{#
<link rel="stylesheet" type="text/css" href="/assets/print.css" media="print" nonce="{{ client.get_nonce() }}" /> #}
{% endblock %}