diff --git a/serve/build.rs b/serve/build.rs index eb927573..d5e551c0 100644 --- a/serve/build.rs +++ b/serve/build.rs @@ -185,7 +185,7 @@ fn build() -> Result<(), BuildError> { for alias in &post.frontmatter.aliases { write( join(&output, path::normalize(alias)), - format!("redirect: {}", post.path.display()).as_bytes(), + pages::html::redirect(&post.path).into_string().as_bytes(), ) .map_err(BuildError::Io)?; } diff --git a/serve/src/main.rs b/serve/src/main.rs index 9bfe0fd5..5c823276 100644 --- a/serve/src/main.rs +++ b/serve/src/main.rs @@ -43,30 +43,27 @@ async fn serve_asset(req: tide::Request<()>) -> tide::Result { let response = if let Some(embedded_file) = asset_path.and_then(|asset_path| Public::get(&asset_path)) { - let data = embedded_file.data.to_vec(); - if let Some(location) = data.strip_prefix(b"redirect: ") { - let location = std::str::from_utf8(location)?; - tide::Redirect::new(location).into() + let etag = etag_header(embedded_file.metadata.sha256_hash()); + let is_modified = req + .header("if-none-match") + .map(|etags| etags.contains(&etag)) + .unwrap_or_default(); + if is_modified { + tide::Response::builder(tide::StatusCode::NotModified) } else { - let etag = etag_header(embedded_file.metadata.sha256_hash()); - let is_modified = req - .header("if-none-match") - .map(|etags| etags.contains(&etag)) - .unwrap_or_default(); - if is_modified { - tide::Response::builder(tide::StatusCode::NotModified) - } else { - tide::Response::builder(tide::StatusCode::Ok) - .body(tide::Body::from(embedded_file.data.to_vec())) - } - .header("content-type", embedded_file.metadata.mimetype()) - .header("cache-control", match embedded_file.metadata.mimetype() { + tide::Response::builder(tide::StatusCode::Ok) + .body(tide::Body::from(embedded_file.data.to_vec())) + } + .header("content-type", embedded_file.metadata.mimetype()) + .header( + "cache-control", + match embedded_file.metadata.mimetype() { "text/html" => "no-cache, max-age=31536000", _ => "max-age=31536000", - }) - .header("etag", etag) - .build() - } + }, + ) + .header("etag", etag) + .build() } else { tide::Response::new(tide::StatusCode::NotFound) }; diff --git a/shared/src/pages/html.rs b/shared/src/pages/html.rs index 51df8eb3..b506c61d 100644 --- a/shared/src/pages/html.rs +++ b/shared/src/pages/html.rs @@ -309,3 +309,19 @@ pub fn places(places: &[places::Place]) -> maud::Markup { &footer_without_copy_right(), ) } + +#[must_use] +pub fn redirect>(to: P) -> maud::Markup { + let to = to.as_ref().display().to_string(); + new( + "redirect", + Some(&maud::html! { + meta http-equiv="refresh" content=(format!("0; url={}", to)); + link rel="canonical" href=(to); + }), + &maud::html! { + "If you are not redirected automatically, follow this" a href=(to) { "link" } + }, + &footer_without_copy_right(), + ) +}