Service Worker: Another attempt to fix freezing UI on iOS

This commit is contained in:
Alexander Zinchuk 2021-10-08 16:17:34 +03:00
parent 1abb31e3ee
commit 0e4c3cb123

View File

@ -1,12 +1,22 @@
import { ASSET_CACHE_NAME } from '../config';
import { pause } from '../util/schedulers';
declare const self: ServiceWorkerGlobalScope;
export async function respondWithCache(e: FetchEvent) {
const cache = await self.caches.open(ASSET_CACHE_NAME);
const cached = await cache.match(e.request);
// An attempt to fix freezing UI on iOS
const CACHE_TIMEOUT = 3000;
if (cached) {
export async function respondWithCache(e: FetchEvent) {
const cacheResult = await withTimeout(async () => {
const cache = await self.caches.open(ASSET_CACHE_NAME);
const cached = await cache.match(e.request);
return { cache, cached };
}, CACHE_TIMEOUT);
const { cache, cached } = cacheResult || {};
if (cache && cached) {
if (cached.ok) {
return cached;
} else {
@ -16,13 +26,26 @@ export async function respondWithCache(e: FetchEvent) {
const remote = await fetch(e.request);
if (remote.ok) {
if (remote.ok && cache) {
cache.put(e.request, remote.clone());
}
return remote;
}
async function withTimeout<T>(cb: () => Promise<T>, timeout: number) {
try {
return await Promise.race([
pause(timeout).then(() => Promise.reject(new Error('TIMEOUT'))),
cb(),
]);
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
return undefined;
}
}
export function clearAssetCache() {
return self.caches.delete(ASSET_CACHE_NAME);
}