mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
SW bits that make it good enough for now
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Copyright 2018 Google Inc. All Rights Reserved.
|
* Copyright 2020 Google Inc. All Rights Reserved.
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ export default function serviceWorkerPlugin({
|
|||||||
const version = versionHash.digest('hex');
|
const version = versionHash.digest('hex');
|
||||||
|
|
||||||
swChunk.code =
|
swChunk.code =
|
||||||
`const ASSETS = ${JSON.stringify(urls)};\n` +
|
`const ASSETS = ${JSON.stringify(urls, null, ' ')};\n` +
|
||||||
`const VERSION = ${JSON.stringify(version)};\n` +
|
`const VERSION = ${JSON.stringify(version)};\n` +
|
||||||
swChunk.code;
|
swChunk.code;
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ import { get } from 'idb-keyval';
|
|||||||
|
|
||||||
// Give TypeScript the correct global.
|
// Give TypeScript the correct global.
|
||||||
declare var self: ServiceWorkerGlobalScope;
|
declare var self: ServiceWorkerGlobalScope;
|
||||||
// This is populated by webpack.
|
|
||||||
declare var BUILD_ASSETS: string[];
|
|
||||||
|
|
||||||
const versionedCache = 'static-' + VERSION;
|
const versionedCache = 'static-' + VERSION;
|
||||||
const dynamicCache = 'dynamic';
|
const dynamicCache = 'dynamic';
|
||||||
@@ -21,11 +19,11 @@ self.addEventListener('install', (event) => {
|
|||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
(async function () {
|
(async function () {
|
||||||
const promises = [];
|
const promises = [];
|
||||||
promises.push(cacheBasics(versionedCache, BUILD_ASSETS));
|
promises.push(cacheBasics(versionedCache, ASSETS));
|
||||||
|
|
||||||
// If the user has already interacted with the app, update the codecs too.
|
// If the user has already interacted with the app, update the codecs too.
|
||||||
if (await get('user-interacted')) {
|
if (await get('user-interacted')) {
|
||||||
promises.push(cacheAdditionalProcessors(versionedCache, BUILD_ASSETS));
|
promises.push(cacheAdditionalProcessors(versionedCache, ASSETS));
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
@@ -67,12 +65,9 @@ self.addEventListener('fetch', (event) => {
|
|||||||
// We only care about GET from here on in.
|
// We only care about GET from here on in.
|
||||||
if (event.request.method !== 'GET') return;
|
if (event.request.method !== 'GET') return;
|
||||||
|
|
||||||
if (
|
if (url.pathname.startsWith('/c/demo-')) {
|
||||||
url.pathname.startsWith('/demo-') ||
|
|
||||||
url.pathname.startsWith('/wc-polyfill')
|
|
||||||
) {
|
|
||||||
cacheOrNetworkAndCache(event, dynamicCache);
|
cacheOrNetworkAndCache(event, dynamicCache);
|
||||||
cleanupCache(event, dynamicCache, BUILD_ASSETS);
|
cleanupCache(event, dynamicCache, ASSETS);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +77,7 @@ self.addEventListener('fetch', (event) => {
|
|||||||
self.addEventListener('message', (event) => {
|
self.addEventListener('message', (event) => {
|
||||||
switch (event.data) {
|
switch (event.data) {
|
||||||
case 'cache-all':
|
case 'cache-all':
|
||||||
event.waitUntil(cacheAdditionalProcessors(versionedCache, BUILD_ASSETS));
|
event.waitUntil(cacheAdditionalProcessors(versionedCache, ASSETS));
|
||||||
break;
|
break;
|
||||||
case 'skip-waiting':
|
case 'skip-waiting':
|
||||||
self.skipWaiting();
|
self.skipWaiting();
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ export function serveShareTarget(event: FetchEvent): void {
|
|||||||
const dataPromise = event.request.formData();
|
const dataPromise = event.request.formData();
|
||||||
|
|
||||||
// Redirect so the user can refresh the page without resending data.
|
// Redirect so the user can refresh the page without resending data.
|
||||||
// @ts-ignore It doesn't like me giving a response to respondWith, although it's allowed.
|
|
||||||
event.respondWith(Response.redirect('/?share-target'));
|
event.respondWith(Response.redirect('/?share-target'));
|
||||||
|
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
@@ -93,17 +92,18 @@ function getAssetsWithPrefix(assets: string[], prefixes: string[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function cacheBasics(cacheName: string, buildAssets: string[]) {
|
export async function cacheBasics(cacheName: string, buildAssets: string[]) {
|
||||||
const toCache = ['/', '/assets/favicon.ico'];
|
const toCache = ['/'];
|
||||||
|
|
||||||
const prefixesToCache = [
|
const prefixesToCache = [
|
||||||
|
// TODO: this is likely incomplete
|
||||||
// Main app JS & CSS:
|
// Main app JS & CSS:
|
||||||
'main-app.',
|
'c/initial-app-',
|
||||||
// Service worker handler:
|
// Service worker handler:
|
||||||
'offliner.',
|
'c/sw-bridge-',
|
||||||
// Little icons for the demo images on the homescreen:
|
// Little icons for the demo images on the homescreen:
|
||||||
'icon-demo-',
|
'c/icon-demo-',
|
||||||
// Site logo:
|
// Site logo:
|
||||||
'logo.',
|
'c/logo-',
|
||||||
];
|
];
|
||||||
|
|
||||||
const prefixMatches = getAssetsWithPrefix(buildAssets, prefixesToCache);
|
const prefixMatches = getAssetsWithPrefix(buildAssets, prefixesToCache);
|
||||||
@@ -121,6 +121,7 @@ export async function cacheAdditionalProcessors(
|
|||||||
let toCache = [];
|
let toCache = [];
|
||||||
|
|
||||||
const prefixesToCache = [
|
const prefixesToCache = [
|
||||||
|
// TODO: these will need to change
|
||||||
// Worker which handles image processing:
|
// Worker which handles image processing:
|
||||||
'processor-worker.',
|
'processor-worker.',
|
||||||
// processor-worker imports:
|
// processor-worker imports:
|
||||||
@@ -144,6 +145,7 @@ export async function cacheAdditionalProcessors(
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// TODO: this is likely wrong
|
||||||
// No point caching decoders the browser already supports:
|
// No point caching decoders the browser already supports:
|
||||||
toCache = toCache.filter(
|
toCache = toCache.filter(
|
||||||
(asset) =>
|
(asset) =>
|
||||||
|
|||||||
Reference in New Issue
Block a user