From 833f37dad84806bca5a39fe01a1f1e8b90e7e322 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sun, 8 Apr 2018 13:31:58 +0200 Subject: [PATCH] Added new version checker --- scripts/AnimeNotifier.ts | 24 ++++------------------ scripts/NewVersionCheck.ts | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 20 deletions(-) create mode 100644 scripts/NewVersionCheck.ts diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index 77d4778d..d2e04930 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -11,6 +11,7 @@ import InfiniteScroller from "./InfiniteScroller" import ServiceWorkerManager from "./ServiceWorkerManager" import { displayAiringDate, displayDate, displayTime } from "./DateView" import { findAll, canUseWebP, requestIdleCallback, swapElements, delay } from "./Utils" +import { checkNewVersion } from "./NewVersionCheck" import * as actions from "./Actions" export default class AnimeNotifier { @@ -34,7 +35,6 @@ export default class AnimeNotifier { diffCompletedForCurrentPath: boolean lastReloadContentPath: string currentSoundTrackId: string - etags: Map constructor(app: Application) { this.app = app @@ -196,25 +196,9 @@ export default class AnimeNotifier { // Bind unload event window.addEventListener("beforeunload", this.onBeforeUnload.bind(this)) - // Check etags of scripts and styles - this.etags = new Map() - - delay(2000).then(async () => { - let response = await fetch("/scripts") - let newETag = response.headers.get("ETag") - let oldETag = this.etags.get("/scripts") - - this.etags.set("/scripts", newETag) - console.log("etags", this.etags) - - if(!oldETag || !newETag) { - return - } - - if(oldETag !== newETag) { - this.statusMessage.showInfo("A new version of the website is available. Please refresh the page.", -1) - } - }) + // Periodically check etags of scripts and styles to let the user know about page updates + checkNewVersion("/scripts", this.statusMessage) + checkNewVersion("/styles", this.statusMessage) // // Download popular anime titles for the search // let response = await fetch("/api/popular/anime/titles/500") diff --git a/scripts/NewVersionCheck.ts b/scripts/NewVersionCheck.ts new file mode 100644 index 00000000..050eb680 --- /dev/null +++ b/scripts/NewVersionCheck.ts @@ -0,0 +1,41 @@ +import { delay } from "./Utils" +import StatusMessage from "./StatusMessage" + +let etags = new Map() +let hasNewVersion = false +let newVersionCheckDelay = 60000 + +export async function checkNewVersion(url: string, statusMessage: StatusMessage) { + if(hasNewVersion) { + return + } + + try { + let response = await fetch(url) + + if(!response.ok) { + console.warn("Error fetch", url) + return + } + + let newETag = response.headers.get("ETag") + let oldETag = etags.get(url) + + if(newETag) { + etags.set(url, newETag) + } + + if(oldETag && newETag && oldETag !== newETag) { + statusMessage.showInfo("A new version of the website is available. Please refresh the page.", -1) + + // Do not check for new versions again. + hasNewVersion = true + return + } + } catch(err) { + console.warn("Error fetching", url + "\n", err) + } + + delay(newVersionCheckDelay).then(() => checkNewVersion(url, statusMessage)) +} +