Skip to content

Commit

Permalink
fix/oauth_cache (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardZaydler authored Oct 23, 2024
1 parent e104982 commit eb28f8c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
15 changes: 0 additions & 15 deletions src/serviceWorkerRegistration.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import { registerRoute } from "workbox-routing";
import { NetworkOnly } from "workbox-strategies";

registerRoute(({ url }) => {
return url.pathname.includes("/oauth");
}, new NetworkOnly());

export function register(onUpdate: () => void) {
if ("serviceWorker" in navigator) {
window.addEventListener("load", async () => {
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) {
Expand Down
36 changes: 26 additions & 10 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -21,16 +22,31 @@ 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,
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("|")})[/\\\\\]`)
Expand Down

0 comments on commit eb28f8c

Please sign in to comment.