Skip to content

Commit

Permalink
fix: update service worker when navigating
Browse files Browse the repository at this point in the history
  • Loading branch information
devrsi0n committed Oct 30, 2024
1 parent 0086d2a commit 7f6a80c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/bundle-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@ jobs:
NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }}
NEXT_PUBLIC_APP_URL: ${{ secrets.NEXT_PUBLIC_APP_URL }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
PRIVATE_VAPID: ${{ secrets.PRIVATE_VAPID }}
EMAIL_API_KEY: ${{ secrets.EMAIL_API_KEY }}
STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_KEY }}
STRIPE_SIGN_SECRET: ${{ secrets.STRIPE_SIGN_SECRET }}
SW_CACHE_ID: ${{ secrets.SW_CACHE_ID }}
QSTASH_NEXT_SIGNING_KEY: ${{ secrets.QSTASH_NEXT_SIGNING_KEY }}
QSTASH_CURRENT_SIGNING_KEY: ${{ secrets.QSTASH_CURRENT_SIGNING_KEY }}
TINYBIRD_ADMIN_TOKEN: ${{ secrets.TINYBIRD_ADMIN_TOKEN }}
CRON_SECRET: ${{ secrets.CRON_SECRET }}
NEXT_PUBLIC_VAPID: ${{ secrets.NEXT_PUBLIC_VAPID }}
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: ${{ secrets.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY }}
11 changes: 11 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ jobs:
NEXTAUTH_URL: ${{ secrets.NEXTAUTH_URL }}
NEXT_PUBLIC_APP_URL: ${{ secrets.NEXT_PUBLIC_APP_URL }}
SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
PRIVATE_VAPID: ${{ secrets.PRIVATE_VAPID }}
EMAIL_API_KEY: ${{ secrets.EMAIL_API_KEY }}
STRIPE_SECRET_KEY: ${{ secrets.STRIPE_SECRET_KEY }}
STRIPE_SIGN_SECRET: ${{ secrets.STRIPE_SIGN_SECRET }}
SW_CACHE_ID: ${{ secrets.SW_CACHE_ID }}
QSTASH_NEXT_SIGNING_KEY: ${{ secrets.QSTASH_NEXT_SIGNING_KEY }}
QSTASH_CURRENT_SIGNING_KEY: ${{ secrets.QSTASH_CURRENT_SIGNING_KEY }}
TINYBIRD_ADMIN_TOKEN: ${{ secrets.TINYBIRD_ADMIN_TOKEN }}
CRON_SECRET: ${{ secrets.CRON_SECRET }}
NEXT_PUBLIC_VAPID: ${{ secrets.NEXT_PUBLIC_VAPID }}
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY: ${{ secrets.NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY }}
Original file line number Diff line number Diff line change
Expand Up @@ -27,52 +27,59 @@ export function useReloadWhenSwChange() {
}

navigator.serviceWorker.register('/sw.js').then((registration) => {
// Track updates to the Service Worker.
if (!navigator.serviceWorker.controller) {
// The window client isn't currently controlled so it's a new service
// worker that will activate immediately
return;
}
registration.update();

handleNewServiceWorker(registration, () => {
if (!registration.waiting) {
// Just to ensure registration.waiting is available before
// calling postMessage()
return;
// Handle both page refresh/close and SPA navigation
const skipWaitingOnNavigation = () => {
if (registration.waiting) {
logger.debug('Activating new service worker on navigation');
registration.waiting.postMessage('skipWaiting');
}
};

registration.waiting.postMessage('skipWaiting');
});
});
}, []);
}
// Listen for regular page unload
window.addEventListener('beforeunload', skipWaitingOnNavigation);

function handleNewServiceWorker(
registration: ServiceWorkerRegistration,
callback: () => void,
) {
if (registration.waiting) {
// SW is waiting to activate. Can occur if multiple clients open and
// one of the clients is refreshed.
return callback();
}
// Listen for SPA navigation
window.addEventListener('popstate', skipWaitingOnNavigation);
const originalPushState = history.pushState.bind(history);
const originalReplaceState = history.replaceState.bind(history);

function listenInstalledStateChange() {
registration.installing?.addEventListener('statechange', (event: Event) => {
// @ts-ignore
if (event.target?.state === 'installed') {
// A new service worker is available, inform the user
callback();
}
});
}
history.pushState = (...args) => {
originalPushState(...args);
skipWaitingOnNavigation();
};

history.replaceState = (...args) => {
originalReplaceState(...args);
skipWaitingOnNavigation();
};

if (registration.installing) {
return listenInstalledStateChange();
}
registration.addEventListener('updatefound', () => {
registration.installing?.addEventListener(
'statechange',
(event: Event) => {
// @ts-ignore
if (event.target?.state === 'installed' && registration.waiting) {
// Will activate on next navigation
logger.debug(
'New service worker installed, waiting for navigation',
);
}
},
);
});

// We are currently controlled so a new SW may be found...
// Add a listener in case a new SW is found,
registration.addEventListener('updatefound', listenInstalledStateChange);
// Cleanup
return () => {
window.removeEventListener('beforeunload', skipWaitingOnNavigation);
window.removeEventListener('popstate', skipWaitingOnNavigation);
history.pushState = originalPushState;
history.replaceState = originalReplaceState;
};
});
}, []);
}

0 comments on commit 7f6a80c

Please sign in to comment.