fix: 增强强制刷新缓存功能 - 添加HTTP禁止缓存头和前端缓存清除
This commit is contained in:
@@ -61,11 +61,26 @@ function argon_is_force_refresh_enabled() {
|
||||
*/
|
||||
function argon_get_assets_version() {
|
||||
if (argon_is_force_refresh_enabled()) {
|
||||
return $GLOBALS['theme_version'] . '.' . time();
|
||||
// 使用启用时间作为版本号,确保同一小时内版本一致但与之前不同
|
||||
$enabled_time = get_option('argon_force_refresh_enabled_time', 0);
|
||||
return $GLOBALS['theme_version'] . '.r' . $enabled_time;
|
||||
}
|
||||
return $GLOBALS['theme_version'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 强制刷新时发送禁止缓存的 HTTP 头
|
||||
*/
|
||||
function argon_force_refresh_headers() {
|
||||
if (!is_admin() && argon_is_force_refresh_enabled()) {
|
||||
header('Cache-Control: no-cache, no-store, must-revalidate, max-age=0');
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
|
||||
header('X-Argon-Force-Refresh: enabled');
|
||||
}
|
||||
}
|
||||
add_action('send_headers', 'argon_force_refresh_headers');
|
||||
|
||||
/**
|
||||
* 启用强制刷新缓存
|
||||
*/
|
||||
|
||||
45
header.php
45
header.php
@@ -292,6 +292,51 @@
|
||||
|
||||
<?php wp_head(); ?>
|
||||
|
||||
<?php if (function_exists('argon_is_force_refresh_enabled') && argon_is_force_refresh_enabled()): ?>
|
||||
<!-- 强制刷新缓存 - 清除浏览器和 Service Worker 缓存 -->
|
||||
<script>
|
||||
(function() {
|
||||
var forceRefreshKey = 'argon_force_refresh_version';
|
||||
var currentVersion = '<?php echo get_option('argon_force_refresh_enabled_time', 0); ?>';
|
||||
var lastVersion = localStorage.getItem(forceRefreshKey);
|
||||
|
||||
// 版本变化时清除所有缓存
|
||||
if (lastVersion !== currentVersion) {
|
||||
localStorage.setItem(forceRefreshKey, currentVersion);
|
||||
|
||||
// 清除 Service Worker 缓存
|
||||
if ('caches' in window) {
|
||||
caches.keys().then(function(names) {
|
||||
names.forEach(function(name) {
|
||||
caches.delete(name);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 注销所有 Service Worker
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
||||
registrations.forEach(function(registration) {
|
||||
registration.unregister();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 如果不是首次访问,强制硬刷新
|
||||
if (lastVersion !== null) {
|
||||
// 清除 sessionStorage
|
||||
try { sessionStorage.clear(); } catch(e) {}
|
||||
|
||||
// 强制硬刷新(绕过缓存)
|
||||
if (window.location.reload) {
|
||||
window.location.reload(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
<?php endif; ?>
|
||||
|
||||
<!-- 移动端资源加载修复 - 确保 CSS 完全应用 -->
|
||||
<script>
|
||||
(function() {
|
||||
|
||||
Reference in New Issue
Block a user