🏡 index : ~doyle/rgit.git

author Jordan Doyle <jordan@doyle.la> 2023-09-01 3:45:39.0 +01:00:00
committer Jordan Doyle <jordan@doyle.la> 2023-09-01 3:45:39.0 +01:00:00
commit
73ef6091408f43ecc1cc6cfe83c837344626c670 [patch]
tree
1e589830a090653918ccdc229ae3fe3fea714015
parent
674679645084133e5d2098e4b5e6cad7b69a9746
download
73ef6091408f43ecc1cc6cfe83c837344626c670.tar.gz

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(-)

diff --git a/Cargo.lock b/Cargo.lock
index 565a31a..e95a417 100644
--- a/Cargo.lock
+++ a/Cargo.lock
@@ -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",
diff --git a/Cargo.toml b/Cargo.toml
index f1b8b62..5f0b18f 100644
--- a/Cargo.toml
+++ a/Cargo.toml
@@ -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"] }
diff --git a/src/main.rs b/src/main.rs
index cb49875..0ed6f08 100644
--- a/src/main.rs
+++ a/src/main.rs
@@ -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))]
diff --git a/templates/base.html b/templates/base.html
index 4c02da1..3000365 100644
--- a/templates/base.html
+++ a/templates/base.html
@@ -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>

diff --git a/templates/repo/about.html b/templates/repo/about.html
index c0e8565..d111304 100644
--- a/templates/repo/about.html
+++ a/templates/repo/about.html
@@ -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 %}
diff --git a/templates/repo/commit.html b/templates/repo/commit.html
index 15097ea..caa78f0 100644
--- a/templates/repo/commit.html
+++ a/templates/repo/commit.html
@@ -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 %}
diff --git a/templates/repo/diff.html b/templates/repo/diff.html
index 42fd90d..dba30f1 100644
--- a/templates/repo/diff.html
+++ a/templates/repo/diff.html
@@ -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 %}
diff --git a/templates/repo/file.html b/templates/repo/file.html
index d2727ec..9e09908 100644
--- a/templates/repo/file.html
+++ a/templates/repo/file.html
@@ -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 %}