fix: guard service worker registration
This commit is contained in:
+71
-12
@@ -13,16 +13,75 @@ declare global {
|
||||
|
||||
window.__OPENCHAMBER_RUNTIME_APIS__ = createWebAPIs();
|
||||
|
||||
if (import.meta.env.PROD) {
|
||||
registerSW({
|
||||
onRegisterError(error: unknown) {
|
||||
console.warn('[PWA] service worker registration failed:', error);
|
||||
},
|
||||
});
|
||||
} else if ('serviceWorker' in navigator) {
|
||||
void navigator.serviceWorker.getRegistrations()
|
||||
.then((registrations) => Promise.all(registrations.map((registration) => registration.unregister())))
|
||||
.catch(() => {});
|
||||
}
|
||||
type PrerenderingDocument = Document & {
|
||||
prerendering?: boolean;
|
||||
};
|
||||
|
||||
import('@openchamber/ui/main');
|
||||
const canUseServiceWorker = (): boolean => {
|
||||
if (!('serviceWorker' in navigator)) return false;
|
||||
if (!window.isSecureContext) return false;
|
||||
if (window.location.protocol !== 'http:' && window.location.protocol !== 'https:') return false;
|
||||
|
||||
const documentState = document as PrerenderingDocument;
|
||||
if (documentState.prerendering || String(document.visibilityState) === 'prerender') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const runWhenDocumentCanRegisterServiceWorker = (task: () => void): void => {
|
||||
let completed = false;
|
||||
const run = () => {
|
||||
if (completed) return;
|
||||
if (canUseServiceWorker()) {
|
||||
completed = true;
|
||||
task();
|
||||
}
|
||||
};
|
||||
|
||||
const afterLoad = () => {
|
||||
setTimeout(run, 0);
|
||||
};
|
||||
|
||||
if (document.readyState === 'complete') {
|
||||
afterLoad();
|
||||
} else {
|
||||
window.addEventListener('load', afterLoad, { once: true });
|
||||
}
|
||||
|
||||
const documentState = document as PrerenderingDocument;
|
||||
if (documentState.prerendering || String(document.visibilityState) === 'prerender') {
|
||||
document.addEventListener('visibilitychange', run, { once: true });
|
||||
}
|
||||
};
|
||||
|
||||
const registerPwaServiceWorker = (): void => {
|
||||
runWhenDocumentCanRegisterServiceWorker(() => {
|
||||
try {
|
||||
registerSW({
|
||||
onRegisterError(error: unknown) {
|
||||
console.warn('[PWA] service worker registration skipped:', error);
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn('[PWA] service worker registration skipped:', error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const unregisterDevelopmentServiceWorkers = (): void => {
|
||||
runWhenDocumentCanRegisterServiceWorker(() => {
|
||||
void navigator.serviceWorker.getRegistrations()
|
||||
.then((registrations) => Promise.all(registrations.map((registration) => registration.unregister())))
|
||||
.catch(() => {});
|
||||
});
|
||||
};
|
||||
|
||||
void import('@openchamber/ui/main');
|
||||
|
||||
if (import.meta.env.PROD) {
|
||||
registerPwaServiceWorker();
|
||||
} else {
|
||||
unregisterDevelopmentServiceWorkers();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user