Skip to content

Commit

Permalink
Don't check version.txt if the service worker is in use
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmz committed Aug 28, 2024
1 parent b7cd33b commit 0d10556
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/components/app-updated.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@ import { useSelector } from 'react-redux';
import { useRegisterSW } from 'virtual:pwa-register/react';

import { useEvent } from 'react-use-event-hook';
import { useState } from 'react';
import styles from './app-updated.module.scss';
import { ButtonLink } from './button-link';

const { intervalSec } = CONFIG.appVersionCheck;

export function AppUpdated() {
const updated = useSelector((state) => state.appUpdated.updated);
const versionFileUpdated = useSelector((state) => state.appUpdated.updated);
const [swRegistered, setSwRegistered] = useState(false);

const {
needRefresh: [needRefresh],
needRefresh: [workerUpdated],
updateServiceWorker,
} = useRegisterSW({
onRegistered(r) {
r && setInterval(() => r.update(), intervalSec * 1000);
if (r) {
setSwRegistered(true);
setInterval(() => r.update(), intervalSec * 1000);
}
},
});

const reloadPage = useEvent(() => {
if (needRefresh) {
if (workerUpdated) {
updateServiceWorker();
// Sometimes the updateServiceWorker doesn't refresh the page, so reload
// it manually after some time
Expand All @@ -31,11 +36,9 @@ export function AppUpdated() {
}
});

if (!updated && !needRefresh) {
return null;
}
const needRefresh = swRegistered ? workerUpdated : versionFileUpdated;

return (
return needRefresh ? (
<div className={styles.bar}>
<div className={styles.indicator}>
There’s an update for {CONFIG.siteTitle}!{' '}
Expand All @@ -45,5 +48,5 @@ export function AppUpdated() {
when you are ready.
</div>
</div>
);
) : null;
}

0 comments on commit 0d10556

Please sign in to comment.