support running the service worker during local development

This commit is contained in:
Bruno Windels 2021-03-23 15:18:07 +01:00
parent 76fdbbb2fe
commit a8ca82ca4d
3 changed files with 17 additions and 8 deletions

View File

@ -23,8 +23,8 @@
import {Platform} from "./src/platform/web/Platform.js"; import {Platform} from "./src/platform/web/Platform.js";
main(new Platform(document.body, { main(new Platform(document.body, {
worker: "src/worker.js", worker: "src/worker.js",
downloadSandbox: "assets/download-sandbox.html", downloadSandbox: "assets/download-sandbox.html",
// ln -s src/platform/web/service-worker.template.js sw.js // NOTE: uncomment this if you want the service worker for local development
// serviceWorker: "sw.js", // serviceWorker: "sw.js",
// push: { // push: {
// appId: "io.element.hydrogen.web", // appId: "io.element.hydrogen.web",

View File

@ -270,12 +270,21 @@ async function buildServiceWorker(swSource, version, globalHash, assets) {
hashedCachedOnRequestAssets.push(resolved); 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 // write service worker
swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`); swSource = swSource.replace(`"%%VERSION%%"`, `"${version}"`);
swSource = swSource.replace(`"%%GLOBAL_HASH%%"`, `"${globalHash}"`); swSource = swSource.replace(`"%%GLOBAL_HASH%%"`, `"${globalHash}"`);
swSource = swSource.replace(`"%%UNHASHED_PRECACHED_ASSETS%%"`, JSON.stringify(unhashedPreCachedAssets)); swSource = replaceArrayInSource("UNHASHED_PRECACHED_ASSETS", unhashedPreCachedAssets);
swSource = swSource.replace(`"%%HASHED_PRECACHED_ASSETS%%"`, JSON.stringify(hashedPreCachedAssets)); swSource = replaceArrayInSource("HASHED_PRECACHED_ASSETS", hashedPreCachedAssets);
swSource = swSource.replace(`"%%HASHED_CACHED_ON_REQUEST_ASSETS%%"`, JSON.stringify(hashedCachedOnRequestAssets)); 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 // service worker should not have a hashed name as it is polled by the browser for updates
await assets.writeUnhashed("sw.js", swSource); await assets.writeUnhashed("sw.js", swSource);
} }

View File

@ -17,9 +17,9 @@ limitations under the License.
const VERSION = "%%VERSION%%"; const VERSION = "%%VERSION%%";
const GLOBAL_HASH = "%%GLOBAL_HASH%%"; const GLOBAL_HASH = "%%GLOBAL_HASH%%";
const UNHASHED_PRECACHED_ASSETS = "%%UNHASHED_PRECACHED_ASSETS%%"; const UNHASHED_PRECACHED_ASSETS = [];
const HASHED_PRECACHED_ASSETS = "%%HASHED_PRECACHED_ASSETS%%"; const HASHED_PRECACHED_ASSETS = [];
const HASHED_CACHED_ON_REQUEST_ASSETS = "%%HASHED_CACHED_ON_REQUEST_ASSETS%%"; const HASHED_CACHED_ON_REQUEST_ASSETS = [];
const unhashedCacheName = `hydrogen-assets-${GLOBAL_HASH}`; const unhashedCacheName = `hydrogen-assets-${GLOBAL_HASH}`;
const hashedCacheName = `hydrogen-assets`; const hashedCacheName = `hydrogen-assets`;
const mediaThumbnailCacheName = `hydrogen-media-thumbnails-v2`; const mediaThumbnailCacheName = `hydrogen-media-thumbnails-v2`;