尽管配置文件看起来只接受普通字符串值,但它实际上只是一个Python脚本,因此我们可以导入和使用随机数生成器等函数.
在conf.py
的顶部添加以下一行:
from secrets import token_urlsafe
将您的自定义CSS+JS更改为:
html_css_files = [f'css/custom.css?v={token_urlsafe(8)[:8]}']
html_js_files = [f'js/custom.js?v={token_urlsafe(8)[:8]}']
这将使您的定制的css+js文件像普通的css+js文件一样加载,末尾带有一个随机的8个字符的插件--从而禁用缓存:
<!-- Sphinx vanilla CSS: -->
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
<link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=19f00094" />
<link rel="stylesheet" type="text/css" href="../_static/copybutton.css?v=76b2166b" />
<!-- Our custom CSS now has a random slug, like vanilla: -->
<link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=Rdy7RNCQ" />
<!-- Sphinx vanilla JS: -->
<script src="../_static/jquery.js?v=5d32c60e"></script>
<script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js?v=e91dff3d"></script>
<script src="../_static/doctools.js?v=888ff710"></script>
<script src="../_static/sphinx_highlight.js?v=4825356b"></script>
<script src="../_static/clipboard.min.js?v=a7894cd8"></script>
<script src="../_static/copybutton.js?v=f281be69"></script>
<!-- Our custom JS now has a random slug, like vanilla: -->
<script src="../_static/js/custom.js?v=8QronLhw"></script>
请注意,如果您在Docker中运行Sphinx,则?v=xxxxxxxx
slug不会更改,除非您使用--no-cache
选项运行docker build
.