mirror of
https://github.com/bitsocialnet/5chan.git
synced 2026-08-03 07:41:04 +02:00
fix(pwa): make web updates deterministic (#1093)
* fix(pwa): make web updates deterministic * fix(pwa): address review feedback
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { useEffect } from 'react';
|
||||
import packageJson from '../../../package.json';
|
||||
import { fetchLatestStableVersion, isElectron, refreshServiceWorkerRegistration } from '../../lib/app-update';
|
||||
import useAppUpdateStore from '../../stores/use-app-update-store';
|
||||
|
||||
const UPDATE_CHECK_INTERVAL_MS = 60 * 1000;
|
||||
|
||||
const AppUpdateRegistration = () => {
|
||||
useEffect(() => {
|
||||
if (isElectron) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
let isDisposed = false;
|
||||
|
||||
const syncUpdateAvailability = async () => {
|
||||
await refreshServiceWorkerRegistration().catch((error) => {
|
||||
console.error('Failed to refresh service worker registration', error);
|
||||
});
|
||||
|
||||
try {
|
||||
const latestStableVersion = await fetchLatestStableVersion();
|
||||
|
||||
if (!isDisposed) {
|
||||
useAppUpdateStore.getState().setNeedRefresh(packageJson.version !== latestStableVersion);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to check app update availability', error);
|
||||
}
|
||||
};
|
||||
|
||||
void syncUpdateAvailability();
|
||||
const intervalId = window.setInterval(() => {
|
||||
void syncUpdateAvailability();
|
||||
}, UPDATE_CHECK_INTERVAL_MS);
|
||||
return () => {
|
||||
isDisposed = true;
|
||||
window.clearInterval(intervalId);
|
||||
useAppUpdateStore.getState().setNeedRefresh(false);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
export default AppUpdateRegistration;
|
||||
@@ -0,0 +1 @@
|
||||
export { default } from './app-update-registration';
|
||||
Reference in New Issue
Block a user