fix(service-worker): prevent multiple unregister attempts in session
- Updated the unregister script to check if an attempt to unregister service workers has already been made in the current session, preventing unnecessary reloads and improving user experience. - If service workers are registered, the script will perform a hard reload; otherwise, it will unregister any pending registrations.
This commit is contained in:
@@ -1,6 +1,10 @@
|
|||||||
if ('serviceWorker' in navigator) {
|
if ('serviceWorker' in navigator) {
|
||||||
|
// Check if we've already attempted to unregister in this session
|
||||||
|
if (!sessionStorage.getItem('swUnregisterAttempted')) {
|
||||||
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
||||||
if (registrations.length > 0) {
|
if (registrations.length > 0) {
|
||||||
|
// Mark that we've attempted to unregister
|
||||||
|
sessionStorage.setItem('swUnregisterAttempted', 'true');
|
||||||
// If there are registered service workers, do a hard reload first
|
// If there are registered service workers, do a hard reload first
|
||||||
window.location.reload(true);
|
window.location.reload(true);
|
||||||
} else {
|
} else {
|
||||||
@@ -10,4 +14,5 @@ if ('serviceWorker' in navigator) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user