make it blazingly fast with caching of css and js

This commit is contained in:
nora 2025-11-28 23:53:52 +01:00
parent f539a3c45c
commit d15a746558
8 changed files with 77 additions and 18 deletions

11
Cargo.lock generated
View file

@ -238,6 +238,15 @@ dependencies = [
"generic-array",
]
[[package]]
name = "bs58"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf88ba1141d185c399bee5288d850d63b8369520c1eafc32a0430b5b6c287bf4"
dependencies = [
"tinyvec",
]
[[package]]
name = "bumpalo"
version = "3.19.0"
@ -448,6 +457,7 @@ version = "0.1.0"
dependencies = [
"askama",
"axum",
"bs58",
"color-eyre",
"futures",
"jiff",
@ -456,6 +466,7 @@ dependencies = [
"reqwest",
"rootcause",
"serde",
"sha2",
"sqlx",
"tempfile",
"tokio",

View file

@ -31,4 +31,6 @@ tracing = { version = "0.1.40", features = ["attributes"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
[build-dependencies]
bs58 = "0.5.1"
color-eyre = "0.6.3"
sha2 = "0.10.9"

View file

@ -1,6 +1,29 @@
use std::path::PathBuf;
use sha2::Digest;
fn main() {
// Always rerun.
let index_css = include_str!("static/index.css");
let index_js = include_str!("static/index.js");
let index_css_name = get_file_ref("css", index_css);
let index_js_name = get_file_ref("js", index_js);
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
std::fs::write(
out_dir.join("revs.rs"),
format!(
r#"
pub const INDEX_CSS_NAME: &str = "{index_css_name}";
pub const INDEX_JS_NAME: &str = "{index_js_name}";
"#
),
)
.unwrap();
let version = if let Ok(commit) = try_get_commit() {
match has_no_changes() {
Ok(true) => commit,
@ -20,6 +43,15 @@ fn main() {
println!("cargo:rustc-env=GIT_COMMIT_SHORT={version_short}");
}
fn get_file_ref(ext: &str, content: &str) -> String {
let mut hash = sha2::Sha256::new();
hash.update(content);
let digest = hash.finalize();
let rev = bs58::encode(digest).into_string();
let rev = &rev[..16];
format!("/{rev}.{ext}")
}
fn try_get_commit() -> color_eyre::Result<String> {
if let Ok(overridden) = std::env::var("DOES_IT_BUILD_OVERRIDE_VERSION") {
return Ok(overridden);

View file

@ -16,6 +16,10 @@ use crate::{
notification, Result,
};
mod revs {
include!(concat!(env!("OUT_DIR"), "/revs.rs"));
}
#[derive(Clone)]
pub struct AppState {
pub db: Db,
@ -28,8 +32,8 @@ pub async fn webserver(db: Db, notification_repo: String) -> Result<()> {
.route("/build", get(web_build))
.route("/target", get(web_target))
.route("/nightly", get(web_nightly))
.route("/index.css", get(index_css))
.route("/index.js", get(index_js))
.route(revs::INDEX_CSS_NAME, get(index_css))
.route(revs::INDEX_JS_NAME, get(index_js))
.with_state(AppState {
db,
notification_repo,
@ -334,23 +338,33 @@ async fn web_root(State(state): State<AppState>) -> impl IntoResponse {
})
}
async fn index_css() -> impl IntoResponse {
fn reply_static(body: &'static str, content_type: &'static str) -> impl IntoResponse {
(
[(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("text/css; charset=utf-8"),
)],
[
(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static(content_type),
),
(
axum::http::header::CACHE_CONTROL,
axum::http::HeaderValue::from_static("public, max-age=31556952, immutable"),
),
],
body,
)
}
async fn index_css() -> impl IntoResponse {
reply_static(
include_str!("../static/index.css"),
"text/css; charset=utf-8",
)
}
async fn index_js() -> impl IntoResponse {
(
[(
axum::http::header::CONTENT_TYPE,
axum::http::HeaderValue::from_static("application/javascript; charset=utf-8"),
)],
reply_static(
include_str!("../static/index.js"),
"application/javascript; charset=utf-8",
)
}

View file

@ -4,8 +4,8 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Build {{nightly}} {{target}}</title>
<link rel="stylesheet" href="/index.css" />
<script type="module" defer src="index.js"></script>
<link rel="stylesheet" href="{{ revs::INDEX_CSS_NAME }}" />
<script type="module" defer src="{{ revs::INDEX_JS_NAME }}"></script>
</head>
<body>
<h1>

View file

@ -4,8 +4,8 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Does it build?</title>
<link rel="stylesheet" href="index.css" />
<script type="module" defer src="index.js"></script>
<link rel="stylesheet" href="{{ revs::INDEX_CSS_NAME }}" />
<script type="module" defer src="{{ revs::INDEX_JS_NAME }}"></script>
</head>
<body>
<h1>Does it build?</h1>

View file

@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{nightly}} build history</title>
<link rel="stylesheet" href="/index.css" />
<link rel="stylesheet" href="{{ revs::INDEX_CSS_NAME }}" />
</head>
<body>
<h1>Nightly build state for {{nightly}}</h1>

View file

@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>{{target}} build history</title>
<link rel="stylesheet" href="/index.css" />
<link rel="stylesheet" href="{{ revs::INDEX_CSS_NAME }}" />
</head>
<body>
<h1>Target build history for {{target}}</h1>