Skip to content

Commit d8f26cd

Browse files
committed
Add canonical URL for rustdoc pages
Omit the link for crates that specify their own documentation URL.
1 parent c280b04 commit d8f26cd

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/web/crate_details.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct CrateDetails {
3838
pub(crate) metadata: MetaData,
3939
is_library: bool,
4040
license: Option<String>,
41-
documentation_url: Option<String>,
41+
pub(crate) documentation_url: Option<String>,
4242
total_items: Option<f32>,
4343
documented_items: Option<f32>,
4444
total_items_needing_examples: Option<f32>,

src/web/rustdoc.rs

+18
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
195195
#[derive(Debug, Clone, PartialEq, Serialize)]
196196
struct RustdocPage {
197197
latest_path: String,
198+
canonical_url: String,
198199
permalink_path: String,
199200
latest_version: String,
200201
target: String,
@@ -483,6 +484,22 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
483484

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

487+
let canonical_url = if krate.documentation_url.is_none()
488+
|| krate
489+
.documentation_url
490+
.as_ref()
491+
.unwrap()
492+
.starts_with("https://docs.rs/")
493+
{
494+
format!(
495+
"https://docs.rs/{}/latest/{}",
496+
name,
497+
inner_path
498+
)
499+
} else {
500+
"".to_string()
501+
};
502+
486503
metrics
487504
.recently_accessed_releases
488505
.record(krate.crate_id, krate.release_id, target);
@@ -496,6 +513,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
496513
rendering_time.step("rewrite html");
497514
RustdocPage {
498515
latest_path,
516+
canonical_url,
499517
permalink_path,
500518
latest_version,
501519
target,

templates/rustdoc/head.html

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

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

6+
{%- if canonical_url -%}
7+
<link rel="canonical" href="{{canonical_url | safe}}" />
8+
{%- endif -%}
9+
610
<script type="text/javascript">{%- include "theme.js" -%}</script>

0 commit comments

Comments
 (0)