Skip to content

Commit ebaf774

Browse files
committed
Emit canonical URLs for all rustdoc pages
Previously, we did not emit canonical URLs for crates that had an alternate documentation URL. However, this produces bad results for those crates, with older versions being selected as canonical even if the latest version uses docs.rs as its documentation URL.
1 parent 24bf3b2 commit ebaf774

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

src/web/rustdoc.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -490,25 +490,16 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
490490
let latest_path = format!("/crate/{}/latest{}{}", name, target_redirect, query_string);
491491

492492
// Set the canonical URL for search engines to the `/latest/` page on docs.rs.
493-
// For crates with a documentation URL, where that URL doesn't point at docs.rs,
494-
// omit the canonical link to avoid penalizing external documentation.
495493
// Note: The URL this points to may not exist. For instance, if we're rendering
496494
// `struct Foo` in version 0.1.0 of a crate, and version 0.2.0 of that crate removes
497495
// `struct Foo`, this will point at a 404. That's fine: search engines will crawl
498496
// the target and will not canonicalize to a URL that doesn't exist.
499-
let canonical_url = if krate.documentation_url.is_none()
500-
|| krate
501-
.documentation_url
502-
.as_ref()
503-
.unwrap()
504-
.starts_with("https://docs.rs/")
505-
{
506-
// Don't include index.html in the canonical URL.
507-
let canonical_path = inner_path.replace("index.html", "");
508-
format!("https://docs.rs/{}/latest/{}", name, canonical_path)
509-
} else {
510-
"".to_string()
511-
};
497+
// Don't include index.html in the canonical URL.
498+
let canonical_url = format!(
499+
"https://docs.rs/{}/latest/{}",
500+
name,
501+
inner_path.replace("index.html", "")
502+
);
512503

513504
metrics
514505
.recently_accessed_releases
@@ -2063,7 +2054,7 @@ mod test {
20632054

20642055
let web = env.frontend();
20652056

2066-
assert!(!web
2057+
assert!(web
20672058
.get("/dummy-dash/0.1.0/dummy_dash/")
20682059
.send()?
20692060
.text()?

templates/rustdoc/head.html

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
<link rel="search" href="/-/static/opensearch.xml" type="application/opensearchdescription+xml" title="Docs.rs" />
55

6-
{%- if canonical_url -%}
76
<link rel="canonical" href="{{canonical_url | safe}}" />
8-
{%- endif -%}
97

108
<script type="text/javascript">{%- include "theme.js" -%}</script>

0 commit comments

Comments
 (0)