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)) +} +