refactor: Simplify service worker implementation
Remove over-engineering from asset caching service worker: - Remove retry logic, stats tracking, unused message handlers - Inline helper functions used only once - Reduce sw-runtime.js from 314 to 170 lines (46% reduction) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.5
parent
d34df7c314
commit
f713678328
@@ -1,8 +1,14 @@
|
||||
/* eslint-disable no-console */
|
||||
import NotificationSubscriptions from '../api/notificationSubscription';
|
||||
import auth from '../api/auth';
|
||||
import { useAlert } from 'dashboard/composables';
|
||||
|
||||
/**
|
||||
* Request the service worker to prefetch all assets in the manifest.
|
||||
*/
|
||||
const prefetchAssets = () => {
|
||||
navigator.serviceWorker?.controller?.postMessage({ type: 'PREFETCH_ASSETS' });
|
||||
};
|
||||
|
||||
export const verifyServiceWorkerExistence = (callback = () => {}) => {
|
||||
if (!('serviceWorker' in navigator)) {
|
||||
// Service Worker isn't supported on this browser, disable or hide UI.
|
||||
@@ -29,13 +35,20 @@ export const verifyServiceWorkerExistence = (callback = () => {}) => {
|
||||
navigator.serviceWorker.controller
|
||||
) {
|
||||
// New service worker available, will activate on next page load
|
||||
console.log(
|
||||
'New service worker available, will activate on next page load'
|
||||
);
|
||||
// eslint-disable-next-line no-console
|
||||
console.log('New service worker available');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Trigger asset prefetch during idle time
|
||||
if ('requestIdleCallback' in window) {
|
||||
window.requestIdleCallback(() => prefetchAssets(), { timeout: 5000 });
|
||||
} else {
|
||||
// Fallback: prefetch after 3 seconds
|
||||
setTimeout(prefetchAssets, 3000);
|
||||
}
|
||||
|
||||
callback(registration);
|
||||
})
|
||||
.catch(registrationError => {
|
||||
|
||||
Reference in New Issue
Block a user