Feature: cache release data, allow disable release checking (#4917)

This commit is contained in:
shamoon 2025-03-14 08:34:59 -07:00 committed by GitHub
parent 544b9aef2f
commit 859bd459a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 7 deletions

View file

@ -1,9 +1,12 @@
import cache from "memory-cache";
import { useTranslation } from "next-i18next";
import useSWR from "swr";
import { compareVersions, validate } from "compare-versions";
import { MdNewReleases } from "react-icons/md";
export default function Version() {
const LATEST_RELEASE_CACHE_KEY = "latestRelease";
export default function Version({ disableUpdateCheck = false }) {
const { t, i18n } = useTranslation();
const buildTime = process.env.NEXT_PUBLIC_BUILDTIME?.length
@ -12,8 +15,6 @@ export default function Version() {
const revision = process.env.NEXT_PUBLIC_REVISION?.length ? process.env.NEXT_PUBLIC_REVISION : "dev";
const version = process.env.NEXT_PUBLIC_VERSION?.length ? process.env.NEXT_PUBLIC_VERSION : "dev";
const { data: releaseData } = useSWR("/api/releases");
// use Intl.DateTimeFormat to format the date
const formatDate = (date) => {
const options = {
@ -24,7 +25,15 @@ export default function Version() {
return new Intl.DateTimeFormat(i18n.language, options).format(new Date(date));
};
const latestRelease = releaseData?.[0];
let latestRelease = cache.get(LATEST_RELEASE_CACHE_KEY);
const { data: releaseData } = useSWR(latestRelease || disableUpdateCheck ? null : "/api/releases");
if (releaseData) {
latestRelease = releaseData?.[0];
// cache the latest release for 1h
cache.put(LATEST_RELEASE_CACHE_KEY, latestRelease, 3600000);
}
return (
<div id="version" className="flex flex-row items-center">