如何强制Sphinx中的自定义js/css文件始终加载更改而不缓存?

问题描述 投票:0回答:1

conf.py
我有:

html_css_files = ['css/custom.css']
html_js_files = ['js/custom.js']

但是,如果我对

custom.js
custom.css
进行任何更改,则此更改不会反映在用户的浏览器中,除非 http://www.example.com/_static/js/custom.js 被强制重新加载客户端(类似 CSS)。

如何强制自定义CSS + JS永远不会在用户的浏览器上缓存?

javascript css caching python-sphinx browser-cache
1个回答
0
投票

conf.py
的顶部添加以下行:

from secrets import token_urlsafe

将自定义 CSS + JS 更改为:

html_css_files = ['css/custom.css?v=' + token_urlsafe(8)[:8]]
html_js_files = ['js/custom.js?v=' + token_urlsafe(8)[:8]]

这将使您的自定义 CSS + JS 文件像普通 CSS + JS 文件一样加载,并在末尾带有随机的 8 字符 slug - 从而禁用缓存:

<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: -->
<link rel="stylesheet" type="text/css" href="../_static/css/custom.css?v=Rdy7RNCQ" />

<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: -->
<script src="../_static/js/custom.js?v=8QronLhw"></script>
© www.soinside.com 2019 - 2024. All rights reserved.