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