diff --git a/build-scripts/gulp/compress.js b/build-scripts/gulp/compress.js index ce68c1a1087b..1a6032c35c55 100644 --- a/build-scripts/gulp/compress.js +++ b/build-scripts/gulp/compress.js @@ -29,7 +29,7 @@ const compressDistZopfli = (rootDir, modernDir) => [ `${rootDir}/**/${filesGlob}`, `!${modernDir}/**/${filesGlob}`, - `!${rootDir}/sw-modern.js`, + `!${rootDir}/{sw-modern,service_worker}.js`, `${rootDir}/{authorize,onboarding}.html`, ], { base: rootDir } diff --git a/build-scripts/gulp/service-worker.js b/build-scripts/gulp/service-worker.js index ff4d5c2b7e03..e9aa78e3f266 100644 --- a/build-scripts/gulp/service-worker.js +++ b/build-scripts/gulp/service-worker.js @@ -2,8 +2,8 @@ import { deleteAsync } from "del"; import gulp from "gulp"; -import { mkdir, readFile, writeFile } from "node:fs/promises"; -import { join, relative } from "node:path"; +import { mkdir, readFile, symlink, writeFile } from "node:fs/promises"; +import { basename, join, relative } from "node:path"; import { injectManifest } from "workbox-build"; import paths from "../paths.cjs"; @@ -41,10 +41,11 @@ gulp.task("gen-service-worker-app-prod", () => await readFile(join(outPath, "manifest.json"), "utf-8") ); const swSrc = join(paths.app_output_root, manifest["service-worker.js"]); + const swDest = join(paths.app_output_root, `sw-${build}.js`); const buildDir = relative(paths.app_output_root, outPath); const { warnings } = await injectManifest({ swSrc, - swDest: join(paths.app_output_root, `sw-${build}.js`), + swDest, injectionPoint: "__WB_MANIFEST__", // Files that mach this pattern will be considered unique and skip revision check // ignore JS files + translation files @@ -76,6 +77,11 @@ gulp.task("gen-service-worker-app-prod", () => ); } await deleteAsync(`${swSrc}?(.map)`); + // Needed to install new SW from a cached HTML + if (build === "modern") { + const swOld = join(paths.app_output_root, "service_worker.js"); + await symlink(basename(swDest), swOld); + } }) ) ); diff --git a/src/util/register-service-worker.ts b/src/util/register-service-worker.ts index a582cc8aaca4..87d065420d7e 100644 --- a/src/util/register-service-worker.ts +++ b/src/util/register-service-worker.ts @@ -23,13 +23,6 @@ export const registerServiceWorker = async ( return; } - if (reg?.active?.scriptURL.includes("service_worker.js")) { - // We are running an old version of the service worker. Force reload. - await reg.unregister(); - // @ts-ignore Firefox supports force reload - location.reload(true); - } - reg.addEventListener("updatefound", () => { const installingWorker = reg.installing;