Skip to content

Commit

Permalink
chore/sw_cache (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardZaydler authored May 21, 2024
1 parent 03a460c commit 0589cf0
Show file tree
Hide file tree
Showing 9 changed files with 1,741 additions and 38 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"webpack-bundle-analyzer": "4.9.1",
"webpack-cli": "5.1.4",
"webpack-dev-server": "4.15.1",
"webpack-merge": "5.9.0"
"webpack-merge": "5.9.0",
"workbox-webpack-plugin": "7.1.0"
},
"resolutions": {
"@types/react-router": "5.1.18",
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Layout/Layout.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@import '~styles/variables.less';
@import '~styles/mixins.less';
@import "~styles/variables.less";
@import "~styles/mixins.less";

.layout {
display: flex;
Expand Down
2 changes: 2 additions & 0 deletions src/Components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useRef, ReactNode, FC, useEffect } from "react";
import { Loader } from "@skbkontur/react-ui/components/Loader";
import WarningIcon from "@skbkontur/react-icons/Warning";
import { NewVersionAvailableHint } from "../NewVersionAvailableHint/NewVersionAvailableHint";
import classNames from "classnames/bind";

import styles from "./Layout.less";
Expand Down Expand Up @@ -48,6 +49,7 @@ export const Layout: FC<ILayoutProps> = ({ loading = false, error = null, childr
<Loader className={cn("loading")} active={loading} caption="Loading">
{children}
</Loader>
<NewVersionAvailableHint />
</main>
);
};
Expand Down
17 changes: 17 additions & 0 deletions src/Components/NewVersionAvailableHint/NewVersionAvailableHint.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import { Tooltip } from "@skbkontur/react-ui";
import { useAppSelector } from "../../store/hooks";
import { UIState } from "../../store/selectors";
import Info from "@skbkontur/react-icons/Info";

export const NewVersionAvailableHint = () => {
const { isNewFrontendVersionAvailable } = useAppSelector(UIState);

return isNewFrontendVersionAvailable ? (
<div style={{ position: "fixed", bottom: "30px", right: "30px" }}>
<Tooltip render={() => "New version available, please reload the page"}>
<Info size={30} />
</Tooltip>
</div>
) : null;
};
9 changes: 9 additions & 0 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@ import * as Sentry from "@sentry/react";
import ErrorContainer from "./Containers/ErrorContainer";
import { Providers } from "./Providers/Providers";
import SentryInitializer from "./Components/SentryInitializer/SentryInitializer";
import * as serviceWorkerRegistration from "./serviceWorkerRegistration";
import { updateServiceWorker } from "./store/Reducers/UIReducer.slice";
import { store } from "./store/store";

import "./style.less";

const root = document.getElementById("root");

const onUpdate = () => {
store.dispatch(updateServiceWorker());
};

const render = (Component: ComponentType) => {
if (root !== null) {
ReactDOM.render(
Expand Down Expand Up @@ -41,4 +48,6 @@ const load = (): void => {
}
};

serviceWorkerRegistration.register(onUpdate);

export { load as default };
33 changes: 33 additions & 0 deletions src/serviceWorkerRegistration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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) {
installingWorker.onstatechange = () => {
if (
installingWorker.state === "installed" &&
navigator.serviceWorker.controller
) {
onUpdate && onUpdate();
}
};
}
};
} catch (error) {
console.error("Error during service worker registration:", error);
}
});
}
}
7 changes: 6 additions & 1 deletion src/store/Reducers/UIReducer.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { PayloadAction, createSlice } from "@reduxjs/toolkit";
interface IUIState {
isLoading: boolean;
error: string;
isNewFrontendVersionAvailable: boolean;
}

const initialState: IUIState = {
isLoading: false,
error: "",
isNewFrontendVersionAvailable: false,
};

export const UISlice = createSlice({
Expand All @@ -20,9 +22,12 @@ export const UISlice = createSlice({
setError: (state, action) => {
state.error = action.payload;
},
updateServiceWorker: (state) => {
state.isNewFrontendVersionAvailable = true;
},
},
});

export const { toggleLoading, setError } = UISlice.actions;
export const { toggleLoading, setError, updateServiceWorker } = UISlice.actions;

export default UISlice.reducer;
19 changes: 18 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const HtmlWebpackPlugin = require("html-webpack-plugin");
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 @@ -20,6 +21,16 @@ 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,
}),
new ContextReplacementPlugin(
/date-fns[\/\\]/,
new RegExp(`[/\\\\\](${supportedLocales.join("|")})[/\\\\\]`)
Expand Down Expand Up @@ -84,7 +95,13 @@ module.exports = {

optimization: {
minimize: true,
minimizer: [new TerserPlugin({ parallel: true, terserOptions: { sourceMap: true } })],
minimizer: [
new TerserPlugin({
parallel: true,
extractComments: false,
terserOptions: { sourceMap: true },
}),
],
usedExports: true,
splitChunks: {
chunks: "all",
Expand Down
Loading

0 comments on commit 0589cf0

Please sign in to comment.