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

🔄 Auto update CARE #4506

Merged
merged 4 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions src/Components/Common/UpdatableApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const APP_UPDATED_KEY = "app-updated";

interface UpdatableAppProps {
children: ReactNode;
silentlyAutoUpdate?: boolean;
}

const checkForUpdate = async () => {
Expand All @@ -32,6 +33,11 @@ const checkForUpdate = async () => {

const meta = await res.json();

if (appVersion === null) {
// Skip updating since the app potentially is the latest version.
localStorage.setItem(APP_VERSION_KEY, meta.version);
}

if (appVersion !== meta.version) {
// Trigger an update if key: 'app-version' not present in localStorage
// or does not match with metaVersion.
Expand All @@ -40,7 +46,7 @@ const checkForUpdate = async () => {
}
};

const UpdatableApp = ({ children }: UpdatableAppProps) => {
const UpdatableApp = ({ children, silentlyAutoUpdate }: UpdatableAppProps) => {
const [newVersion, setNewVersion] = useState<string>();
const [appUpdated, setAppUpdated] = useState(false);

Expand All @@ -66,23 +72,27 @@ const UpdatableApp = ({ children }: UpdatableAppProps) => {
if (!newVersion) return;

// Service worker cache should be cleared with caches.delete()
if ("caches" in window) {
caches.keys().then((names) => names.forEach(caches.delete));
}
caches.keys().then((names) => names.forEach((name) => caches.delete(name)));

// A second of delay to appreciate the update animation.
setTimeout(() => {
const updateLocalStorageAndReload = () => {
localStorage.setItem(APP_UPDATED_KEY, "true");
window.location.reload();
localStorage.setItem(APP_VERSION_KEY, newVersion);
}, 1000);
};

silentlyAutoUpdate
? updateLocalStorageAndReload()
: setTimeout(updateLocalStorageAndReload, 1000);
};

if (newVersion && silentlyAutoUpdate) updateApp();

return (
<div className="relative">
{children}
{newVersion && <UpdateAppPopup onUpdate={updateApp} />}
<AppUpdatedAlert show={appUpdated && !newVersion} />
<AppUpdatedAlert show={appUpdated} />
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const theme = createMuiTheme({
ReactDOM.render(
<Provider store={store}>
<ThemeProvider theme={theme}>
<UpdatableApp>
<UpdatableApp silentlyAutoUpdate>
<App />
</UpdatableApp>
</ThemeProvider>
Expand Down