Skip to content

Commit 7db45e4

Browse files
rustdoc only use cors when needed
The `crossorigin` attribute may cause issues when the href is not actuall across origins. Specifically, the tag causes the browser to send a preflight OPTIONS request to the href even if it is same-origin. Some tempermental servers may reject all CORS preflect requests even if they're actually same-origin, which causes a CORS error and prevents the fonts from loading, even later on. This commit fixes that problem by not emitting `crossorigin` if the url looks like a ___domain-relative url. Co-authored-by: Guillaume Gomez <[email protected]>
1 parent a955f1c commit 7db45e4

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/librustdoc/html/layout.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,18 @@ struct PageLayout<'a> {
6868
display_krate_version_extra: &'a str,
6969
}
7070

71+
impl PageLayout<'_> {
72+
/// Conservatively determines if [`Self::static_root_path`] is relative to the current origin,
73+
/// so that `crossorigin` may be safely removed from `<link>` elements.
74+
fn static_root_path_may_remove_crossorigin(&self) -> bool {
75+
let href = &*self.static_root_path;
76+
// Everything up to the first `/`, or the whole str.
77+
let first_part = href.split_once('/').map_or(href, |(first, _)| first);
78+
// Reject protocol-relative URLs (`//example.com/`) or anything with a protocol (`http:`, etc.)
79+
!(href.starts_with("//") || first_part.contains(':'))
80+
}
81+
}
82+
7183
pub(crate) use crate::html::render::sidebar::filters;
7284

7385
pub(crate) fn render<T: Display, S: Display>(

src/librustdoc/html/templates/page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="description" content="{{page.description}}"> {# #}
88
<title>{{page.title}}</title> {# #}
99
<script>if(window.___location.protocol!=="file:") {# Hack to skip preloading fonts locally - see #98769 #}
10-
document.head.insertAdjacentHTML("beforeend","{{files.source_serif_4_regular}},{{files.fira_sans_italic}},{{files.fira_sans_regular}},{{files.fira_sans_medium_italic}},{{files.fira_sans_medium}},{{files.source_code_pro_regular}},{{files.source_code_pro_semibold}}".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}${f}">`).join("")) {# #}
10+
document.head.insertAdjacentHTML("beforeend","{{files.source_serif_4_regular}},{{files.fira_sans_italic}},{{files.fira_sans_regular}},{{files.fira_sans_medium_italic}},{{files.fira_sans_medium}},{{files.source_code_pro_regular}},{{files.source_code_pro_semibold}}".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2"{% if !static_root_path_may_remove_crossorigin() %} crossorigin{% endif %} href="{{static_root_path|safe}}${f}">`).join("")) {# #}
1111
</script> {# #}
1212
<link rel="stylesheet" {#+ #}
1313
href="{{static_root_path|safe}}{{files.normalize_css}}"> {# #}

0 commit comments

Comments
 (0)