Skip to content

Commit

Permalink
use html redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
ngalaiko committed Jan 23, 2024
1 parent 40e7469 commit 97b780e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion serve/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down
39 changes: 18 additions & 21 deletions serve/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
Expand Down
16 changes: 16 additions & 0 deletions shared/src/pages/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,19 @@ pub fn places(places: &[places::Place]) -> maud::Markup {
&footer_without_copy_right(),
)
}

#[must_use]
pub fn redirect<P: AsRef<std::path::Path>>(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(),
)
}

0 comments on commit 97b780e

Please sign in to comment.