From d7d5d2c127cb5225309f6d72ec0e7cf2e46f0aa8 Mon Sep 17 00:00:00 2001 From: EduardZaydler <117674342+EduardZaydler@users.noreply.github.com> Date: Thu, 10 Oct 2024 11:48:39 +0700 Subject: [PATCH 1/3] wip --- src/serviceWorkerRegistration.ts | 3 ++- webpack.config.js | 22 ++++++++++++---------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/serviceWorkerRegistration.ts b/src/serviceWorkerRegistration.ts index 530a339f..3d9ab436 100644 --- a/src/serviceWorkerRegistration.ts +++ b/src/serviceWorkerRegistration.ts @@ -2,7 +2,8 @@ import { registerRoute } from "workbox-routing"; import { NetworkOnly } from "workbox-strategies"; registerRoute(({ url }) => { - return url.pathname.includes("/oauth"); + console.log(url.search.includes("oauth")); + return url.search.includes("oauth"); }, new NetworkOnly()); export function register(onUpdate: () => void) { diff --git a/webpack.config.js b/webpack.config.js index c1407a6a..1ec2ea93 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -21,16 +21,18 @@ module.exports = { clean: true, }, plugins: [ - //https://developer.chrome.com/docs/workbox/modules/workbox-webpack-plugin#type-GenerateSW - new GenerateSW({ - // default maximum size is 3mb while our main chunk is 4mb - // that's why size is sincreased to 6mb - maximumFileSizeToCacheInBytes: 6291456, - cleanupOutdatedCaches: true, - clientsClaim: true, - sourcemap: false, - skipWaiting: true, - }), + isDev + ? new GenerateSW({ + //https://developer.chrome.com/docs/workbox/modules/workbox-webpack-plugin#type-GenerateSW + // default maximum size is 3mb while our main chunk is 4mb + // that's why size is sincreased to 6mb + maximumFileSizeToCacheInBytes: 6291456, + cleanupOutdatedCaches: true, + clientsClaim: true, + sourcemap: false, + skipWaiting: true, + }) + : [], new ContextReplacementPlugin( /date-fns[\/\\]/, new RegExp(`[/\\\\\](${supportedLocales.join("|")})[/\\\\\]`) From 9bc090778f06e9c30d24ab8a45c6e73b2851f9e9 Mon Sep 17 00:00:00 2001 From: EduardZaydler <117674342+EduardZaydler@users.noreply.github.com> Date: Thu, 10 Oct 2024 11:56:17 +0700 Subject: [PATCH 2/3] sw dev fix wb conf !sameOrigin cache policy exclude oauth calls from sw added comment refactor refactor refactor final final event.respondwith . . . . . importing 3rdparty script inject manifest redirect handling removed conf lines . . no-cors no-cors no-cors network first for html network first for html network first for html network first for html network first for html network first for html network first for html generate sw generate sw generate sw --- src/serviceWorkerRegistration.ts | 8 ------- webpack.config.js | 38 ++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/serviceWorkerRegistration.ts b/src/serviceWorkerRegistration.ts index 3d9ab436..24b3090a 100644 --- a/src/serviceWorkerRegistration.ts +++ b/src/serviceWorkerRegistration.ts @@ -1,11 +1,3 @@ -import { registerRoute } from "workbox-routing"; -import { NetworkOnly } from "workbox-strategies"; - -registerRoute(({ url }) => { - console.log(url.search.includes("oauth")); - return url.search.includes("oauth"); -}, new NetworkOnly()); - export function register(onUpdate: () => void) { if ("serviceWorker" in navigator) { window.addEventListener("load", async () => { diff --git a/webpack.config.js b/webpack.config.js index 1ec2ea93..556ee92a 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,6 +5,7 @@ const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const ContextReplacementPlugin = webpack.ContextReplacementPlugin; const TerserPlugin = require("terser-webpack-plugin"); const { GenerateSW } = require("workbox-webpack-plugin"); + const supportedLocales = ["en"]; const isDev = process.argv.includes("--mode=development"); @@ -21,18 +22,31 @@ module.exports = { clean: true, }, plugins: [ - isDev - ? new GenerateSW({ - //https://developer.chrome.com/docs/workbox/modules/workbox-webpack-plugin#type-GenerateSW - // default maximum size is 3mb while our main chunk is 4mb - // that's why size is sincreased to 6mb - maximumFileSizeToCacheInBytes: 6291456, - cleanupOutdatedCaches: true, - clientsClaim: true, - sourcemap: false, - skipWaiting: true, - }) - : [], + !isDev && + new GenerateSW({ + //https://developer.chrome.com/docs/workbox/modules/workbox-webpack-plugin#type-GenerateSW + // default maximum size is 3mb while our main chunk is 4mb + // that's why size is sincreased to 6mb + maximumFileSizeToCacheInBytes: 6291456, + cleanupOutdatedCaches: true, + clientsClaim: true, + sourcemap: false, + skipWaiting: true, + exclude: [/index\.html$/, /.*oauth.*/, /\.map$/], + runtimeCaching: [ + { + urlPattern: ({ request }) => + (request.mode === "navigate" || + request.headers.get("accept")?.includes("text/html")) && + !request.url.includes("oauth"), + + handler: "NetworkFirst", + options: { + cacheName: "html-pages", + }, + }, + ], + }), new ContextReplacementPlugin( /date-fns[\/\\]/, new RegExp(`[/\\\\\](${supportedLocales.join("|")})[/\\\\\]`) From ecc8f0e09a12577040e8da67a9748abcfb805e68 Mon Sep 17 00:00:00 2001 From: EduardZaydler <117674342+EduardZaydler@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:54:15 +0700 Subject: [PATCH 3/3] removed update timeout --- src/serviceWorkerRegistration.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/serviceWorkerRegistration.ts b/src/serviceWorkerRegistration.ts index 24b3090a..7f77bb49 100644 --- a/src/serviceWorkerRegistration.ts +++ b/src/serviceWorkerRegistration.ts @@ -4,14 +4,6 @@ export function register(onUpdate: () => void) { try { const registration = await navigator.serviceWorker.register("/service-worker.js"); - setInterval(async () => { - try { - await registration.update(); - } catch (error) { - console.error("Error updating service worker:", error); - } - }, 1000 * 60); - registration.onupdatefound = () => { const installingWorker = registration.installing; if (installingWorker) {