diff --git a/config/webpack.dev.js b/config/webpack.dev.js index 972ea2752..e44e562cb 100644 --- a/config/webpack.dev.js +++ b/config/webpack.dev.js @@ -38,53 +38,57 @@ module.exports = { new Dotenv({ path: `./.env`, }), - // new GenerateSW({ - // skipWaiting: true, - // clientsClaim: true, - // maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, - // runtimeCaching: [ - // { - // urlPattern: /\.(?:js|css)$/, - // handler: 'CacheFirst', - // options: { - // cacheName: 'static-resources', - // expiration: { - // maxAgeSeconds: 60 * 60 * 24 * 7, // 1 week - // }, - // }, - // }, - // { - // urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/, - // handler: 'CacheFirst', - // options: { - // cacheName: 'image-resources', - // expiration: { - // maxAgeSeconds: 60 * 60 * 24 * 7, // 1 week - // }, - // }, - // }, - // { - // urlPattern: /\.(?:woff|woff2|ttf|otf)$/, - // handler:'CacheFirst', - // options: { - // cacheName: 'font-resources', - // expiration: { - // maxAgeSeconds: 60 * 60 * 24 * 90, // 3 months - // }, - // }, - // }, - // { - // urlPattern: /^https?.*/, - // handler: 'NetworkFirst', - // options: { - // cacheName: 'external-resources', - // expiration: { - // maxEntries: 260 - // }, - // }, - // }, - // ], - // }), + new GenerateSW({ + skipWaiting: true, + clientsClaim: true, + maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, + runtimeCaching: [ + { + urlPattern: /env-config\.js$/, + handler: 'NetworkOnly' + }, + { + urlPattern: /\.(?:js|css)$/, + handler: 'CacheFirst', + options: { + cacheName: 'static-resources', + expiration: { + maxAgeSeconds: 60 * 60 * 24 * 7, // 1 week + }, + }, + }, + { + urlPattern: /\.(?:png|jpg|jpeg|svg|gif)$/, + handler: 'CacheFirst', + options: { + cacheName: 'image-resources', + expiration: { + maxAgeSeconds: 60 * 60 * 24 * 7, // 1 week + }, + }, + }, + { + urlPattern: /\.(?:woff|woff2|ttf|otf)$/, + handler:'CacheFirst', + options: { + cacheName: 'font-resources', + expiration: { + maxAgeSeconds: 60 * 60 * 24 * 90, // 3 months + }, + }, + }, + { + urlPattern: /^https?.*/, + handler: 'NetworkFirst', + options: { + cacheName: 'external-resources', + expiration: { + maxEntries: 260 + }, + }, + }, + ], + }), ], optimization: { splitChunks: { diff --git a/config/webpack.prod.js b/config/webpack.prod.js index 7063db33d..c39d6c22b 100644 --- a/config/webpack.prod.js +++ b/config/webpack.prod.js @@ -52,6 +52,10 @@ module.exports = { clientsClaim: true, maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, runtimeCaching: [ + { + urlPattern: /env-config\.js$/, + handler: 'NetworkOnly' + }, { urlPattern: /\.(?:js|css)$/, handler: 'NetworkFirst', diff --git a/public/env-config.js b/public/env-config.js index 111a97b97..fbd5c3254 100644 --- a/public/env-config.js +++ b/public/env-config.js @@ -3,4 +3,5 @@ window._env_ = { API_URL: "https://stageback.streetcode.com.ua/api", REACT_APP_GOOGLE_ANALYTICS: "REACT_APP_GOOGLE_ANALYTICS_VALUE", RECAPTCHA_SITE_KEY: "6LeUO3ApAAAAAOC7F4v0qTsSwIR9mZu33SWjAAtM", + VERSION: "1.0.0" } diff --git a/src/app/common/components/withClearCache.tsx b/src/app/common/components/withClearCache.tsx index 0c92ef1e0..2e7d37779 100644 --- a/src/app/common/components/withClearCache.tsx +++ b/src/app/common/components/withClearCache.tsx @@ -1,3 +1,4 @@ +/* eslint-disable no-restricted-syntax */ /* eslint-disable no-underscore-dangle */ import { useEffect, useState } from 'react'; @@ -10,24 +11,24 @@ const WithClearCache: React.FC<{ children: React.ReactNode }> = ({ children }) = for (const name of names) { caches.delete(name); } + console.log('Cache is cleared'); + window.location.reload(); }); - console.log('Cache is cleared'); } - window.location.reload(); }; useEffect(() => { const localVersion = localStorage.getItem('VERSION'); const envVersion = window._env_.VERSION; - if (typeof envVersion !== 'undefined' && localVersion !== 'undefined') { + if (typeof envVersion !== 'undefined') { const isVersionMatches = localVersion === envVersion; setIsLatestBuildDate(isVersionMatches); if (!isVersionMatches) { - localStorage.setItem('VERSION', window._env_.VERSION); + localStorage.setItem('VERSION', envVersion); refreshCacheAndReload(); } } - }); + }, [isLatestBuildDate]); if (isLatestBuildDate) { return children; diff --git a/src/index.tsx b/src/index.tsx index c3bf711c2..a1524593b 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -8,6 +8,7 @@ import { RouterProvider } from 'react-router-dom'; import router from '@app/router/Routes'; import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; +import WithClearCache from './app/common/components/withClearCache'; declare global { interface Window { @@ -16,6 +17,7 @@ declare global { SERVER_API_URL: string; REACT_APP_GOOGLE_ANALYTICS: string; RECAPTCHA_SITE_KEY: string; + VERSION: string; }; } } @@ -39,10 +41,12 @@ const root = ReactDOM.createRoot( const queryClient = new QueryClient(); root.render( - - - - - - , + + + + + + + + , );