From 6711f363ef1af9c63851b19c01a09f4ded9aa6da Mon Sep 17 00:00:00 2001 From: EduardZaydler <117674342+EduardZaydler@users.noreply.github.com> Date: Wed, 16 Oct 2024 23:09:07 +0700 Subject: [PATCH] generate sw --- service-worker.js | 22 ---------------------- webpack.config.js | 36 ++++++++++++++++++++++++++---------- 2 files changed, 26 insertions(+), 32 deletions(-) delete mode 100644 service-worker.js diff --git a/service-worker.js b/service-worker.js deleted file mode 100644 index eb4fce8a..00000000 --- a/service-worker.js +++ /dev/null @@ -1,22 +0,0 @@ -import { precacheAndRoute } from "workbox-precaching/precacheAndRoute"; -import { CacheableResponsePlugin } from "workbox-cacheable-response"; -import { NetworkFirst } from "workbox-strategies"; -import { registerRoute } from "workbox-routing"; - -precacheAndRoute( - self.__WB_MANIFEST.filter((entry) => !entry.url.includes("index.html" || "oauth")) -); -registerRoute( - ({ request }) => - (request.mode === "navigate" || request.headers.get("accept")?.includes("text/html")) && - !request.url.includes("oauth"), - - new NetworkFirst({ - cacheName: "html-pages", - plugins: [ - new CacheableResponsePlugin({ - statuses: [0, 200], - }), - ], - }) -); diff --git a/webpack.config.js b/webpack.config.js index 8a04f564..735ab8f1 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -4,7 +4,8 @@ const HtmlWebpackPlugin = require("html-webpack-plugin"); const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const ContextReplacementPlugin = webpack.ContextReplacementPlugin; const TerserPlugin = require("terser-webpack-plugin"); -const { InjectManifest } = require("workbox-webpack-plugin"); +const { GenerateSW } = require("workbox-webpack-plugin"); + const supportedLocales = ["en"]; const isDev = process.argv.includes("--mode=development"); @@ -21,16 +22,31 @@ module.exports = { clean: true, }, plugins: [ - // !isDev && - new InjectManifest({ - //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 + !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**"], + runtimeCaching: [ + { + urlPattern: ({ request }) => + request.mode === "navigate" || + (request.headers.get("accept")?.includes("text/html") && + !request.url.includes("oauth")), - maximumFileSizeToCacheInBytes: 6291456, - swSrc: "/service-worker.js", - swDest: "service-worker.js", - }), + handler: "NetworkFirst", + options: { + cacheName: "html-pages", + }, + }, + ], + }), new ContextReplacementPlugin( /date-fns[\/\\]/, new RegExp(`[/\\\\\](${supportedLocales.join("|")})[/\\\\\]`)