Skip to content

Commit 15b73f6

Browse files
committed
feat(html): cache bust static files by adding hashes to file names
Closes #1254
1 parent 542b6fe commit 15b73f6

File tree

16 files changed

+591
-248
lines changed

16 files changed

+591
-248
lines changed

Cargo.lock

Lines changed: 49 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ clap_complete = "4.3.2"
2727
once_cell = "1.17.1"
2828
env_logger = "0.11.1"
2929
handlebars = "6.0"
30+
hex = "0.4"
3031
log = "0.4.17"
3132
memchr = "2.5.0"
3233
opener = "0.7.0"
3334
pulldown-cmark = { version = "0.10.0", default-features = false, features = ["html"] } # Do not update, part of the public api.
3435
regex = "1.8.1"
3536
serde = { version = "1.0.163", features = ["derive"] }
3637
serde_json = "1.0.96"
38+
sha2 = "0.9"
3739
shlex = "1.3.0"
3840
tempfile = "3.4.0"
3941
toml = "0.5.11" # Do not update, see https://github.com/rust-lang/mdBook/issues/2037

guide/book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mathjax-support = true
1313
site-url = "/mdBook/"
1414
git-repository-url = "https://github.com/rust-lang/mdBook/tree/master/guide"
1515
edit-url-template = "https://github.com/rust-lang/mdBook/edit/master/guide/{path}"
16+
hash-files = true
1617

1718
[output.html.playground]
1819
editable = true

guide/src/format/configuration/renderers.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ The following configuration options are available:
168168
This string will be written to a file named CNAME in the root of your site, as
169169
required by GitHub Pages (see [*Managing a custom domain for your GitHub Pages
170170
site*][custom domain]).
171+
- **hash-files:** Include a cryptographic "fingerprint" of the files' contents in CSS, JavaScript, and image asset filenames,
172+
so that if the contents of the file are changed, the name of the file will also change.
173+
For example, `css/chrome.css` may become `css/chrome-9b8f428e.css`.
174+
HTML files are not renamed.
175+
Static CSS and JS files can reference each other using `{{ resource "filename" }}` directives.
171176

172177
[custom domain]: https://docs.github.com/en/github/working-with-github-pages/managing-a-custom-domain-for-your-github-pages-site
173178

guide/src/format/theme/index-hbs.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,13 @@ Of course the inner html can be changed to your liking.
9999

100100
*If you would like other properties or helpers exposed, please [create a new
101101
issue](https://github.com/rust-lang/mdBook/issues)*
102+
103+
### 3. resource
104+
105+
The path to a static file.
106+
It implicitly includes `path_to_root`,
107+
and accounts for files that are renamed with a hash in their filename.
108+
109+
```handlebars
110+
<link rel="stylesheet" href="{{ resource "css/chrome.css" }}">
111+
```

src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,9 @@ pub struct HtmlConfig {
587587
/// The mapping from old pages to new pages/URLs to use when generating
588588
/// redirects.
589589
pub redirect: HashMap<String, String>,
590+
/// If this option is turned on, "cache bust" static files by adding
591+
/// hashes to their file names.
592+
pub hash_files: bool,
590593
}
591594

592595
impl Default for HtmlConfig {
@@ -616,6 +619,7 @@ impl Default for HtmlConfig {
616619
cname: None,
617620
live_reload_endpoint: None,
618621
redirect: HashMap::new(),
622+
hash_files: false,
619623
}
620624
}
621625
}

0 commit comments

Comments
 (0)