From 2062a11e7aae29aeef3e4ef27742bb4ec0ad02a3 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Sun, 18 Jun 2023 23:24:01 +0200 Subject: [PATCH] Implement cache busting: append a suffix to CSS and JS URLs --- docs/user-guide/configuration.md | 44 +++++++++++ mkdocs/commands/build.py | 6 +- mkdocs/config/config_options.py | 2 +- mkdocs/config/defaults.py | 5 ++ mkdocs/structure/files.py | 85 +++++++++++++++++++--- mkdocs/tests/build_tests.py | 2 +- mkdocs/theme.py | 25 +++++-- mkdocs/themes/mkdocs/mkdocs_theme.yml | 6 ++ mkdocs/themes/readthedocs/mkdocs_theme.yml | 4 + mkdocs/utils/__init__.py | 9 ++- mkdocs/utils/templates.py | 7 +- 11 files changed, 171 insertions(+), 24 deletions(-) diff --git a/docs/user-guide/configuration.md b/docs/user-guide/configuration.md index 401caa4a..75481097 100644 --- a/docs/user-guide/configuration.md +++ b/docs/user-guide/configuration.md @@ -497,6 +497,50 @@ Only the plain string variant detects the `.mjs` extension and adds `type="modul NOTE: `*.js` and `*.css` files, just like any other type of file, are always copied from `docs_dir` into the site's deployed copy, regardless if they're linked to the pages via the above configs or not. +### hash_rename_assets + +NEW: **New in version 1.5.** + +Set patterns of files to rename (on the fly, upon copying to the built site) by inserting a hash of the content. This is done for purposes of *cache busting*. + +It is recommended to use this setting whenever possible. + +The patterns follow the [.gitignore pattern format](https://git-scm.com/docs/gitignore#_pattern_format). But in this case these are positive matches, not "ignore" matches. + +For example: + +```yaml +extra_javascript: + - js/foo.js + - vendored/jquery-1.2.3.js +hash_rename_assets: | + *.css + *.js + !/vendored/*.js +``` + +Then the matched file is copied with a modified name and references to it are modified accordingly, e.g.: + +```html +