fix(service-worker): enhance unregister logic and update index.html
- Updated the index.html to load the env-config.js script as a module for better compatibility. - Improved the unregister logic in both the unregister-sw.js and login-page.tsx to specifically target the ngsw-worker, ensuring it is unregistered correctly and the page reloads afterward. This prevents multiple unregister attempts and enhances user experience.
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
/>
|
/>
|
||||||
<title>Worklenz</title>
|
<title>Worklenz</title>
|
||||||
<!-- Environment configuration -->
|
<!-- Environment configuration -->
|
||||||
<script src="/env-config.js"></script>
|
<script type="module" src="/env-config.js"></script>
|
||||||
<!-- Unregister service worker -->
|
<!-- Unregister service worker -->
|
||||||
<script src="/unregister-sw.js"></script>
|
<script src="/unregister-sw.js"></script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -2,13 +2,18 @@ if ('serviceWorker' in navigator) {
|
|||||||
// Check if we've already attempted to unregister in this session
|
// Check if we've already attempted to unregister in this session
|
||||||
if (!sessionStorage.getItem('swUnregisterAttempted')) {
|
if (!sessionStorage.getItem('swUnregisterAttempted')) {
|
||||||
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
||||||
if (registrations.length > 0) {
|
const ngswWorker = registrations.find(reg => reg.active?.scriptURL.includes('ngsw-worker'));
|
||||||
|
|
||||||
|
if (ngswWorker) {
|
||||||
// Mark that we've attempted to unregister
|
// Mark that we've attempted to unregister
|
||||||
sessionStorage.setItem('swUnregisterAttempted', 'true');
|
sessionStorage.setItem('swUnregisterAttempted', 'true');
|
||||||
// If there are registered service workers, do a hard reload first
|
// Unregister the ngsw-worker
|
||||||
window.location.reload(true);
|
ngswWorker.unregister().then(() => {
|
||||||
|
// Reload the page after unregistering
|
||||||
|
window.location.reload(true);
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
// If no service workers are registered, unregister any that might be pending
|
// If no ngsw-worker is found, unregister any other service workers
|
||||||
for(let registration of registrations) {
|
for(let registration of registrations) {
|
||||||
registration.unregister();
|
registration.unregister();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,6 +77,18 @@ const LoginPage: React.FC = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Check and unregister ngsw-worker if present
|
||||||
|
if ('serviceWorker' in navigator) {
|
||||||
|
navigator.serviceWorker.getRegistrations().then(function(registrations) {
|
||||||
|
const ngswWorker = registrations.find(reg => reg.active?.scriptURL.includes('ngsw-worker'));
|
||||||
|
if (ngswWorker) {
|
||||||
|
ngswWorker.unregister().then(() => {
|
||||||
|
window.location.reload();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
trackMixpanelEvent(evt_login_page_visit);
|
trackMixpanelEvent(evt_login_page_visit);
|
||||||
if (currentSession && !currentSession?.setup_completed) {
|
if (currentSession && !currentSession?.setup_completed) {
|
||||||
navigate('/worklenz/setup');
|
navigate('/worklenz/setup');
|
||||||
|
|||||||
Reference in New Issue
Block a user