feat: 增加前端自动刷新功能
- 新增自动刷新选项,检测到版本更新时自动刷新用户浏览器 - 使用 localStorage 存储版本号进行比对 - 设置5秒冷却期防止刷新循环 - 刷新前清理浏览器 Service Worker 缓存
This commit is contained in:
@@ -335,6 +335,63 @@ function argon_frontend_hot_reload_notice() {
|
||||
}
|
||||
add_action('wp_footer', 'argon_frontend_hot_reload_notice');
|
||||
|
||||
// 前端自动刷新脚本
|
||||
function argon_hot_reload_auto_refresh_script() {
|
||||
if (get_option('argon_enable_hot_reload', 'false') != 'true') {
|
||||
return;
|
||||
}
|
||||
if (get_option('argon_hot_reload_auto_refresh', 'false') != 'true') {
|
||||
return;
|
||||
}
|
||||
|
||||
$current_version = $GLOBALS['theme_version'];
|
||||
?>
|
||||
<script id="argon-hot-reload-checker">
|
||||
(function() {
|
||||
var currentVersion = '<?php echo esc_js($current_version); ?>';
|
||||
var storageKey = 'argon_theme_version';
|
||||
var lastRefreshKey = 'argon_last_refresh';
|
||||
var refreshCooldown = 5000; // 5秒冷却,防止刷新循环
|
||||
|
||||
// 获取存储的版本
|
||||
var storedVersion = localStorage.getItem(storageKey);
|
||||
var lastRefresh = parseInt(localStorage.getItem(lastRefreshKey) || '0', 10);
|
||||
var now = Date.now();
|
||||
|
||||
// 如果没有存储版本,先存储当前版本
|
||||
if (!storedVersion) {
|
||||
localStorage.setItem(storageKey, currentVersion);
|
||||
return;
|
||||
}
|
||||
|
||||
// 版本不同且不在冷却期内,执行刷新
|
||||
if (storedVersion !== currentVersion && (now - lastRefresh) > refreshCooldown) {
|
||||
// 先更新存储,防止刷新循环
|
||||
localStorage.setItem(storageKey, currentVersion);
|
||||
localStorage.setItem(lastRefreshKey, now.toString());
|
||||
|
||||
// 清理浏览器缓存后刷新
|
||||
if ('caches' in window) {
|
||||
caches.keys().then(function(names) {
|
||||
names.forEach(function(name) {
|
||||
caches.delete(name);
|
||||
});
|
||||
}).then(function() {
|
||||
location.reload(true);
|
||||
});
|
||||
} else {
|
||||
location.reload(true);
|
||||
}
|
||||
} else if (storedVersion !== currentVersion) {
|
||||
// 版本不同但在冷却期,只更新存储
|
||||
localStorage.setItem(storageKey, currentVersion);
|
||||
}
|
||||
})();
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
add_action('wp_head', 'argon_hot_reload_auto_refresh_script', 1);
|
||||
|
||||
// 初始化热更新检测
|
||||
add_action('init', 'argon_hot_reload_init');
|
||||
|
||||
|
||||
16
settings.php
16
settings.php
@@ -4655,6 +4655,20 @@ window.pjaxLoaded = function(){
|
||||
|
||||
|
||||
|
||||
<?php $argon_hot_reload_auto_refresh = get_option('argon_hot_reload_auto_refresh', 'false'); ?>
|
||||
|
||||
<label style="display:block;margin-top:10px;">
|
||||
|
||||
<input type="checkbox" name="argon_hot_reload_auto_refresh" value="true" <?php if ($argon_hot_reload_auto_refresh=='true'){echo 'checked';}?>/>
|
||||
|
||||
<?php _e('自动刷新页面(检测到版本更新时自动刷新用户浏览器)', 'argon');?>
|
||||
|
||||
</label>
|
||||
|
||||
<p class="description" style="margin-left:24px;"><?php _e('推荐开启。用户访问时如检测到主题版本变化,将自动刷新页面以加载最新功能。', 'argon');?></p>
|
||||
|
||||
|
||||
|
||||
<?php $argon_hot_reload_frontend_notice = get_option('argon_hot_reload_frontend_notice', 'false'); ?>
|
||||
|
||||
<label style="display:block;margin-top:10px;">
|
||||
@@ -5406,6 +5420,8 @@ function argon_update_themeoptions(){
|
||||
|
||||
argon_update_option_checkbox('argon_enable_hot_reload');
|
||||
|
||||
argon_update_option_checkbox('argon_hot_reload_auto_refresh');
|
||||
|
||||
argon_update_option_checkbox('argon_hot_reload_frontend_notice');
|
||||
|
||||
argon_update_option('argon_enable_into_article_animation');
|
||||
|
||||
Reference in New Issue
Block a user