Append hash to CSS assets for cache busting
Diff
Cargo.lock | 13 +++++++++++++
Cargo.toml | 2 ++
src/main.rs | 42 +++++++++++++++++++++++++++++++++++++++---
templates/base.html | 2 +-
templates/repo/about.html | 4 ++--
templates/repo/commit.html | 4 ++--
templates/repo/diff.html | 4 ++--
templates/repo/file.html | 4 ++--
8 files changed, 59 insertions(+), 16 deletions(-)
@@ -1855,10 +1855,12 @@
"md5",
"moka",
"nom",
"once_cell",
"parking_lot 0.12.1",
"path-clean",
"rsass",
"serde",
"sha2",
"sled",
"syntect",
"time",
@@ -2056,6 +2058,17 @@
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sha2"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8"
dependencies = [
"cfg-if",
"cpufeatures",
@@ -25,9 +25,11 @@
nom = "7.1"
md5 = "0.7"
moka = { version = "0.11.1", features = ["future"] }
once_cell = "1.18"
path-clean = "1.0.1"
parking_lot = "0.12"
serde = { version = "1.0", features = ["derive"] }
sha2 = "0.10"
syntect = "5"
sled = { version = "0.34", features = ["compression"] }
time = { version = "0.3", features = ["serde"] }
@@ -20,6 +20,9 @@
};
use bat::assets::HighlightingAssets;
use clap::Parser;
use once_cell::sync::{Lazy, OnceCell};
use sha2::digest::FixedOutput;
use sha2::Digest;
use sled::Db;
use syntect::html::ClassStyle;
use tokio::{
@@ -40,6 +43,12 @@
const CRATE_VERSION: &str = clap::crate_version!();
static GLOBAL_CSS: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/statics/css/style.css",));
static GLOBAL_CSS_HASH: Lazy<Box<str>> = Lazy::new(|| build_asset_hash(GLOBAL_CSS));
static HIGHLIGHT_CSS_HASH: OnceCell<Box<str>> = OnceCell::new();
static DARK_HIGHLIGHT_CSS_HASH: OnceCell<Box<str>> = OnceCell::new();
#[derive(Parser, Debug)]
#[clap(author, version, about)]
pub struct Args {
@@ -114,6 +123,7 @@
.into_boxed_str()
.into_boxed_bytes(),
);
HIGHLIGHT_CSS_HASH.set(build_asset_hash(css)).unwrap();
let dark_theme = bat_assets.get_theme("TwoDark");
let dark_css =
@@ -123,6 +133,9 @@
.into_boxed_str()
.into_boxed_bytes(),
);
DARK_HIGHLIGHT_CSS_HASH
.set(build_asset_hash(dark_css))
.unwrap();
let static_favicon = |content: &'static [u8]| {
move || async move {
@@ -148,15 +161,21 @@
let app = Router::new()
.route("/", get(methods::index::handle))
.route(
&format!("/style-{}.css", *GLOBAL_CSS_HASH),
get(static_css(GLOBAL_CSS)),
)
.route(
&format!("/highlight-{}.css", HIGHLIGHT_CSS_HASH.get().unwrap()),
get(static_css(css)),
)
.route(
"/style.css",
get(static_css(include_bytes!(concat!(
env!("OUT_DIR"),
"/statics/css/style.css"
)))),
&format!(
"/highlight-dark-{}.css",
DARK_HIGHLIGHT_CSS_HASH.get().unwrap()
),
get(static_css(dark_css)),
)
.route("/highlight.css", get(static_css(css)))
.route("/highlight-dark.css", get(static_css(dark_css)))
.route(
"/favicon.ico",
get(static_favicon(include_bytes!("../statics/favicon.ico"))),
@@ -221,6 +240,15 @@
}
})
.await
}
#[must_use]
pub fn build_asset_hash(v: &[u8]) -> Box<str> {
let mut hasher = sha2::Sha256::default();
hasher.update(v);
let mut out = hex::encode(hasher.finalize_fixed());
out.truncate(10);
Box::from(out)
}
#[instrument(skip(t))]
@@ -1,10 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>{% block title %}rgit{% endblock %}</title>
<link rel="stylesheet" type="text/css" href="/style.css" />
<link rel="stylesheet" type="text/css" href="/style-{{ crate::GLOBAL_CSS_HASH.as_ref() }}.css" />
{%- block head -%}{%- endblock %}
</head>
@@ -1,10 +1,10 @@
{% extends "repo/base.html" %}
{% block head -%}
{%- if let Some(readme) = readme -%}
{%- if readme.0 == crate::git::ReadmeFormat::Markdown %}
<link rel="stylesheet" type="text/css" href="/highlight.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark.css" />
<link rel="stylesheet" type="text/css" href="/highlight-{{ crate::HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark-{{ crate::DARK_HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
{%- endif -%}
{%- endif -%}
{% endblock %}
@@ -1,8 +1,8 @@
{% extends "repo/base.html" %}
{% block head %}
<link rel="stylesheet" type="text/css" href="/highlight.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark.css" />
<link rel="stylesheet" type="text/css" href="/highlight-{{ crate::HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark-{{ crate::DARK_HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
{%- endblock %}
{% block commit_nav_class %}active{% endblock %}
@@ -1,8 +1,8 @@
{% extends "repo/base.html" %}
{%- block head %}
<link rel="stylesheet" type="text/css" href="/highlight.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark.css" />
<link rel="stylesheet" type="text/css" href="/highlight-{{ crate::HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark-{{ crate::DARK_HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
{%- endblock -%}
{% block diff_nav_class %}active{% endblock %}
@@ -1,8 +1,8 @@
{% extends "repo/base.html" %}
{% block head %}
<link rel="stylesheet" type="text/css" href="/highlight.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark.css" />
<link rel="stylesheet" type="text/css" href="/highlight-{{ crate::HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
<link rel="stylesheet" type="text/css" href="/highlight-dark-{{ crate::DARK_HIGHLIGHT_CSS_HASH.get().unwrap() }}.css" />
{%- endblock %}
{% block tree_nav_class %}active{% endblock %}