Skip to content

Commit

Permalink
Merge pull request #32 from trueberryless-org/caching
Browse files Browse the repository at this point in the history
Add caching
  • Loading branch information
trueberryless authored Feb 23, 2025
2 parents 7033a1a + b337f76 commit c3de8d3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ export default defineConfig({
adapter: node({
mode: "standalone",
}),
experimental: {
session: true,
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Badge } from '@astrojs/starlight/components';
<VersionBadge />
)}
{pluginConfig.showInSiteTitle === "deferred" && (
<VersionBadge server:defer>
<VersionBadge server:defer useCache={true}>
<Badge text={"Loading..."} variant="default" size="medium" slot="fallback" />
</VersionBadge>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
---

Expand Down
4 changes: 3 additions & 1 deletion packages/starlight-plugin-show-latest-version/libs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit c3de8d3

Please sign in to comment.