Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caching #32

Merged
merged 1 commit into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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