diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index d2e04930..b547d5e4 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -11,7 +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 { checkNewVersionDelayed } from "./NewVersionCheck" import * as actions from "./Actions" export default class AnimeNotifier { @@ -197,8 +197,8 @@ export default class AnimeNotifier { window.addEventListener("beforeunload", this.onBeforeUnload.bind(this)) // Periodically check etags of scripts and styles to let the user know about page updates - checkNewVersion("/scripts", this.statusMessage) - checkNewVersion("/styles", this.statusMessage) + checkNewVersionDelayed("/scripts", this.statusMessage) + checkNewVersionDelayed("/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 index c07e701d..cf080071 100644 --- a/scripts/NewVersionCheck.ts +++ b/scripts/NewVersionCheck.ts @@ -5,16 +5,29 @@ let etags = new Map() let hasNewVersion = false let newVersionCheckDelay = 60000 -export async function checkNewVersion(url: string, statusMessage: StatusMessage) { +async function checkNewVersion(url: string, statusMessage: StatusMessage) { if(hasNewVersion) { return } try { - let response = await fetch(url) + let headers = {} + + if(etags.has(url)) { + headers["If-None-Match"] = etags.get(url) + } + + let response = await fetch(url, { + headers + }) + + // Not modified response + if(response.status === 304) { + return + } if(!response.ok) { - console.warn("Error fetching", url) + console.warn("Error fetching", url, response.status) return } @@ -34,8 +47,11 @@ export async function checkNewVersion(url: string, statusMessage: StatusMessage) } } catch(err) { console.warn("Error fetching", url + "\n", err) + } finally { + checkNewVersionDelayed(url, statusMessage) } - - delay(newVersionCheckDelay).then(() => checkNewVersion(url, statusMessage)) } +export async function checkNewVersionDelayed(url: string, statusMessage: StatusMessage) { + delay(newVersionCheckDelay).then(() => checkNewVersion(url, statusMessage)) +}