Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions crates/bin/docs_rs_web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ repository.workspace = true
edition.workspace = true
build = "build.rs"

[package.metadata.cargo-machete]
ignored = [
# used in build script output
# used in askama templates, can be moved to web binary
"slug",
]

[dependencies]
anyhow = { workspace = true }
askama = { workspace = true }
Expand Down Expand Up @@ -60,7 +53,6 @@ regex = { workspace = true }
sentry = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
slug = "0.1.1"
sqlx = { workspace = true }
strum = { workspace = true }
syntect = { version = "5.0.0", default-features = false, features = ["dump-load", "html", "parsing", "regex-onig"] }
Expand Down
21 changes: 20 additions & 1 deletion crates/bin/docs_rs_web/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ fn main() -> Result<()> {
}
writeln!(etag_file, "];")?;

// Include paths as well as contents so additions, removals, and renames
// change the cache buster. BTreeMap provides deterministic ordering.
let mut asset_hash = md5::Context::new();
for (path, etag) in &etag_map {
asset_hash.consume(path.as_bytes());
asset_hash.consume([0]);
asset_hash.consume(etag.as_bytes());
asset_hash.consume([0]);
}
// 64 bits is enough for a cache buster while keeping asset URLs short.
let asset_hash = format!("{:x}", asset_hash.finalize());
writeln!(
etag_file,
"pub const STATIC_ASSET_HASH: &str = {:?};",
&asset_hash[..16],
)?;

etag_file.sync_all()?;

// trigger recompilation when a new migration is added
Expand Down Expand Up @@ -165,13 +182,15 @@ fn calculate_static_etags(etag_map: &mut ETagMap) -> Result<()> {
for entry in walkdir::WalkDir::new(static_dir) {
let entry = entry?;
let path = entry.path();
// Track directories too, so adding or removing assets reruns the build.
tracked::track(path)?;
if !path.is_file() {
continue;
}

let partial_path = path.strip_prefix(static_dir).unwrap();
let partial_path_str = partial_path.to_string_lossy().to_string();
etag_map.insert(partial_path_str, etag_from_path(path)?);
etag_map.insert(partial_path_str, etag_from_content(tracked::read(path)?));
}
}

Expand Down
26 changes: 22 additions & 4 deletions crates/bin/docs_rs_web/src/handlers/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
TemplateData,
templates::{RenderBrands, RenderRegular, RenderSolid, filters},
},
routes::cached_static_redirect,
utils::{self, licenses},
};
use anyhow::{Context as _, anyhow};
Expand Down Expand Up @@ -245,10 +246,7 @@ pub(crate) async fn rustdoc_redirector_handler(
// redirect all ico requests
// originally from:
// https://github.com/rust-lang/docs.rs/commit/f3848a34c391841a2516a9e6ad1f80f6f490c6d0
return Ok(axum_cached_redirect(
"/-/static/favicon.ico",
CachePolicy::ForeverInCdnAndBrowser,
)?);
return Ok(cached_static_redirect("favicon.ico").into_response());
}

// edge case 3:
Expand Down Expand Up @@ -1108,6 +1106,26 @@ mod test {
use test_case::test_case;
use tracing::info;

#[test_case("/rust-logo.ico")]
#[test_case("/some-crate/1.0.0/favicon.ico")]
#[tokio::test(flavor = "multi_thread")]
async fn favicon_redirect(path: &str) -> Result<()> {
let env = TestEnvironment::new().await?;
let target = format!("/-/static/v/{}/favicon.ico", crate::STATIC_ASSET_HASH,);
let response = env
.web_app()
.await
.assert_redirect_cached(
path,
&target,
CachePolicy::ForeverInCdn(crate::cache::SURROGATE_KEY_DOCSRS_STATIC.into()),
env.config(),
)
.await?;
assert_eq!(response.status(), StatusCode::TEMPORARY_REDIRECT);
Ok(())
}

fn has_content_len(headers: &HeaderMap) -> bool {
let content_length: usize = headers
.get(CONTENT_LENGTH)
Expand Down
Loading
Loading