Service Worker: Prevent unregistering of other workers (#3124)
This commit is contained in:
parent
56f41804c1
commit
9622408132
@ -10,6 +10,8 @@ type WorkerAction = {
|
|||||||
payload: Record<string, any>;
|
payload: Record<string, any>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const IGNORE_WORKER_PATH = '/k/';
|
||||||
|
|
||||||
function handleWorkerMessage(e: MessageEvent) {
|
function handleWorkerMessage(e: MessageEvent) {
|
||||||
const action: WorkerAction = e.data;
|
const action: WorkerAction = e.data;
|
||||||
if (DEBUG_MORE) {
|
if (DEBUG_MORE) {
|
||||||
@ -21,9 +23,7 @@ function handleWorkerMessage(e: MessageEvent) {
|
|||||||
const payload = action.payload;
|
const payload = action.payload;
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case 'focusMessage':
|
case 'focusMessage':
|
||||||
if (dispatch.focusMessage) {
|
dispatch.focusMessage?.(payload as any);
|
||||||
dispatch.focusMessage(payload as any);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 'playNotificationSound':
|
case 'playNotificationSound':
|
||||||
playNotifySoundDebounced(action.payload.id);
|
playNotifySoundDebounced(action.payload.id);
|
||||||
@ -47,14 +47,16 @@ function subscribeToWorker() {
|
|||||||
if (IS_SERVICE_WORKER_SUPPORTED) {
|
if (IS_SERVICE_WORKER_SUPPORTED) {
|
||||||
window.addEventListener('load', async () => {
|
window.addEventListener('load', async () => {
|
||||||
try {
|
try {
|
||||||
if (!navigator.serviceWorker.controller) {
|
const controller = navigator.serviceWorker.controller;
|
||||||
|
if (!controller || controller.scriptURL.includes(IGNORE_WORKER_PATH)) {
|
||||||
const registrations = await navigator.serviceWorker.getRegistrations();
|
const registrations = await navigator.serviceWorker.getRegistrations();
|
||||||
if (registrations.length) {
|
const ourRegistrations = registrations.filter((r) => !r.scope.includes(IGNORE_WORKER_PATH));
|
||||||
|
if (ourRegistrations.length) {
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log('[SW] Hard reload detected, re-enabling Service Worker');
|
console.log('[SW] Hard reload detected, re-enabling Service Worker');
|
||||||
}
|
}
|
||||||
await Promise.all(registrations.map((r) => r.unregister()));
|
await Promise.all(ourRegistrations.map((r) => r.unregister()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user