Skip to content

Make "Go to latest version" point to /latest/ #1562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 18, 2021
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
97 changes: 31 additions & 66 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
#[derive(Debug, Clone, PartialEq, Serialize)]
struct RustdocPage {
latest_path: String,
permalink_path: String,
latest_version: String,
target: String,
inner_path: String,
Expand Down Expand Up @@ -448,23 +449,29 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
};

// Find the path of the latest version for the `Go to latest` and `Permalink` links
let mut latest_path = if latest_release.build_status {
let target_redirect = if latest_release.build_status {
let target = if target.is_empty() {
&krate.metadata.default_target
} else {
target
};
format!(
"/crate/{}/{}/target-redirect/{}/{}",
name, latest_version, target, inner_path
)
format!("/target-redirect/{}/{}", target, inner_path)
} else {
format!("/crate/{}/{}", name, latest_version)
"".to_string()
};
if let Some(query) = req.url.query() {
latest_path.push('?');
latest_path.push_str(query);
}

let query_string = if let Some(query) = req.url.query() {
format!("?{}", query)
} else {
"".to_string()
};

let permalink_path = format!(
"/crate/{}/{}{}{}",
name, latest_version, target_redirect, query_string
);

let latest_path = format!("/crate/{}/latest{}{}", name, target_redirect, query_string);

metrics
.recently_accessed_releases
Expand All @@ -479,6 +486,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
rendering_time.step("rewrite html");
RustdocPage {
latest_path,
permalink_path,
latest_version,
target,
inner_path,
Expand Down Expand Up @@ -840,25 +848,25 @@ mod test {
let redirect = latest_version_redirect("/dummy/0.1.0/dummy/", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
"/crate/dummy/latest/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
);

// check it keeps the subpage
let redirect = latest_version_redirect("/dummy/0.1.0/dummy/blah/", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-unknown-linux-gnu/dummy/blah/index.html"
"/crate/dummy/latest/target-redirect/x86_64-unknown-linux-gnu/dummy/blah/index.html"
);
let redirect = latest_version_redirect("/dummy/0.1.0/dummy/blah/blah.html", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-unknown-linux-gnu/dummy/blah/blah.html"
"/crate/dummy/latest/target-redirect/x86_64-unknown-linux-gnu/dummy/blah/blah.html"
);

// check it also works for deleted pages
let redirect =
latest_version_redirect("/dummy/0.1.0/dummy/struct.will-be-deleted.html", web)?;
assert_eq!(redirect, "/crate/dummy/0.2.0/target-redirect/x86_64-unknown-linux-gnu/dummy/struct.will-be-deleted.html");
assert_eq!(redirect, "/crate/dummy/latest/target-redirect/x86_64-unknown-linux-gnu/dummy/struct.will-be-deleted.html");

Ok(())
})
Expand Down Expand Up @@ -888,14 +896,14 @@ mod test {
latest_version_redirect("/dummy/0.1.0/x86_64-pc-windows-msvc/dummy", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-pc-windows-msvc/dummy/index.html"
"/crate/dummy/latest/target-redirect/x86_64-pc-windows-msvc/dummy/index.html"
);

let redirect =
latest_version_redirect("/dummy/0.1.0/x86_64-pc-windows-msvc/dummy/", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-pc-windows-msvc/dummy/index.html"
"/crate/dummy/latest/target-redirect/x86_64-pc-windows-msvc/dummy/index.html"
);

let redirect = latest_version_redirect(
Expand All @@ -904,7 +912,7 @@ mod test {
)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-pc-windows-msvc/dummy/struct.Blah.html"
"/crate/dummy/latest/target-redirect/x86_64-pc-windows-msvc/dummy/struct.Blah.html"
);

Ok(())
Expand All @@ -930,7 +938,7 @@ mod test {

let web = env.frontend();
let redirect = latest_version_redirect("/dummy/0.1.0/dummy/", web)?;
assert_eq!(redirect, "/crate/dummy/0.2.0");
assert_eq!(redirect, "/crate/dummy/latest");

Ok(())
})
Expand Down Expand Up @@ -964,56 +972,13 @@ mod test {
let redirect = latest_version_redirect("/dummy/0.1.0/dummy/", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
"/crate/dummy/latest/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
);

let redirect = latest_version_redirect("/dummy/0.2.1/dummy/", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.0/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
);

Ok(())
})
}

#[test_case(true)]
#[test_case(false)]
fn redirect_latest_with_all_yanked(archive_storage: bool) {
wrapper(|env| {
env.fake_release()
.name("dummy")
.version("0.1.0")
.archive_storage(archive_storage)
.rustdoc_file("dummy/index.html")
.yanked(true)
.create()?;
env.fake_release()
.name("dummy")
.version("0.2.0")
.archive_storage(archive_storage)
.rustdoc_file("dummy/index.html")
.yanked(true)
.create()?;
env.fake_release()
.name("dummy")
.version("0.2.1")
.archive_storage(archive_storage)
.rustdoc_file("dummy/index.html")
.yanked(true)
.create()?;

let web = env.frontend();
let redirect = latest_version_redirect("/dummy/0.1.0/dummy/", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.1/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
);

let redirect = latest_version_redirect("/dummy/0.2.0/dummy/", web)?;
assert_eq!(
redirect,
"/crate/dummy/0.2.1/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
"/crate/dummy/latest/target-redirect/x86_64-unknown-linux-gnu/dummy/index.html"
);

Ok(())
Expand Down Expand Up @@ -1807,7 +1772,7 @@ mod test {
"/tungstenite/0.10.0/tungstenite/?search=String%20-%3E%20Message",
env.frontend()
)?,
"/crate/tungstenite/0.11.0/target-redirect/x86_64-unknown-linux-gnu/tungstenite/index.html?search=String%20-%3E%20Message",
"/crate/tungstenite/latest/target-redirect/x86_64-unknown-linux-gnu/tungstenite/index.html?search=String%20-%3E%20Message",
);
Ok(())
});
Expand All @@ -1824,7 +1789,7 @@ mod test {
.source_file("src/objects/exc.rs", b"//! some docs")
.create()?;
env.fake_release().name("pyo3").version("0.13.2").create()?;
let target_redirect = "/crate/pyo3/0.13.2/target-redirect/x86_64-unknown-linux-gnu/src/pyo3/objects/exc.rs.html";
let target_redirect = "/crate/pyo3/latest/target-redirect/x86_64-unknown-linux-gnu/src/pyo3/objects/exc.rs.html";
assert_eq!(
latest_version_redirect(
"/pyo3/0.2.7/src/pyo3/objects/exc.rs.html",
Expand All @@ -1834,7 +1799,7 @@ mod test {
);
assert_redirect(
target_redirect,
"/pyo3/0.13.2/pyo3/?search=exc",
"/pyo3/latest/pyo3/?search=exc",
env.frontend(),
)?;
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion templates/rustdoc/topbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

{%- if metadata.version_or_latest == "latest" -%}
<li class="pure-menu-item">
<a href="{{latest_path | safe}}" class="pure-menu-link description" title="Get a link to this specific version">
<a href="{{permalink_path | safe}}" class="pure-menu-link description" title="Get a link to this specific version">
{{ "link" | fas }} Permalink
</a>
</li>
Expand Down