forked from revoltchat/revite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add auto-update and out-of-date indicator
- Loading branch information
Showing
10 changed files
with
145 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Text } from "preact-i18n"; | ||
|
||
import { Modal } from "@revoltchat/ui"; | ||
|
||
import { noop, noopTrue } from "../../../lib/js"; | ||
|
||
import { APP_VERSION } from "../../../version"; | ||
import { ModalProps } from "../types"; | ||
|
||
export default function OutOfDate({ | ||
onClose, | ||
version, | ||
}: ModalProps<"out_of_date">) { | ||
return ( | ||
<Modal | ||
title={<Text id="app.special.modals.out_of_date.title" />} | ||
description={ | ||
<> | ||
<Text id="app.special.modals.out_of_date.description" /> | ||
<br /> | ||
<Text | ||
id="app.special.modals.out_of_date.version" | ||
fields={{ client: APP_VERSION, server: version }} | ||
/> | ||
</> | ||
} | ||
actions={[ | ||
{ | ||
palette: "plain", | ||
onClick: noop, | ||
children: ( | ||
<Text id="app.special.modals.out_of_date.attempting" /> | ||
), | ||
}, | ||
{ | ||
palette: "plain-secondary", | ||
onClick: noopTrue, | ||
children: ( | ||
<Text id="app.special.modals.out_of_date.ignore" /> | ||
), | ||
}, | ||
]} | ||
onClose={onClose} | ||
nonDismissable | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,8 @@ | ||
import { registerSW } from "virtual:pwa-register"; | ||
|
||
import "./styles/index.scss"; | ||
import { render } from "preact"; | ||
|
||
import { internalEmit } from "./lib/eventEmitter"; | ||
|
||
import { App } from "./pages/app"; | ||
|
||
export const updateSW = registerSW({ | ||
onNeedRefresh() { | ||
internalEmit("PWA", "update"); | ||
}, | ||
onOfflineReady() { | ||
console.info("Ready to work offline."); | ||
// show a ready to work offline to user | ||
}, | ||
}); | ||
import "./updateWorker"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
render(<App />, document.getElementById("app")!); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import semver from "semver"; | ||
import { ulid } from "ulid"; | ||
import { registerSW } from "virtual:pwa-register"; | ||
|
||
import { internalEmit } from "./lib/eventEmitter"; | ||
|
||
import { modalController } from "./context/modals"; | ||
|
||
import { APP_VERSION } from "./version"; | ||
|
||
const INTERVAL_HOUR = 36e5; | ||
|
||
let forceUpdate = false; | ||
let registration: ServiceWorkerRegistration | undefined; | ||
|
||
export const updateSW = registerSW({ | ||
onNeedRefresh() { | ||
if (forceUpdate) { | ||
updateSW(true); | ||
} else { | ||
internalEmit("PWA", "update"); | ||
} | ||
}, | ||
onOfflineReady() { | ||
console.info("Ready to work offline."); | ||
// show a ready to work offline to user | ||
}, | ||
onRegistered(r) { | ||
registration = r; | ||
|
||
// Check for updates every hour | ||
setInterval(() => r!.update(), INTERVAL_HOUR); | ||
}, | ||
}); | ||
|
||
/** | ||
* Check whether the client is out of date | ||
*/ | ||
async function checkVersion() { | ||
const { version } = (await fetch("https://api.revolt.chat/release").then( | ||
(res) => res.json(), | ||
)) as { version: string }; | ||
|
||
if (!semver.satisfies(APP_VERSION, version)) { | ||
// Let the worker know we should immediately refresh | ||
forceUpdate = true; | ||
|
||
// Prompt service worker to update | ||
registration?.update(); | ||
|
||
// Push information that the client is out of date | ||
modalController.push({ | ||
key: ulid(), | ||
type: "out_of_date", | ||
version, | ||
}); | ||
} | ||
} | ||
|
||
if (import.meta.env.VITE_API_URL === "https://api.revolt.chat") { | ||
// Check for critical updates hourly | ||
checkVersion(); | ||
setInterval(checkVersion, INTERVAL_HOUR); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters