From d2455aa1c1bfc599a07966a7d717c1380d41bbc0 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 14 Jan 2021 22:36:34 -0500 Subject: Cache better (#296) * Many improvements around bandwidth use - Use ETags for RSS/Atom feeds - Use cache-control headers - Update to rust nightly (for rust-analyzer and faster builds) - Limit feeds to the last 20 posts: https://twitter.com/theprincessxena/status/1349891678857998339 - Use if-none-match to limit bandwidth further Also does this: - bump go_vanity to 0.3.0 and lets users customize the branch name - fix formatting on jsonfeed - remove last vestige of kubernetes/docker support Signed-off-by: Christine Dodrill * expire cache quicker for dynamic pages Signed-off-by: Christine Dodrill * add rss ttl Signed-off-by: Christine Dodrill * add blogpost Signed-off-by: Christine Dodrill --- src/main.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index c05ac49..285bb93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,20 +113,39 @@ async fn main() -> Result<()> { .and(with_state(state.clone())) .and_then(handlers::patrons); - let files = warp::path("static").and(warp::fs::dir("./static")); - let css = warp::path("css").and(warp::fs::dir("./css")); + let files = warp::path("static") + .and(warp::fs::dir("./static")) + .map(|reply| { + warp::reply::with_header( + reply, + "Cache-Control", + "public, max-age=86400, stale-if-error=60", + ) + }); + + let css = warp::path("css").and(warp::fs::dir("./css")).map(|reply| { + warp::reply::with_header( + reply, + "Cache-Control", + "public, max-age=86400, stale-if-error=60", + ) + }); + let sw = warp::path("sw.js").and(warp::fs::file("./static/js/sw.js")); let robots = warp::path("robots.txt").and(warp::fs::file("./static/robots.txt")); let favicon = warp::path("favicon.ico").and(warp::fs::file("./static/favicon/favicon.ico")); let jsonfeed = warp::path("blog.json") .and(with_state(state.clone())) + .and(warp::header::optional("if-none-match")) .and_then(handlers::feeds::jsonfeed); let atom = warp::path("blog.atom") .and(with_state(state.clone())) + .and(warp::header::optional("if-none-match")) .and_then(handlers::feeds::atom); let rss = warp::path("blog.rss") .and(with_state(state.clone())) + .and(warp::header::optional("if-none-match")) .and_then(handlers::feeds::rss); let sitemap = warp::path("sitemap.xml") .and(with_state(state.clone())) @@ -135,6 +154,7 @@ async fn main() -> Result<()> { let go_vanity_jsonfeed = warp::path("jsonfeed") .and(warp::any().map(move || "christine.website/jsonfeed")) .and(warp::any().map(move || "https://tulpa.dev/Xe/jsonfeed")) + .and(warp::any().map(move || "master")) .and_then(go_vanity::gitea); let metrics_endpoint = warp::path("metrics").and(warp::path::end()).map(move || { @@ -149,14 +169,37 @@ async fn main() -> Result<()> { .unwrap() }); - let site = index - .or(contact.or(feeds).or(resume.or(signalboost)).or(patrons)) - .or(blog_index.or(series.or(series_view).or(post_view))) + let static_pages = index + .or(feeds) + .or(resume.or(signalboost)) + .or(patrons) + .or(jsonfeed.or(atom.or(sitemap)).or(rss)) + .or(favicon.or(robots).or(sw)) + .or(contact) + .map(|reply| { + warp::reply::with_header( + reply, + "Cache-Control", + "public, max-age=86400, stale-if-error=60", + ) + }); + + let dynamic_pages = blog_index + .or(series.or(series_view).or(post_view)) .or(gallery_index.or(gallery_post_view)) .or(talk_index.or(talk_post_view)) - .or(jsonfeed.or(atom).or(rss.or(sitemap))) - .or(files.or(css).or(favicon).or(sw.or(robots))) + .map(|reply| { + warp::reply::with_header( + reply, + "Cache-Control", + "public, max-age=600, stale-if-error=60", + ) + }); + + let site = static_pages + .or(dynamic_pages) .or(healthcheck.or(metrics_endpoint).or(go_vanity_jsonfeed)) + .or(files.or(css)) .map(|reply| { warp::reply::with_header( reply, -- cgit v1.2.3