SW bits that make it good enough for now

This commit is contained in:
Jake Archibald
2020-09-25 09:38:19 +01:00
parent e11b4cf22c
commit 4d8efcea66
4 changed files with 15 additions and 18 deletions

View File

@@ -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");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at

View File

@@ -59,7 +59,7 @@ export default function serviceWorkerPlugin({
const version = versionHash.digest('hex');
swChunk.code =
`const ASSETS = ${JSON.stringify(urls)};\n` +
`const ASSETS = ${JSON.stringify(urls, null, ' ')};\n` +
`const VERSION = ${JSON.stringify(version)};\n` +
swChunk.code;
},

View File

@@ -10,8 +10,6 @@ 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';
@@ -21,11 +19,11 @@ self.addEventListener('install', (event) => {
event.waitUntil(
(async function () {
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 (await get('user-interacted')) {
promises.push(cacheAdditionalProcessors(versionedCache, BUILD_ASSETS));
promises.push(cacheAdditionalProcessors(versionedCache, ASSETS));
}
await Promise.all(promises);
@@ -67,12 +65,9 @@ self.addEventListener('fetch', (event) => {
// 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')
) {
if (url.pathname.startsWith('/c/demo-')) {
cacheOrNetworkAndCache(event, dynamicCache);
cleanupCache(event, dynamicCache, BUILD_ASSETS);
cleanupCache(event, dynamicCache, ASSETS);
return;
}
@@ -82,7 +77,7 @@ self.addEventListener('fetch', (event) => {
self.addEventListener('message', (event) => {
switch (event.data) {
case 'cache-all':
event.waitUntil(cacheAdditionalProcessors(versionedCache, BUILD_ASSETS));
event.waitUntil(cacheAdditionalProcessors(versionedCache, ASSETS));
break;
case 'skip-waiting':
self.skipWaiting();

View File

@@ -48,7 +48,6 @@ export function serveShareTarget(event: FetchEvent): void {
const dataPromise = event.request.formData();
// 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.waitUntil(
@@ -93,17 +92,18 @@ function getAssetsWithPrefix(assets: string[], prefixes: string[]) {
}
export async function cacheBasics(cacheName: string, buildAssets: string[]) {
const toCache = ['/', '/assets/favicon.ico'];
const toCache = ['/'];
const prefixesToCache = [
// TODO: this is likely incomplete
// Main app JS & CSS:
'main-app.',
'c/initial-app-',
// Service worker handler:
'offliner.',
'c/sw-bridge-',
// Little icons for the demo images on the homescreen:
'icon-demo-',
'c/icon-demo-',
// Site logo:
'logo.',
'c/logo-',
];
const prefixMatches = getAssetsWithPrefix(buildAssets, prefixesToCache);
@@ -121,6 +121,7 @@ export async function cacheAdditionalProcessors(
let toCache = [];
const prefixesToCache = [
// TODO: these will need to change
// Worker which handles image processing:
'processor-worker.',
// processor-worker imports:
@@ -144,6 +145,7 @@ export async function cacheAdditionalProcessors(
}),
);
// TODO: this is likely wrong
// No point caching decoders the browser already supports:
toCache = toCache.filter(
(asset) =>