diff --git a/index.html b/index.html index e5746572..fb6934bb 100644 --- a/index.html +++ b/index.html @@ -23,8 +23,8 @@ import {Platform} from "./src/platform/web/Platform.js"; main(new Platform(document.body, { worker: "src/worker.js", - downloadSandbox: "assets/download-sandbox.html", - // ln -s src/platform/web/service-worker.template.js sw.js + downloadSandbox: "assets/download-sandbox.html", + // NOTE: uncomment this if you want the service worker for local development // serviceWorker: "sw.js", // push: { // appId: "io.element.hydrogen.web", diff --git a/scripts/build.mjs b/scripts/build.mjs index 6622f092..29a4bb54 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -270,12 +270,21 @@ async function buildServiceWorker(swSource, version, globalHash, assets) { hashedCachedOnRequestAssets.push(resolved); } } + + const replaceArrayInSource = (name, value) => { + const newSource = swSource.replace(`${name} = []`, `${name} = ${JSON.stringify(value)}`); + if (newSource === swSource) { + throw new Error(`${name} was not found in the service worker source`); + } + return newSource; + }; + // write service worker swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`); swSource = swSource.replace(`"%%GLOBAL_HASH%%"`, `"${globalHash}"`); - swSource = swSource.replace(`"%%UNHASHED_PRECACHED_ASSETS%%"`, JSON.stringify(unhashedPreCachedAssets)); - swSource = swSource.replace(`"%%HASHED_PRECACHED_ASSETS%%"`, JSON.stringify(hashedPreCachedAssets)); - swSource = swSource.replace(`"%%HASHED_CACHED_ON_REQUEST_ASSETS%%"`, JSON.stringify(hashedCachedOnRequestAssets)); + swSource = replaceArrayInSource("UNHASHED_PRECACHED_ASSETS", unhashedPreCachedAssets); + swSource = replaceArrayInSource("HASHED_PRECACHED_ASSETS", hashedPreCachedAssets); + swSource = replaceArrayInSource("HASHED_CACHED_ON_REQUEST_ASSETS", hashedCachedOnRequestAssets); // service worker should not have a hashed name as it is polled by the browser for updates await assets.writeUnhashed("sw.js", swSource); } diff --git a/src/platform/web/service-worker.template.js b/src/platform/web/service-worker.template.js index 0e28ac7a..7d545138 100644 --- a/src/platform/web/service-worker.template.js +++ b/src/platform/web/service-worker.template.js @@ -17,9 +17,9 @@ limitations under the License. const VERSION = "%%VERSION%%"; const GLOBAL_HASH = "%%GLOBAL_HASH%%"; -const UNHASHED_PRECACHED_ASSETS = "%%UNHASHED_PRECACHED_ASSETS%%"; -const HASHED_PRECACHED_ASSETS = "%%HASHED_PRECACHED_ASSETS%%"; -const HASHED_CACHED_ON_REQUEST_ASSETS = "%%HASHED_CACHED_ON_REQUEST_ASSETS%%"; +const UNHASHED_PRECACHED_ASSETS = []; +const HASHED_PRECACHED_ASSETS = []; +const HASHED_CACHED_ON_REQUEST_ASSETS = []; const unhashedCacheName = `hydrogen-assets-${GLOBAL_HASH}`; const hashedCacheName = `hydrogen-assets`; const mediaThumbnailCacheName = `hydrogen-media-thumbnails-v2`;