From d17a17bd3c9a183edff5195bc35accf89c0f4e7f Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 28 Dec 2022 18:33:14 -0800 Subject: [PATCH] Use server-side endpoint to properly cache GH release data --- src/components/version.jsx | 6 +----- src/pages/api/releases.js | 6 ++++++ 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 src/pages/api/releases.js diff --git a/src/components/version.jsx b/src/components/version.jsx index ef2977500..de79647ba 100644 --- a/src/components/version.jsx +++ b/src/components/version.jsx @@ -3,8 +3,6 @@ import useSWR from "swr"; import { compareVersions } from "compare-versions"; import { MdNewReleases } from "react-icons/md"; -import cachedFetch from "utils/proxy/cached-fetch"; - export default function Version() { const { t, i18n } = useTranslation(); @@ -12,9 +10,7 @@ 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 cachedFetcher = (resource) => cachedFetch(resource, 5); - - const { data: releaseData } = useSWR("https://api.github.com/repos/benphelps/homepage/releases", cachedFetcher); + const { data: releaseData } = useSWR("/api/releases"); // use Intl.DateTimeFormat to format the date const formatDate = (date) => { diff --git a/src/pages/api/releases.js b/src/pages/api/releases.js new file mode 100644 index 000000000..b5b3df009 --- /dev/null +++ b/src/pages/api/releases.js @@ -0,0 +1,6 @@ +import cachedFetch from "utils/proxy/cached-fetch"; + +export default async function handler(req, res) { + const releasesURL = "https://api.github.com/repos/benphelps/homepage/releases"; + return res.send(await cachedFetch(releasesURL, 5)); +}