mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-15 01:59:57 +00:00
# Conflicts: # codecs/cpp.Dockerfile # codecs/imagequant/example.html # codecs/webp/dec/webp_dec.d.ts # codecs/webp/dec/webp_dec.js # codecs/webp/dec/webp_dec.wasm # codecs/webp/enc/webp_enc.d.ts # codecs/webp/enc/webp_enc.js # codecs/webp/enc/webp_enc.wasm # package-lock.json # package.json # src/codecs/tiny.webp # src_old/codecs/encoders.ts # src_old/codecs/processor-worker/tiny.avif # src_old/codecs/processor-worker/tiny.webp # src_old/codecs/tiny.webp # src_old/components/compress/index.tsx # src_old/lib/util.ts # src_old/sw/util.ts
92 lines
2.2 KiB
TypeScript
92 lines
2.2 KiB
TypeScript
import {
|
|
cacheOrNetworkAndCache,
|
|
cleanupCache,
|
|
cacheOrNetwork,
|
|
cacheBasics,
|
|
cacheAdditionalProcessors,
|
|
serveShareTarget,
|
|
} from './util';
|
|
import { get } from 'idb-keyval';
|
|
|
|
// Give TypeScript the correct global.
|
|
declare var self: ServiceWorkerGlobalScope;
|
|
// This is populated by webpack.
|
|
declare var BUILD_ASSETS: string[];
|
|
|
|
const versionedCache = 'static-' + VERSION;
|
|
const dynamicCache = 'dynamic';
|
|
const expectedCaches = [versionedCache, dynamicCache];
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
(async function () {
|
|
const promises = [];
|
|
promises.push(cacheBasics(versionedCache, BUILD_ASSETS));
|
|
|
|
// If the user has already interacted with the app, update the codecs too.
|
|
if (await get('user-interacted')) {
|
|
promises.push(cacheAdditionalProcessors(versionedCache, BUILD_ASSETS));
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
})(),
|
|
);
|
|
});
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
self.clients.claim();
|
|
|
|
event.waitUntil(
|
|
(async function () {
|
|
// Remove old caches.
|
|
const promises = (await caches.keys()).map((cacheName) => {
|
|
if (!expectedCaches.includes(cacheName))
|
|
return caches.delete(cacheName);
|
|
});
|
|
|
|
await Promise.all<any>(promises);
|
|
})(),
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
const url = new URL(event.request.url);
|
|
|
|
// Don't care about other-origin URLs
|
|
if (url.origin !== location.origin) return;
|
|
|
|
if (
|
|
url.pathname === '/' &&
|
|
url.searchParams.has('share-target') &&
|
|
event.request.method === 'POST'
|
|
) {
|
|
serveShareTarget(event);
|
|
return;
|
|
}
|
|
|
|
// We only care about GET from here on in.
|
|
if (event.request.method !== 'GET') return;
|
|
|
|
if (
|
|
url.pathname.startsWith('/demo-') ||
|
|
url.pathname.startsWith('/wc-polyfill')
|
|
) {
|
|
cacheOrNetworkAndCache(event, dynamicCache);
|
|
cleanupCache(event, dynamicCache, BUILD_ASSETS);
|
|
return;
|
|
}
|
|
|
|
cacheOrNetwork(event);
|
|
});
|
|
|
|
self.addEventListener('message', (event) => {
|
|
switch (event.data) {
|
|
case 'cache-all':
|
|
event.waitUntil(cacheAdditionalProcessors(versionedCache, BUILD_ASSETS));
|
|
break;
|
|
case 'skip-waiting':
|
|
self.skipWaiting();
|
|
break;
|
|
}
|
|
});
|