Skip to content

Commit

Permalink
Link old service worker to new name (#21581)
Browse files Browse the repository at this point in the history
  • Loading branch information
steverep authored Aug 5, 2024
1 parent 62d8434 commit dfbf4ab
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build-scripts/gulp/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
12 changes: 9 additions & 3 deletions build-scripts/gulp/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
})
)
);
7 changes: 0 additions & 7 deletions src/util/register-service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit dfbf4ab

Please sign in to comment.