From d2a7996ea1fde56b2998bee5cd688bedb4ede05e Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 14 Jun 2023 13:25:48 +0200 Subject: [PATCH] Service Worker: Fix "disabled" error (#3289) --- src/serviceWorker.ts | 7 +++---- src/util/setupServiceWorker.ts | 22 ++++++++-------------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/serviceWorker.ts b/src/serviceWorker.ts index cc0c2f413..136ae3d1a 100644 --- a/src/serviceWorker.ts +++ b/src/serviceWorker.ts @@ -1,3 +1,4 @@ +import { DEBUG } from './config'; import { respondForProgressive } from './serviceWorker/progressive'; import { respondForDownload } from './serviceWorker/download'; import { respondWithCache, clearAssetCache, respondWithCacheNetworkFirst } from './serviceWorker/assetCache'; @@ -16,10 +17,8 @@ const RE_NETWORK_FIRST_ASSETS = /\.(wasm|html)$/; const RE_CACHE_FIRST_ASSETS = /[\da-f]{20}.*\.(js|css|woff2?|svg|png|jpg|jpeg|tgs|json|wasm)$/; const ACTIVATE_TIMEOUT = 3000; -const TEMP_DEBUG = true; - self.addEventListener('install', (e) => { - if (TEMP_DEBUG) { + if (DEBUG) { // eslint-disable-next-line no-console console.log('ServiceWorker installed'); } @@ -29,7 +28,7 @@ self.addEventListener('install', (e) => { }); self.addEventListener('activate', (e) => { - if (TEMP_DEBUG) { + if (DEBUG) { // eslint-disable-next-line no-console console.log('ServiceWorker activated'); } diff --git a/src/util/setupServiceWorker.ts b/src/util/setupServiceWorker.ts index 038c9fb6f..c4de0b1e4 100644 --- a/src/util/setupServiceWorker.ts +++ b/src/util/setupServiceWorker.ts @@ -1,4 +1,4 @@ -import { DEBUG_MORE, IS_TEST } from '../config'; +import { DEBUG, DEBUG_MORE, IS_TEST } from '../config'; import { getActions } from '../global'; import { formatShareText } from './deeplink'; import { IS_ANDROID, IS_IOS, IS_SERVICE_WORKER_SUPPORTED } from './windowEnvironment'; @@ -12,8 +12,6 @@ type WorkerAction = { const IGNORE_WORKER_PATH = '/k/'; -const TEMP_DEBUG = true; - function handleWorkerMessage(e: MessageEvent) { const action: WorkerAction = e.data; if (DEBUG_MORE) { @@ -54,7 +52,7 @@ if (IS_SERVICE_WORKER_SUPPORTED) { const registrations = await navigator.serviceWorker.getRegistrations(); const ourRegistrations = registrations.filter((r) => !r.scope.includes(IGNORE_WORKER_PATH)); if (ourRegistrations.length) { - if (TEMP_DEBUG) { + if (DEBUG) { // eslint-disable-next-line no-console console.log('[SW] Hard reload detected, re-enabling Service Worker'); } @@ -64,30 +62,26 @@ if (IS_SERVICE_WORKER_SUPPORTED) { await navigator.serviceWorker.register(new URL('../serviceWorker.ts', import.meta.url)); - if (TEMP_DEBUG) { + if (DEBUG) { // eslint-disable-next-line no-console console.log('[SW] ServiceWorker registered'); } await navigator.serviceWorker.ready; - // eslint-disable-next-line no-console - console.log('Service Worker', navigator.serviceWorker?.controller?.scriptURL); + // Wait for registration to be available + await navigator.serviceWorker.getRegistration(); if (navigator.serviceWorker.controller) { - if (TEMP_DEBUG) { + if (DEBUG) { // eslint-disable-next-line no-console console.log('[SW] ServiceWorker ready'); } subscribeToWorker(); } else { - if (TEMP_DEBUG) { + if (DEBUG) { // eslint-disable-next-line no-console console.error('[SW] ServiceWorker not available'); - // eslint-disable-next-line no-console - console.warn('Assigned registration', await navigator.serviceWorker.getRegistration()); - // eslint-disable-next-line no-console - console.warn('Ready promise', navigator.serviceWorker?.ready); } if (!IS_IOS && !IS_ANDROID && !IS_TEST) { @@ -95,7 +89,7 @@ if (IS_SERVICE_WORKER_SUPPORTED) { } } } catch (err) { - if (TEMP_DEBUG) { + if (DEBUG) { // eslint-disable-next-line no-console console.error('[SW] ServiceWorker registration failed: ', err); }