Service Worker: An attempt to fix freezing UI on iOS

This commit is contained in:
Alexander Zinchuk 2021-08-27 21:09:00 +03:00
parent 84e1a5b1de
commit f4fad13266

View File

@ -2,10 +2,12 @@ import { DEBUG } from './config';
import { respondForProgressive } from './serviceWorker/progressive'; import { respondForProgressive } from './serviceWorker/progressive';
import { respondWithCache, clearAssetCache } from './serviceWorker/assetCache'; import { respondWithCache, clearAssetCache } from './serviceWorker/assetCache';
import { handlePush, handleNotificationClick, handleClientMessage } from './serviceWorker/pushNotification'; import { handlePush, handleNotificationClick, handleClientMessage } from './serviceWorker/pushNotification';
import { pause } from './util/schedulers';
declare const self: ServiceWorkerGlobalScope; declare const self: ServiceWorkerGlobalScope;
const ASSET_CACHE_PATTERN = /[0-9a-f]{20}.*\.(js|css|woff2?|svg|png|jpg|jpeg|json|wasm)$/; const ASSET_CACHE_PATTERN = /[0-9a-f]{20}.*\.(js|css|woff2?|svg|png|jpg|jpeg|json|wasm)$/;
const ACTIVATE_TIMEOUT = 3000;
self.addEventListener('install', (e) => { self.addEventListener('install', (e) => {
if (DEBUG) { if (DEBUG) {
@ -23,9 +25,17 @@ self.addEventListener('activate', (e) => {
console.log('ServiceWorker activated'); console.log('ServiceWorker activated');
} }
e.waitUntil(clearAssetCache()); e.waitUntil(
// Become available to all pages Promise.race([
e.waitUntil(self.clients.claim()); // An attempt to fix freezing UI on iOS
pause(ACTIVATE_TIMEOUT),
Promise.all([
clearAssetCache(),
// Become available to all pages
self.clients.claim(),
]),
]),
);
}); });
// eslint-disable-next-line no-restricted-globals // eslint-disable-next-line no-restricted-globals