fix(sw): 延迟注册Service Worker以提升页面稳定性

- 在页面加载完成后延迟3秒再注册Service Worker
- 避免Service Worker注册影响页面初始渲染性能
- 确保页面资源加载完成后再进行Service Worker初始化
This commit is contained in:
hehh
2025-11-25 18:49:29 +08:00
parent e0feaf1cda
commit 1531be198d
2 changed files with 19 additions and 14 deletions

View File

@@ -421,13 +421,15 @@
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', function () {
navigator.serviceWorker.register('./js/sw.js')
.then(function (registration) {
console.log('SW registered: ', registration);
})
.catch(function (registrationError) {
console.log('SW registration failed: ', registrationError);
});
setTimeout(() => {
navigator.serviceWorker.register('./js/sw.js')
.then(function(registration) {
console.log('SW registered: ', registration);
})
.catch(function(registrationError) {
console.log('SW registration failed: ', registrationError);
});
}, 3000); // 页面稳定后再注册
});
}
</script>