From b337f76da5ec5c2d0f9e06588406f991f1ec7869 Mon Sep 17 00:00:00 2001 From: trueberryless Date: Sun, 23 Feb 2025 19:56:28 +0100 Subject: [PATCH] add caching --- docs/astro.config.ts | 3 ++ .../components/DynamicVersionBadge.astro | 2 +- .../components/VersionBadge.astro | 36 ++++++++++++++++++- .../libs/utils.ts | 4 ++- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/docs/astro.config.ts b/docs/astro.config.ts index ab9b63f..793102c 100644 --- a/docs/astro.config.ts +++ b/docs/astro.config.ts @@ -59,4 +59,7 @@ export default defineConfig({ adapter: node({ mode: "standalone", }), + experimental: { + session: true, + }, }); diff --git a/packages/starlight-plugin-show-latest-version/components/DynamicVersionBadge.astro b/packages/starlight-plugin-show-latest-version/components/DynamicVersionBadge.astro index ac69d50..b3db30e 100644 --- a/packages/starlight-plugin-show-latest-version/components/DynamicVersionBadge.astro +++ b/packages/starlight-plugin-show-latest-version/components/DynamicVersionBadge.astro @@ -8,7 +8,7 @@ import { Badge } from '@astrojs/starlight/components'; )} {pluginConfig.showInSiteTitle === "deferred" && ( - + )} diff --git a/packages/starlight-plugin-show-latest-version/components/VersionBadge.astro b/packages/starlight-plugin-show-latest-version/components/VersionBadge.astro index 37ed435..743275a 100644 --- a/packages/starlight-plugin-show-latest-version/components/VersionBadge.astro +++ b/packages/starlight-plugin-show-latest-version/components/VersionBadge.astro @@ -1,10 +1,44 @@ --- +import type { StarlightPluginShowLatestVersionContext } from '../libs/types'; import { Badge } from '@astrojs/starlight/components'; import fetchVersion from '../libs/utils'; import { releasePageUrls } from "../libs/urlBuilder"; import pluginConfig from 'virtual:starlight-plugin-show-latest-version-config'; -const version = await fetchVersion(pluginConfig); +interface Props { + useCache?: boolean; +} + +const { useCache = false } = Astro.props; + +let version: StarlightPluginShowLatestVersionContext = { versionAvailable: false }; +if (useCache) { + // This setup caches the result in Astro.session for 1 hour. + // Call `clearVersionCache` to manually invalidate the cache. 🚀 + const CACHE_EXPIRATION_MS = 60 * 60 * 1000; + + const cacheKey = `versionCache:${pluginConfig.source.type}:${pluginConfig.source.slug}`; + const cachedData = await Astro.session?.get(cacheKey); + + if (cachedData) { + const { timestamp, context } = JSON.parse(cachedData); + if (Date.now() - timestamp < CACHE_EXPIRATION_MS) { + version = context; + } + else { + version = await fetchVersion(pluginConfig); + await Astro.session?.set(cacheKey, JSON.stringify({ timestamp: Date.now(), context: version })); + } + } + else { + version = await fetchVersion(pluginConfig); + await Astro.session?.set(cacheKey, JSON.stringify({ timestamp: Date.now(), context: version })); + } +} +else { + version = await fetchVersion(pluginConfig); +} + const sourceUrl = releasePageUrls[pluginConfig.source.type](pluginConfig.source.slug); --- diff --git a/packages/starlight-plugin-show-latest-version/libs/utils.ts b/packages/starlight-plugin-show-latest-version/libs/utils.ts index bba87d0..72a4afc 100644 --- a/packages/starlight-plugin-show-latest-version/libs/utils.ts +++ b/packages/starlight-plugin-show-latest-version/libs/utils.ts @@ -40,7 +40,7 @@ export default async function fetchVersion( const prefix = prefixMatch ? prefixMatch[1] : undefined; const hasVPrefix = tagName.startsWith("v") || tagName.includes("@v"); - return { + const context: StarlightPluginShowLatestVersionContext = { versionAvailable: true, version, versionWithoutPrefix, @@ -53,6 +53,8 @@ export default async function fetchVersion( hasVPrefix, isStableVersion: !isPrereleaseVersion, }; + + return context; } catch (error) { console.error(error); return { versionAvailable: false }; // Fallback: no version available