From 4ddd1d32196099de9f0bf592af4c94b7b1eb89aa Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sat, 11 Mar 2023 21:32:46 +0800 Subject: [PATCH] Fix update check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change from 1-hour when visible (which is like impossible) to… every page visible debounced up to 1 hour + 30 min interval when visible --- src/app.jsx | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/app.jsx b/src/app.jsx index ed6146a8..3ec2bfaf 100644 --- a/src/app.jsx +++ b/src/app.jsx @@ -264,7 +264,9 @@ function App() { return !/^\/(login|welcome)/.test(pathname); }, [location]); - useInterval(() => { + const lastCheckDate = useRef(); + const checkForUpdates = () => { + lastCheckDate.current = Date.now(); console.log('✨ Check app update'); fetch('./version.json') .then((r) => r.json()) @@ -274,7 +276,21 @@ function App() { .catch((e) => { console.error(e); }); - }, visible && 1000 * 60 * 60); // 1 hour + }; + useInterval(() => checkForUpdates, visible && 1000 * 60 * 30); // 30 minutes + usePageVisibility((visible) => { + if (visible) { + if (!lastCheckDate.current) { + checkForUpdates(); + } else { + const diff = Date.now() - lastCheckDate.current; + if (diff > 1000 * 60 * 60) { + // 1 hour + checkForUpdates(); + } + } + } + }); return ( <>