Service worker: cache static
This commit is contained in:
parent
72eb78a543
commit
feb9d9b1ae
@ -7,6 +7,7 @@
|
|||||||
/// #if MTPROTO_SW
|
/// #if MTPROTO_SW
|
||||||
import './mtproto.worker';
|
import './mtproto.worker';
|
||||||
/// #endif
|
/// #endif
|
||||||
|
//import CacheStorageController from '../cacheStorage';
|
||||||
import { isSafari } from '../../helpers/userAgent';
|
import { isSafari } from '../../helpers/userAgent';
|
||||||
import { logger, LogTypes } from '../logger';
|
import { logger, LogTypes } from '../logger';
|
||||||
import type { DownloadOptions } from './apiFileManager';
|
import type { DownloadOptions } from './apiFileManager';
|
||||||
@ -20,6 +21,17 @@ const ctx = self as any as ServiceWorkerGlobalScope;
|
|||||||
|
|
||||||
const deferredPromises: {[taskId: number]: CancellablePromise<any>} = {};
|
const deferredPromises: {[taskId: number]: CancellablePromise<any>} = {};
|
||||||
|
|
||||||
|
export interface ServiceWorkerTask extends WorkerTaskTemplate {
|
||||||
|
type: 'requestFilePart',
|
||||||
|
payload: [number, InputFileLocation, number, number]
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface ServiceWorkerTaskResponse extends WorkerTaskTemplate {
|
||||||
|
type: 'requestFilePart',
|
||||||
|
payload?: UploadFile.uploadFile,
|
||||||
|
originalPayload?: ServiceWorkerTask['payload']
|
||||||
|
};
|
||||||
|
|
||||||
/// #if !MTPROTO_SW
|
/// #if !MTPROTO_SW
|
||||||
ctx.addEventListener('message', (e) => {
|
ctx.addEventListener('message', (e) => {
|
||||||
const task = e.data as ServiceWorkerTaskResponse;
|
const task = e.data as ServiceWorkerTaskResponse;
|
||||||
@ -35,20 +47,32 @@ ctx.addEventListener('message', (e) => {
|
|||||||
});
|
});
|
||||||
/// #endif
|
/// #endif
|
||||||
|
|
||||||
|
//const cacheStorage = new CacheStorageController('cachedAssets');
|
||||||
let taskId = 0;
|
let taskId = 0;
|
||||||
|
|
||||||
export interface ServiceWorkerTask extends WorkerTaskTemplate {
|
async function requestCache(event: FetchEvent) {
|
||||||
type: 'requestFilePart',
|
try {
|
||||||
payload: [number, InputFileLocation, number, number]
|
const cache = await ctx.caches.open('cachedAssets');
|
||||||
};
|
const file = await cache.match(event.request);
|
||||||
|
|
||||||
export interface ServiceWorkerTaskResponse extends WorkerTaskTemplate {
|
if(file) {
|
||||||
type: 'requestFilePart',
|
return file;
|
||||||
payload?: UploadFile.uploadFile,
|
}
|
||||||
originalPayload?: ServiceWorkerTask['payload']
|
|
||||||
};
|
const response = await fetch(event.request);
|
||||||
|
cache.put(event.request, response.clone());
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch(err) {
|
||||||
|
return fetch(event.request);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const onFetch = (event: FetchEvent): void => {
|
const onFetch = (event: FetchEvent): void => {
|
||||||
|
if(event.request.url.indexOf(location.origin + '/') === 0 && event.request.url.match(/\.(js|css|jpe?g|json|wasm|png|mp3|svg|tgs|ico|woff2?)$/)) {
|
||||||
|
return event.respondWith(requestCache(event));
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [, url, scope, params] = /http[:s]+\/\/.*?(\/(.*?)(?:$|\/(.*)$))/.exec(event.request.url) || [];
|
const [, url, scope, params] = /http[:s]+\/\/.*?(\/(.*?)(?:$|\/(.*)$))/.exec(event.request.url) || [];
|
||||||
|
|
||||||
@ -160,6 +184,7 @@ ctx.addEventListener('activate', (event) => {
|
|||||||
/* if (!ctx.cache) initCache();
|
/* if (!ctx.cache) initCache();
|
||||||
if (!ctx.network) initNetwork(); */
|
if (!ctx.network) initNetwork(); */
|
||||||
|
|
||||||
|
event.waitUntil(ctx.caches.delete('cachedAssets'));
|
||||||
event.waitUntil(ctx.clients.claim());
|
event.waitUntil(ctx.clients.claim());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user