Skip to content

Commit 3eac639

Browse files
authored
Rollup merge of #101702 - jsha:static-files2, r=notriddle,GuillaumeGomez
rustdoc: add hash to filename of toolchain files All static files used by rustdoc are now stored in static.files/ and their filenames include a hash of their contents. Their filenames 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. This has a useful side effect: once toolchain files aren't affected by resource suffix, it will become possible for docs.rs to include crate version in the resource suffix. That should fix a caching issue with `/latest/` URLs: rust-lang/docs.rs#1593. My goal is that it should be safe to serve all rustdoc JS, CSS, and fonts with infinite caching headers, even when new versions of a crate are uploaded in the same place as old versions. 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. Example listing: ``` $ cd build/x86_64-unknown-linux-gnu/doc/ && find . | egrep 'js$|css$' | egrep -v 'sidebar-items|implementors' | sort ./crates1.65.0.js ./rust.css ./search-index1.65.0.js ./source-files1.65.0.js ./static.files/ayu-2bfd0af01c176fd5.css ./static.files/dark-95d11b5416841799.css ./static.files/light-c83a97e93a11f15a.css ./static.files/main-efc63f77fb116394.js ./static.files/normalize-76eba96aa4d2e634.css ./static.files/noscript-5bf457055038775c.css ./static.files/rustdoc-7a422337900fa894.css ./static.files/scrape-examples-3dd10048bcead3a4.js ./static.files/search-47f3c289722672cf.js ./static.files/settings-17b08337296ac774.js ./static.files/settings-3f95eacb845293c0.css ./static.files/source-script-215e9db86679192e.js ./static.files/storage-26d846fcae82ff09.js ``` Fixes #98413
2 parents 6b8d9dd + c9dbfe3 commit 3eac639

File tree

18 files changed

+284
-512
lines changed

18 files changed

+284
-512
lines changed

src/librustdoc/config.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,6 @@ pub(crate) struct RenderOptions {
239239
pub(crate) default_settings: FxHashMap<String, String>,
240240
/// If present, suffix added to CSS/JavaScript files when referencing them in generated pages.
241241
pub(crate) resource_suffix: String,
242-
/// Whether to run the static CSS/JavaScript through a minifier when outputting them. `true` by
243-
/// default.
244-
pub(crate) enable_minification: bool,
245242
/// Whether to create an index page in the root of the output directory. If this is true but
246243
/// `enable_index_page` is None, generate a static listing of crates instead.
247244
pub(crate) enable_index_page: bool,
@@ -416,7 +413,9 @@ impl Options {
416413

417414
let to_check = matches.opt_strs("check-theme");
418415
if !to_check.is_empty() {
419-
let paths = match theme::load_css_paths(static_files::themes::LIGHT) {
416+
let paths = match theme::load_css_paths(
417+
std::str::from_utf8(static_files::STATIC_FILES.theme_light_css.bytes).unwrap(),
418+
) {
420419
Ok(p) => p,
421420
Err(e) => {
422421
diag.struct_err(&e.to_string()).emit();
@@ -557,7 +556,9 @@ impl Options {
557556

558557
let mut themes = Vec::new();
559558
if matches.opt_present("theme") {
560-
let paths = match theme::load_css_paths(static_files::themes::LIGHT) {
559+
let paths = match theme::load_css_paths(
560+
std::str::from_utf8(static_files::STATIC_FILES.theme_light_css.bytes).unwrap(),
561+
) {
561562
Ok(p) => p,
562563
Err(e) => {
563564
diag.struct_err(&e.to_string()).emit();
@@ -675,7 +676,6 @@ impl Options {
675676
ModuleSorting::Alphabetical
676677
};
677678
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
678-
let enable_minification = !matches.opt_present("disable-minification");
679679
let markdown_no_toc = matches.opt_present("markdown-no-toc");
680680
let markdown_css = matches.opt_strs("markdown-css");
681681
let markdown_playground_url = matches.opt_str("markdown-playground-url");
@@ -768,7 +768,6 @@ impl Options {
768768
extern_html_root_takes_precedence,
769769
default_settings,
770770
resource_suffix,
771-
enable_minification,
772771
enable_index_page,
773772
index_page,
774773
static_root_path,

src/librustdoc/html/layout.rs

+14-9
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,
@@ -34,17 +35,23 @@ pub(crate) struct Page<'a> {
3435
}
3536

3637
impl<'a> Page<'a> {
37-
pub(crate) fn get_static_root_path(&self) -> &str {
38-
self.static_root_path.unwrap_or(self.root_path)
38+
pub(crate) fn get_static_root_path(&self) -> String {
39+
match self.static_root_path {
40+
Some(s) => s.to_string(),
41+
None => format!("{}static.files/", self.root_path),
42+
}
3943
}
4044
}
4145

4246
#[derive(Template)]
4347
#[template(path = "page.html")]
4448
struct PageLayout<'a> {
45-
static_root_path: &'a str,
49+
static_root_path: String,
4650
page: &'a Page<'a>,
4751
layout: &'a Layout,
52+
53+
files: &'static StaticFiles,
54+
4855
themes: Vec<String>,
4956
sidebar: String,
5057
content: String,
@@ -61,19 +68,17 @@ pub(crate) fn render<T: Print, S: Print>(
6168
) -> String {
6269
let static_root_path = page.get_static_root_path();
6370
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();
71+
let mut themes: Vec<String> = style_files.iter().map(|s| s.basename().unwrap()).collect();
6972
themes.sort();
73+
7074
let rustdoc_version = rustc_interface::util::version_str().unwrap_or("unknown version");
7175
let content = Buffer::html().to_display(t); // Note: This must happen before making the sidebar.
7276
let sidebar = Buffer::html().to_display(sidebar);
7377
PageLayout {
7478
static_root_path,
7579
page,
7680
layout,
81+
files: &STATIC_FILES,
7782
themes,
7883
sidebar,
7984
content,

src/librustdoc/html/render/context.rs

+7-19
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

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

500500
let (sender, receiver) = channel();
501-
let mut scx = SharedContext {
501+
let scx = SharedContext {
502502
tcx,
503503
src_root,
504504
local_sources,
@@ -521,19 +521,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
521521
call_locations,
522522
};
523523

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

@@ -647,10 +634,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
647634
</section>\
648635
</noscript>\
649636
<link rel=\"stylesheet\" type=\"text/css\" \
650-
href=\"{root_path}settings{suffix}.css\">\
651-
<script defer src=\"{root_path}settings{suffix}.js\"></script>",
652-
root_path = page.static_root_path.unwrap_or(""),
653-
suffix = page.resource_suffix,
637+
href=\"{static_root_path}{settings_css}\">\
638+
<script defer src=\"{static_root_path}{settings_js}\"></script>",
639+
static_root_path = page.get_static_root_path(),
640+
settings_css = static_files::STATIC_FILES.settings_css,
641+
settings_js = static_files::STATIC_FILES.settings_js,
654642
)
655643
},
656644
&shared.style_files,

src/librustdoc/html/render/print_item.rs

+4-4
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
visibility_print_with_space, 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,
151-
static_root_path: page.get_static_root_path(),
150+
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)