From a8ca82ca4d2a01baf16a2a0c79ce2da4a665c91b Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 23 Mar 2021 15:18:07 +0100 Subject: [PATCH] support running the service worker during local development --- index.html | 4 ++-- scripts/build.mjs | 15 ++++++++++++--- src/platform/web/service-worker.template.js | 6 +++--- 3 files changed, 17 insertions(+), 8 deletions(-) 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`;