Browse Source

Try to fix loading new version in Safari

master
morethanwords 3 years ago
parent
commit
e675ee1070
  1. 14
      src/lib/serviceWorker/cache.ts

14
src/lib/serviceWorker/cache.ts

@ -4,6 +4,8 @@ @@ -4,6 +4,8 @@
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import { pause } from "../../helpers/schedulers/pause";
const ctx = self as any as ServiceWorkerGlobalScope;
export const CACHE_ASSETS_NAME = 'cachedAssets';
@ -11,10 +13,18 @@ function isCorrectResponse(response: Response) { @@ -11,10 +13,18 @@ function isCorrectResponse(response: Response) {
return response.ok && response.status === 200;
}
function timeoutRace<T extends Promise<any>>(promise: T) {
return Promise.race([
promise,
pause(10000).then(() => Promise.reject())
]);
}
export async function requestCache(event: FetchEvent) {
try {
const cache = await ctx.caches.open(CACHE_ASSETS_NAME);
const file = await cache.match(event.request, {ignoreVary: true});
// const cache = await ctx.caches.open(CACHE_ASSETS_NAME);
const cache = await timeoutRace(ctx.caches.open(CACHE_ASSETS_NAME));
const file = await timeoutRace(cache.match(event.request, {ignoreVary: true}));
if(file && isCorrectResponse(file)) {
return file;

Loading…
Cancel
Save