Skip to content

Commit 8b97052

Browse files
committed
rustdoc: add hash to filename of toolchain files
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 Cache-Control: immutable because any updates will show up as a new URL. Invocation-specific files like crates-NN.js, search-index-NN.js, and sidebar-items-NN.js still get the resource suffix. The --disable-minification flag is removed because it would vary the output of static files based on invocation flags. Instead, for rustdoc development purposes it's preferable to symlink static files to a non-minified copy for quick iteration.
1 parent 8ab71ab commit 8b97052

File tree

15 files changed

+398
-479
lines changed

15 files changed

+398
-479
lines changed

src/librustdoc/config.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,6 @@ pub(crate) struct RenderOptions {
237237
pub(crate) default_settings: FxHashMap<String, String>,
238238
/// If present, suffix added to CSS/JavaScript files when referencing them in generated pages.
239239
pub(crate) resource_suffix: String,
240-
/// Whether to run the static CSS/JavaScript through a minifier when outputting them. `true` by
241-
/// default.
242-
pub(crate) enable_minification: bool,
243240
/// Whether to create an index page in the root of the output directory. If this is true but
244241
/// `enable_index_page` is None, generate a static listing of crates instead.
245242
pub(crate) enable_index_page: bool,
@@ -412,7 +409,9 @@ impl Options {
412409

413410
let to_check = matches.opt_strs("check-theme");
414411
if !to_check.is_empty() {
415-
let paths = match theme::load_css_paths(static_files::themes::LIGHT) {
412+
let paths = match theme::load_css_paths(
413+
std::str::from_utf8(static_files::STATIC_FILES.theme_light_css.bytes).unwrap(),
414+
) {
416415
Ok(p) => p,
417416
Err(e) => {
418417
diag.struct_err(&e.to_string()).emit();
@@ -553,7 +552,9 @@ impl Options {
553552

554553
let mut themes = Vec::new();
555554
if matches.opt_present("theme") {
556-
let paths = match theme::load_css_paths(static_files::themes::LIGHT) {
555+
let paths = match theme::load_css_paths(
556+
std::str::from_utf8(static_files::STATIC_FILES.theme_light_css.bytes).unwrap(),
557+
) {
557558
Ok(p) => p,
558559
Err(e) => {
559560
diag.struct_err(&e.to_string()).emit();
@@ -671,7 +672,6 @@ impl Options {
671672
ModuleSorting::Alphabetical
672673
};
673674
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
674-
let enable_minification = !matches.opt_present("disable-minification");
675675
let markdown_no_toc = matches.opt_present("markdown-no-toc");
676676
let markdown_css = matches.opt_strs("markdown-css");
677677
let markdown_playground_url = matches.opt_str("markdown-playground-url");
@@ -756,7 +756,6 @@ impl Options {
756756
extern_html_root_takes_precedence,
757757
default_settings,
758758
resource_suffix,
759-
enable_minification,
760759
enable_index_page,
761760
index_page,
762761
static_root_path,

src/librustdoc/html/layout.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ 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};
98

109
use askama::Template;
1110

11+
use super::static_files::{StaticFiles, STATIC_FILES};
12+
1213
#[derive(Clone)]
1314
pub(crate) struct Layout {
1415
pub(crate) logo: String,
@@ -45,6 +46,9 @@ struct PageLayout<'a> {
4546
static_root_path: &'a str,
4647
page: &'a Page<'a>,
4748
layout: &'a Layout,
49+
50+
files: &'static StaticFiles,
51+
4852
themes: Vec<String>,
4953
sidebar: String,
5054
content: String,
@@ -61,19 +65,17 @@ pub(crate) fn render<T: Print, S: Print>(
6165
) -> String {
6266
let static_root_path = page.get_static_root_path();
6367
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();
68+
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
6969
themes.sort();
70+
7071
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
7172
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7273
let sidebar = Buffer::html().to_display(sidebar);
7374
PageLayout {
7475
static_root_path,
7576
page,
7677
layout,
78+
files: &STATIC_FILES,
7779
themes,
7880
sidebar,
7981
content,

src/librustdoc/html/render/context.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use crate::html::escape::Escape;
3232
use crate::html::format::{join_with_double_colon, Buffer};
3333
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
3434
use crate::html::url_parts_builder::UrlPartsBuilder;
35-
use crate::html::{layout, sources};
35+
use crate::html::{layout, sources, static_files};
3636
use crate::scrape_examples::AllCallLocations;
3737
use crate::try_err;
3838

@@ -499,7 +499,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
499499
);
500500

501501
let (sender, receiver) = channel();
502-
let mut scx = SharedContext {
502+
let scx = SharedContext {
503503
tcx,
504504
src_root,
505505
local_sources,
@@ -522,19 +522,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
522522
call_locations,
523523
};
524524

525-
// Add the default themes to the `Vec` of stylepaths
526-
//
527-
// Note that these must be added before `sources::render` is called
528-
// so that the resulting source pages are styled
529-
//
530-
// `light.css` is not disabled because it is the stylesheet that stays loaded
531-
// by the browser as the theme stylesheet. The theme system (hackily) works by
532-
// changing the href to this stylesheet. All other themes are disabled to
533-
// prevent rule conflicts
534-
scx.style_files.push(StylePath { path: PathBuf::from("light.css") });
535-
scx.style_files.push(StylePath { path: PathBuf::from("dark.css") });
536-
scx.style_files.push(StylePath { path: PathBuf::from("ayu.css") });
537-
538525
let dst = output;
539526
scx.ensure_dir(&dst)?;
540527

@@ -649,10 +636,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
649636
</section>\
650637
</noscript>\
651638
<link rel=\"stylesheet\" type=\"text/css\" \
652-
href=\"{root_path}settings{suffix}.css\">\
653-
<script defer src=\"{root_path}settings{suffix}.js\"></script>",
654-
root_path = page.static_root_path.unwrap_or(""),
655-
suffix = page.resource_suffix,
639+
href=\"{static_root_path}{settings_css}\">\
640+
<script defer src=\"{static_root_path}{settings_js}\"></script>",
641+
static_root_path = page.static_root_path.unwrap_or(""),
642+
settings_css = static_files::STATIC_FILES.settings_css,
643+
settings_js = static_files::STATIC_FILES.settings_js,
656644
)
657645
},
658646
&shared.style_files,

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::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)