Skip to content

Commit ae66335

Browse files
committed
Redirect to the default target if the current target doesn't exist
1 parent ba9364b commit ae66335

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/web/rustdoc.rs

+28
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
363363

364364
return if ctry!(req, storage.exists(&path)) {
365365
redirect(&name, &version, &req_path[3..])
366+
} else if req_path.get(3).map_or(false, |p| p.contains('-')) {
367+
// This is a target, not a module; it may not have been built.
368+
// Redirect to the default target and show a search page instead of a hard 404.
369+
redirect(
370+
&format!("/crate/{}", name),
371+
&format!("{}/target-redirect", version),
372+
&req_path[3..],
373+
)
366374
} else {
367375
Err(Nope::ResourceNotFound.into())
368376
};
@@ -1746,4 +1754,24 @@ mod test {
17461754
Ok(())
17471755
})
17481756
}
1757+
1758+
#[test]
1759+
fn test_missing_target_redirects_to_search() {
1760+
wrapper(|env| {
1761+
env.fake_release()
1762+
.name("winapi")
1763+
.version("0.3.9")
1764+
.rustdoc_file("winapi/macro.ENUM.html")
1765+
.create()?;
1766+
1767+
assert_redirect(
1768+
"/winapi/0.3.9/x86_64-unknown-linux-gnu/winapi/macro.ENUM.html",
1769+
"/winapi/0.3.9/winapi/macro.ENUM.html",
1770+
env.frontend(),
1771+
)?;
1772+
assert_not_found("/winapi/0.3.9/winapi/struct.not_here.html", env.frontend())?;
1773+
1774+
Ok(())
1775+
})
1776+
}
17491777
}

0 commit comments

Comments
 (0)