Try to fix loading new version in Safari

This commit is contained in:
morethanwords 2021-10-06 23:54:14 +04:00
parent 5337730397
commit bc3a4cce10

View File

@ -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) {
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;