From 3aa1992a3f4eee58dbcf063c5a9ff4832e3b3263 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 15 Oct 2020 19:12:16 +0200 Subject: [PATCH] WIP of messaging to sw --- index.html | 29 +++++++++++++++++++++++++---- scripts/build.mjs | 5 +++-- src/main.js | 2 +- src/service-worker.template.js | 13 +++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 8d97a805..513a7b03 100644 --- a/index.html +++ b/index.html @@ -29,14 +29,32 @@ } }); - diff --git a/scripts/build.mjs b/scripts/build.mjs index f7bb7857..81399039 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -87,7 +87,7 @@ async function build({modernOnly}) { const globalHash = assets.hashForAll(); - await buildServiceWorker(swSource, globalHash, assets); + await buildServiceWorker(swSource, version, globalHash, assets); await buildHtml(doc, version, globalHash, modernOnly, assets); console.log(`built hydrogen ${version} (${globalHash}) successfully with ${assets.size} files`); } @@ -246,7 +246,7 @@ async function buildManifest(assets) { await assets.write("manifest.json", JSON.stringify(webManifest)); } -async function buildServiceWorker(swSource, globalHash, assets) { +async function buildServiceWorker(swSource, version, globalHash, assets) { const unhashedPreCachedAssets = ["index.html"]; const hashedPreCachedAssets = []; const hashedCachedOnRequestAssets = []; @@ -261,6 +261,7 @@ async function buildServiceWorker(swSource, globalHash, assets) { } } // 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)); diff --git a/src/main.js b/src/main.js index d482c078..91c31321 100644 --- a/src/main.js +++ b/src/main.js @@ -148,6 +148,6 @@ export async function main(container, paths, legacyExtras) { const view = new RootView(vm); container.appendChild(view.mount()); } catch(err) { - console.error(`${err.message}:\n${err.stack}`); + console.error(`${err.message}:\n${err.stack}sdfdf`); } } diff --git a/src/service-worker.template.js b/src/service-worker.template.js index 6ae3988f..af8eae88 100644 --- a/src/service-worker.template.js +++ b/src/service-worker.template.js @@ -15,6 +15,7 @@ See the License for the specific language governing permissions and 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%%"; @@ -67,6 +68,18 @@ self.addEventListener('fetch', (event) => { event.respondWith(handleRequest(event.request)); }); +self.addEventListener('message', (event) => { + const reply = content => event.source.postMessage({replyTo: event.data?.id, content}); + switch (event.data?.type) { + case "version": + reply({version: VERSION, buildHash: GLOBAL_HASH}); + break; + case "skipWaiting": + self.skipWaiting(); + break; + } +}); + async function handleRequest(request) { const baseURL = self.registration.scope; if (request.url === baseURL) {