diff --git a/lib/omt.ejs b/lib/omt.ejs index 77639fc4..174f906f 100644 --- a/lib/omt.ejs +++ b/lib/omt.ejs @@ -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 diff --git a/lib/sw-plugin.js b/lib/sw-plugin.js index ff473c2d..75809a4e 100644 --- a/lib/sw-plugin.js +++ b/lib/sw-plugin.js @@ -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; }, diff --git a/src/sw/index.ts b/src/sw/index.ts index 697a7a21..1e611971 100644 --- a/src/sw/index.ts +++ b/src/sw/index.ts @@ -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(); diff --git a/src/sw/util.ts b/src/sw/util.ts index d14492cb..20a901e5 100644 --- a/src/sw/util.ts +++ b/src/sw/util.ts @@ -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) =>