From 97363edd2ad3c2222c44c4bd30edaecca301dedf Mon Sep 17 00:00:00 2001 From: sashapanasiuk5 <51517423+sashapanasiuk5@users.noreply.github.com> Date: Fri, 13 Sep 2024 15:43:41 +0300 Subject: [PATCH] fix problem with infinite reloading --- .env | 3 ++- src/app/common/components/withClearCache.tsx | 21 +++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.env b/.env index e8f0ecca3..2248819df 100644 --- a/.env +++ b/.env @@ -2,4 +2,5 @@ REACT_APP_BACKEND_URL=https://localhost:5001/api REACT_APP_TEMPVAL=tempval API_URL=https://best.api.ever.com REACT_APP_GOOGLE_ANALYTICS=tempval -RECAPTCHA_SITE_KEY="6LeUO3ApAAAAAOC7F4v0qTsSwIR9mZu33SWjAAtM" \ No newline at end of file +RECAPTCHA_SITE_KEY="6LeUO3ApAAAAAOC7F4v0qTsSwIR9mZu33SWjAAtM" +VERSION=1.0.0 \ No newline at end of file diff --git a/src/app/common/components/withClearCache.tsx b/src/app/common/components/withClearCache.tsx index 84f3fe966..0c92ef1e0 100644 --- a/src/app/common/components/withClearCache.tsx +++ b/src/app/common/components/withClearCache.tsx @@ -1,8 +1,8 @@ /* eslint-disable no-underscore-dangle */ -import { Component, useEffect, useState } from 'react'; +import { useEffect, useState } from 'react'; const WithClearCache: React.FC<{ children: React.ReactNode }> = ({ children }) => { - const [isLatestBuildDate, setIsLatestBuildDate] = useState(false); + const [isLatestBuildDate, setIsLatestBuildDate] = useState(true); const refreshCacheAndReload = () => { if (caches) { @@ -11,20 +11,23 @@ const WithClearCache: React.FC<{ children: React.ReactNode }> = ({ children }) = caches.delete(name); } }); - console.log("Cache is cleared") + console.log('Cache is cleared'); } window.location.reload(); }; useEffect(() => { const localVersion = localStorage.getItem('VERSION'); - const isVersionMatches = localVersion === window._env_.VERSION; - setIsLatestBuildDate(isVersionMatches); - if (!isVersionMatches) { - localStorage.setItem('VERSION', window._env_.VERSION); - refreshCacheAndReload(); + const envVersion = window._env_.VERSION; + if (typeof envVersion !== 'undefined' && localVersion !== 'undefined') { + const isVersionMatches = localVersion === envVersion; + setIsLatestBuildDate(isVersionMatches); + if (!isVersionMatches) { + localStorage.setItem('VERSION', window._env_.VERSION); + refreshCacheAndReload(); + } } - }, []); + }); if (isLatestBuildDate) { return children;