From 2e750309983b67e5baadeb4109960e8637c1c894 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 26 Aug 2024 21:20:53 +0300 Subject: Add Last-Modified header to post pages to help heuristic caching --- src/frontend/mod.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src') diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index e142ee1..7ddad07 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -4,6 +4,7 @@ use axum::{ http::{StatusCode, Uri}, response::IntoResponse, }; +use axum_extra::headers::HeaderMapExt; use futures_util::FutureExt; use serde::Deserialize; use std::convert::TryInto; @@ -270,6 +271,7 @@ pub async fn homepage( db.get_channels(&hcard_url).map(|i| i.unwrap_or_default()) ); + if user.is_some() { headers.insert( axum::http::header::CACHE_CONTROL, @@ -370,6 +372,26 @@ pub async fn catchall( ); } + if post["type"][0].as_str() == Some("h-entry") { + let last_modified = post["properties"]["updated"] + .as_array() + .and_then(|v| v.last()) + .or_else(|| post["properties"]["published"] + .as_array() + .and_then(|v| v.last()) + ) + .and_then(serde_json::Value::as_str) + .and_then(|dt| chrono::DateTime::::parse_from_rfc3339(dt).ok()); + + if let Some(last_modified) = last_modified { + headers.typed_insert( + axum_extra::headers::LastModified::from( + std::time::SystemTime::from(last_modified) + ) + ); + } + } + // Render the homepage ( StatusCode::OK, -- cgit 1.4.1