Skip to content

Commit

Permalink
Migrate menus URLs under /crate/
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 30, 2023
1 parent 529aa33 commit 83aca23
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
56 changes: 42 additions & 14 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,13 +1298,15 @@ mod tests {

fn run_check_links(
env: &TestEnvironment,
url: &str,
url_start: &str,
url_end: &str,
extra: &str,
should_contain_redirect: bool,
) {
run_check_links_redir(
env,
url,
url_start,
url_end,
extra,
should_contain_redirect,
should_contain_redirect,
Expand All @@ -1313,18 +1315,30 @@ mod tests {

fn run_check_links_redir(
env: &TestEnvironment,
url: &str,
url_start: &str,
url_end: &str,
extra: &str,
should_contain_redirect: bool,
ajax_should_contain_redirect: bool,
) {
let response = env.frontend().get(url).send().unwrap();
let response = env
.frontend()
.get(&format!("{url_start}{url_end}"))
.send()
.unwrap();
assert!(response.status().is_success());
let list1 = check_links(response.text().unwrap(), false, should_contain_redirect);
// Same test with AJAX endpoint.
let (start, extra_name) = if url_start.starts_with("/crate/") {
("", "/crate")
} else {
("/crate", "")
};
let response = env
.frontend()
.get(&format!("/-/menus/platforms{url}{extra}"))
.get(&format!(
"{start}{url_start}/menus/platforms{extra_name}{url_end}{extra}"
))
.send()
.unwrap();
assert!(response.status().is_success());
Expand Down Expand Up @@ -1356,17 +1370,31 @@ mod tests {

// FIXME: For some reason, there are target-redirects on non-AJAX lists on docs.rs
// crate pages other than the "default" one.
run_check_links_redir(env, "/crate/dummy/0.4.0/features", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0/builds", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0/source/", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0/source/README.md", "", true, false);

run_check_links(env, "/crate/dummy/0.4.0", "", false);
run_check_links(env, "/dummy/latest/dummy", "/", true);
run_check_links(env, "/dummy/0.4.0/x86_64-pc-windows-msvc/dummy", "/", true);
run_check_links_redir(env, "/crate/dummy/0.4.0", "/features", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0", "/builds", "", true, false);
run_check_links_redir(env, "/crate/dummy/0.4.0", "/source/", "", true, false);
run_check_links_redir(
env,
"/crate/dummy/0.4.0",
"/source/README.md",
"",
true,
false,
);

run_check_links(env, "/crate/dummy/0.4.0", "", "/", false);
run_check_links(env, "/dummy/latest", "/dummy", "/", true);
run_check_links(
env,
"/dummy/0.4.0",
"/x86_64-pc-windows-msvc/dummy",
"/",
true,
);
run_check_links(
env,
"/dummy/0.4.0/x86_64-pc-windows-msvc/dummy/struct.A.html",
"/dummy/0.4.0",
"/x86_64-pc-windows-msvc/dummy/struct.A.html",
"/",
true,
);
Expand Down
26 changes: 11 additions & 15 deletions src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,51 +237,47 @@ pub(super) fn build_axum_routes() -> AxumRouter {
get_internal(super::source::source_browser_handler),
)
.route(
"/-/menus/platforms/crate/:name/:version",
"/crate/:name/:version/menus/platforms/crate/",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/features",
"/crate/:name/:version/menus/platforms/crate/features",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/builds",
"/crate/:name/:version/menus/platforms/crate/builds",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/builds/*path",
"/crate/:name/:version/menus/platforms/crate/builds/*path",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/source/",
"/crate/:name/:version/menus/platforms/crate/source/",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/source/*path",
"/crate/:name/:version/menus/platforms/crate/source/*path",
get_internal(super::crate_details::get_all_platforms_root),
)
.route(
"/-/menus/platforms/crate/:name/:version/:target",
"/crate/:name/:version/menus/platforms/:target",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/crate/:name/:version/:target/*path",
"/crate/:name/:version/menus/platforms/:target/*path",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/:name/:version/",
"/crate/:name/:version/menus/platforms/",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/:name/:version/:target/",
"/crate/:name/:version/menus/platforms/:target/",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/platforms/:name/:version/:target/*path",
get_internal(super::crate_details::get_all_platforms),
)
.route(
"/-/menus/releases/:name",
"/crate/:name/:version/menus/releases",
get_internal(super::crate_details::get_all_releases),
)
.route(
Expand Down
18 changes: 14 additions & 4 deletions static/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ function loadAjaxMenu(menu, id, msg, path, extra) {
// We're not in a documentation page, so no need to do anything.
return;
}
const crateName = window.location.pathname.split('/')[1];
const parts = window.location.pathname.split("/");
let crateName = parts[1];
let version = parts[2];
if (crateName === "crate") {
crateName = parts[2];
version = parts[3];
path += "/crate";
}
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState !== XMLHttpRequest.DONE) {
Expand All @@ -33,7 +40,8 @@ function loadAjaxMenu(menu, id, msg, path, extra) {
document.getElementById(id).innerHTML = `Failed to load ${msg}`;
}
};
xhttp.open("GET", `/-/menus/${path}/${crateName}${extra}`, true);
console.log(extra, path);
xhttp.open("GET", `/crate/${crateName}/${version}/menus/${path}${extra}`, true);
xhttp.send();
};

Expand Down Expand Up @@ -88,13 +96,15 @@ function loadAjaxMenu(menu, id, msg, path, extra) {
if (newMenu.querySelector("#releases-list")) {
loadAjaxMenu(newMenu, "releases-list", "release list", "releases", "");
} else if (newMenu.querySelector("#platforms")) {
const parts = window.location.pathname.split("/");
const startFrom = parts[1] === "crate" ? 4 : 3;
loadAjaxMenu(
newMenu,
"platforms",
"platforms list",
"platforms",
// We get everything except the first crate name.
"/" + window.location.pathname.split("/").slice(2).join("/")
// We get everything except the first crate name and the version.
"/" + parts.slice(startFrom).join("/")
);
}
}
Expand Down

0 comments on commit 83aca23

Please sign in to comment.