fix(sw): 延迟注册Service Worker以提升页面稳定性
- 在页面加载完成后延迟3秒再注册Service Worker - 避免Service Worker注册影响页面初始渲染性能 - 确保页面资源加载完成后再进行Service Worker初始化
This commit is contained in:
@@ -435,6 +435,7 @@
|
|||||||
<script>
|
<script>
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
window.addEventListener('load', function() {
|
window.addEventListener('load', function() {
|
||||||
|
setTimeout(() => {
|
||||||
navigator.serviceWorker.register('./js/sw.js')
|
navigator.serviceWorker.register('./js/sw.js')
|
||||||
.then(function(registration) {
|
.then(function(registration) {
|
||||||
console.log('SW registered: ', registration);
|
console.log('SW registered: ', registration);
|
||||||
@@ -442,6 +443,8 @@
|
|||||||
.catch(function(registrationError) {
|
.catch(function(registrationError) {
|
||||||
console.log('SW registration failed: ', registrationError);
|
console.log('SW registration failed: ', registrationError);
|
||||||
});
|
});
|
||||||
|
}, 3000); // 页面稳定后再注册
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
@@ -421,6 +421,7 @@
|
|||||||
<script>
|
<script>
|
||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
window.addEventListener('load', function () {
|
window.addEventListener('load', function () {
|
||||||
|
setTimeout(() => {
|
||||||
navigator.serviceWorker.register('./js/sw.js')
|
navigator.serviceWorker.register('./js/sw.js')
|
||||||
.then(function(registration) {
|
.then(function(registration) {
|
||||||
console.log('SW registered: ', registration);
|
console.log('SW registered: ', registration);
|
||||||
@@ -428,6 +429,7 @@
|
|||||||
.catch(function(registrationError) {
|
.catch(function(registrationError) {
|
||||||
console.log('SW registration failed: ', registrationError);
|
console.log('SW registration failed: ', registrationError);
|
||||||
});
|
});
|
||||||
|
}, 3000); // 页面稳定后再注册
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user