Skip to content

Commit 2ab04ac

Browse files
committed
rustdoc: improve static file handling
All static files used by rustdoc are now stored in static.files/ and include a hash of their contents. They no longer include the contents of the --resource-suffix flag. This clarifies caching semantics: anything in static.files can use a long Cache-Control header because any updates will show up as a new URL. Invocation-specific files like crates-NN.js, search-index-NN.js, sidebar-items-NN.js, and implementors/trait.Xyz.js still get the resource suffix.
1 parent 2e35f95 commit 2ab04ac

File tree

10 files changed

+386
-340
lines changed

10 files changed

+386
-340
lines changed

src/librustdoc/html/layout.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::path::PathBuf;
22

33
use rustc_data_structures::fx::FxHashMap;
44

5-
use crate::error::Error;
65
use crate::externalfiles::ExternalHtml;
76
use crate::html::format::{Buffer, Print};
87
use crate::html::render::{ensure_trailing_slash, StylePath};
8+
use crate::html::static_files;
99

1010
use askama::Template;
1111

@@ -45,6 +45,25 @@ struct PageLayout<'a> {
4545
static_root_path: &'a str,
4646
page: &'a Page<'a>,
4747
layout: &'a Layout,
48+
font_preloads: Vec<&'static static_files::StaticFile>,
49+
50+
storage_js: &'static static_files::StaticFile,
51+
main_js: &'static static_files::StaticFile,
52+
source_script_js: &'static static_files::StaticFile,
53+
scrape_examples_js: &'static static_files::StaticFile,
54+
55+
search_js: &'static static_files::StaticFile,
56+
settings_js: &'static static_files::StaticFile,
57+
settings_css: &'static static_files::StaticFile,
58+
59+
favicon_svg: &'static static_files::StaticFile,
60+
favicon_png_16: &'static static_files::StaticFile,
61+
favicon_png_32: &'static static_files::StaticFile,
62+
63+
rust_logo_svg: &'static static_files::StaticFile,
64+
wheel_svg: &'static static_files::StaticFile,
65+
66+
css: Vec<&'static static_files::StaticFile>,
4867
themes: Vec<String>,
4968
sidebar: String,
5069
content: String,
@@ -61,19 +80,47 @@ pub(crate) fn render<T: Print, S: Print>(
6180
) -> String {
6281
let static_root_path = page.get_static_root_path();
6382
let krate_with_trailing_slash = ensure_trailing_slash(&layout.krate).to_string();
64-
let mut themes: Vec<String> = style_files
65-
.iter()
66-
.map(StylePath::basename)
67-
.collect::<Result<_, Error>>()
68-
.unwrap_or_default();
83+
let mut themes: Vec<String> =
84+
style_files.iter().map(|s| s.path.display().to_string()).collect();
85+
themes.push(static_files::themes::LIGHT.to_string());
86+
themes.push(static_files::themes::DARK.to_string());
87+
themes.push(static_files::themes::AYU.to_string());
6988
themes.sort();
89+
let css = vec![&static_files::NORMALIZE_CSS, &static_files::RUSTDOC_CSS];
90+
7091
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
7192
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7293
let sidebar = Buffer::html().to_display(sidebar);
7394
PageLayout {
7495
static_root_path,
7596
page,
7697
layout,
98+
font_preloads: vec![
99+
&static_files::source_serif_4::REGULAR,
100+
&static_files::source_serif_4::BOLD,
101+
&static_files::fira_sans::REGULAR,
102+
&static_files::fira_sans::MEDIUM,
103+
&static_files::source_code_pro::REGULAR,
104+
&static_files::source_code_pro::SEMIBOLD,
105+
],
106+
107+
storage_js: &static_files::STORAGE_JS,
108+
main_js: &static_files::MAIN_JS,
109+
source_script_js: &static_files::sidebar::SOURCE_SCRIPT,
110+
scrape_examples_js: &static_files::SCRAPE_EXAMPLES_JS,
111+
112+
search_js: &static_files::SEARCH_JS,
113+
settings_js: &static_files::SETTINGS_JS,
114+
settings_css: &static_files::SETTINGS_CSS,
115+
116+
favicon_svg: &static_files::RUST_FAVICON_SVG,
117+
favicon_png_16: &static_files::RUST_FAVICON_PNG_16,
118+
favicon_png_32: &static_files::RUST_FAVICON_PNG_32,
119+
120+
rust_logo_svg: &static_files::RUST_LOGO_SVG,
121+
wheel_svg: &static_files::WHEEL_SVG,
122+
123+
css,
77124
themes,
78125
sidebar,
79126
content,

src/librustdoc/html/render/context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
469469
);
470470

471471
let (sender, receiver) = channel();
472-
let mut scx = SharedContext {
472+
let scx = SharedContext {
473473
tcx,
474474
src_root,
475475
local_sources,
@@ -501,9 +501,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
501501
// by the browser as the theme stylesheet. The theme system (hackily) works by
502502
// changing the href to this stylesheet. All other themes are disabled to
503503
// prevent rule conflicts
504-
scx.style_files.push(StylePath { path: PathBuf::from("light.css") });
505-
scx.style_files.push(StylePath { path: PathBuf::from("dark.css") });
506-
scx.style_files.push(StylePath { path: PathBuf::from("ayu.css") });
504+
// scx.style_files.push(StylePath { path: PathBuf::from("light.css") });
505+
// scx.style_files.push(StylePath { path: PathBuf::from("dark.css") });
506+
// scx.style_files.push(StylePath { path: PathBuf::from("ayu.css") });
507507

508508
let dst = output;
509509
scx.ensure_dir(&dst)?;

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl AllTypes {
339339
}
340340

341341
fn scrape_examples_help(shared: &SharedContext<'_>) -> String {
342-
let mut content = SCRAPE_EXAMPLES_HELP_MD.to_owned();
342+
let mut content = SCRAPE_EXAMPLES_HELP_MD.as_str().to_owned();
343343
content.push_str(&format!(
344344
"## More information\n\n\
345345
If you want more information about this feature, please read the [corresponding chapter in the Rustdoc book]({}/rustdoc/scraped-examples.html).",

src/librustdoc/html/render/print_item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ use crate::html::format::{
3030
join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
3131
Buffer, Ending, PrintWithSpace,
3232
};
33-
use crate::html::highlight;
3433
use crate::html::layout::Page;
3534
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
3635
use crate::html::url_parts_builder::UrlPartsBuilder;
36+
use crate::html::{highlight, static_files};
3737

3838
use askama::Template;
3939
use itertools::Itertools;
@@ -52,8 +52,8 @@ struct PathComponent {
5252
#[derive(Template)]
5353
#[template(path = "print_item.html")]
5454
struct ItemVars<'a> {
55-
page: &'a Page<'a>,
5655
static_root_path: &'a str,
56+
clipboard_svg: &'static static_files::StaticFile,
5757
typ: &'a str,
5858
name: &'a str,
5959
item_type: &'a str,
@@ -147,8 +147,8 @@ pub(super) fn print_item(
147147
};
148148

149149
let item_vars = ItemVars {
150-
page,
151150
static_root_path: page.get_static_root_path(),
151+
clipboard_svg: &static_files::CLIPBOARD_SVG,
152152
typ,
153153
name: item.name.as_ref().unwrap().as_str(),
154154
item_type: &item.type_().to_string(),

0 commit comments

Comments
 (0)