Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PWA regularly check for service worker updates and automaticially reload the page #1188

Merged
merged 16 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions modern/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SocketController from './SocketController';
import CachingController from './CachingController';
import { useEffectAsync } from './reactHelper';
import { sessionActions } from './store';
import UpdateCheckPrompt from './UpdateCheckPrompt';

const useStyles = makeStyles(() => ({
page: {
Expand Down Expand Up @@ -48,6 +49,7 @@ const App = () => {
<>
<SocketController />
<CachingController />
<UpdateCheckPrompt />
<div className={classes.page}>
<Outlet />
</div>
Expand Down
57 changes: 57 additions & 0 deletions modern/src/UpdateCheckPrompt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Snackbar, Button } from '@mui/material';
import React from 'react'
import { useTranslation } from './common/components/LocalizationProvider';
import { useAttributePreference } from './common/util/preferences';
import { useRegisterSW } from 'virtual:pwa-register/react'

// Based on https://vite-pwa-org.netlify.app/frameworks/react.html
function UpdateCheckPrompt() {
const t = useTranslation();

const serviceWorkerUpdateInterval = useAttributePreference('serviceWorkerUpdateInterval', 3600000);

const {
needRefresh: [needRefresh],
updateServiceWorker,
} = useRegisterSW({
onRegisteredSW(swUrl, swRegistration) {
if (serviceWorkerUpdateInterval > 0 && swRegistration) {
setInterval(async () => {
if (!(!swRegistration.installing && navigator)) {
return;
}

if (('connection' in navigator) && !navigator.onLine) {
return;
}

const newSW = await fetch(swUrl, {
cache: 'no-store',
headers: {
'cache': 'no-store',
'cache-control': 'no-cache',
},
});

if (newSW?.status === 200) {
await swRegistration.update();
}
}, serviceWorkerUpdateInterval);
}
}
});

return (
<Snackbar
open={needRefresh}
message={t('settingsUpdateAvailable')}
action={(
<Button onClick={() => updateServiceWorker(true)}>
{t('settingsReload')}
</Button>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just use an icon button? Like this one:

https://mui.com/material-ui/material-icons/?query=ref&selected=Refresh

It looks better and also we can remove one of the strings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, did not even think about this.
image

)}
/>
jinzo marked this conversation as resolved.
Show resolved Hide resolved
);
}

export default UpdateCheckPrompt;
4 changes: 4 additions & 0 deletions modern/src/common/attributes/useServerAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export default (t) => useMemo(() => ({
name: t('settingsTotpForce'),
type: 'boolean',
},
serviceWorkerUpdateInterval: {
name: t('settingsServiceWorkerUpdateInterval'),
type: 'number',
},
'ui.disableLoginLanguage': {
name: t('attributeUiDisableLoginLanguage'),
type: 'boolean',
Expand Down
3 changes: 3 additions & 0 deletions modern/src/resources/l10n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@
"settingsDarkMode": "Dark Mode",
"settingsTotpEnable": "Enable One-time Password",
"settingsTotpForce": "Force One-time Password",
"settingsServiceWorkerUpdateInterval": "ServiceWorker Update Interval",
"settingsReload": "Reload",
"settingsUpdateAvailable": "There is an update available.",
"reportTitle": "Reports",
"reportScheduled": "Scheduled Reports",
"reportDevice": "Device",
Expand Down
1 change: 1 addition & 0 deletions modern/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite-plugin-pwa/react" />
jinzo marked this conversation as resolved.
Show resolved Hide resolved
1 change: 0 additions & 1 deletion modern/vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export default defineConfig(() => ({
svgr(),
react(),
VitePWA({
registerType: 'autoUpdate',
workbox: {
navigateFallbackDenylist: [/^\/api/],
},
Expand Down