css caching

This commit is contained in:
Robin Appelman 2023-04-08 20:53:34 +02:00
commit 6485612898
5 changed files with 36 additions and 5 deletions

7
Cargo.lock generated
View file

@ -587,6 +587,7 @@ dependencies = [
"itertools", "itertools",
"lightningcss", "lightningcss",
"maud", "maud",
"md5",
"serde", "serde",
"sqlx", "sqlx",
"steamid-ng", "steamid-ng",
@ -1062,6 +1063,12 @@ dependencies = [
"digest", "digest",
] ]
[[package]]
name = "md5"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "490cc448043f947bae3cbee9c203358d62dbee0db12107a74be5c30ccfd09771"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.5.0"

View file

@ -25,3 +25,4 @@ itertools = "0.10.5"
lightningcss = { version = "1.0.0-alpha.40", features = ["browserslist", "visitor"] } lightningcss = { version = "1.0.0-alpha.40", features = ["browserslist", "visitor"] }
base64 = "0.21.0" base64 = "0.21.0"
urlencoding = "2.1.2" urlencoding = "2.1.2"
md5 = "0.7.0"

View file

@ -18,7 +18,13 @@ fn main() {
println!("cargo:rerun-if-changed=style"); println!("cargo:rerun-if-changed=style");
println!("cargo:rerun-if-changed=images"); println!("cargo:rerun-if-changed=images");
write(format!("{out_dir}/style.css"), build_style()).expect("failed to write compiled style"); let style = build_style();
write(format!("{out_dir}/style.css"), &style).expect("failed to write compiled style");
write(
format!("{out_dir}/style.md5"),
format!("{:x}", md5::compute(&style)),
)
.expect("failed to write compiled style hash");
} }
pub fn build_style() -> String { pub fn build_style() -> String {

View file

@ -14,7 +14,7 @@ use axum::http::{HeaderValue, Request};
use axum::response::IntoResponse; use axum::response::IntoResponse;
use axum::{extract::State, routing::get, Router, Server}; use axum::{extract::State, routing::get, Router, Server};
pub use error::Error; pub use error::Error;
use hyper::header::CONTENT_TYPE; use hyper::header::{CACHE_CONTROL, CONTENT_TYPE, ETAG};
use hyperlocal::UnixServerExt; use hyperlocal::UnixServerExt;
use maud::Markup; use maud::Markup;
use sqlx::PgPool; use sqlx::PgPool;
@ -52,7 +52,7 @@ async fn main() -> Result<()> {
let app = Router::new() let app = Router::new()
.route("/", get(index)) .route("/", get(index))
.route("/style.css", get(style)) .route("/style.:version.css", get(style))
.route("/:id", get(demo)) .route("/:id", get(demo))
.layer( .layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| { TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
@ -96,8 +96,20 @@ async fn main() -> Result<()> {
async fn style() -> impl IntoResponse { async fn style() -> impl IntoResponse {
let style = include_str!(concat!(env!("OUT_DIR"), "/style.css")); let style = include_str!(concat!(env!("OUT_DIR"), "/style.css"));
let etag = concat!(
r#"""#,
include_str!(concat!(env!("OUT_DIR"), "/style.md5")),
r#"""#
);
( (
[(CONTENT_TYPE, HeaderValue::from_static("text/css"))], [
(CONTENT_TYPE, HeaderValue::from_static("text/css")),
(ETAG, HeaderValue::from_static(etag)),
(
CACHE_CONTROL,
HeaderValue::from_static("public, max-age=2592000, immutable"),
),
],
style, style,
) )
} }

View file

@ -10,12 +10,17 @@ pub trait Page {
} }
pub fn render<T: Page>(page: T) -> Markup { pub fn render<T: Page>(page: T) -> Markup {
let style_url = concat!(
"/style.",
include_str!(concat!(env!("OUT_DIR"), "/style.md5")),
".css",
);
html! { html! {
(DOCTYPE) (DOCTYPE)
html { html {
head { head {
title { (page.title()) } title { (page.title()) }
link rel="stylesheet" type="text/css" href="/style.css"; link rel="stylesheet" type="text/css" href=(style_url);
} }
body { body {
header { header {