Skip to content
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
4 changes: 3 additions & 1 deletion db/kv-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ function getKV(): KVNamespace {
export async function kvCached<T>(
key: string,
fn: () => Promise<T>,
opts?: { ttl?: number },
): Promise<T> {
const kv = getKV();
const cached = await kv.get(key, "json");
if (cached !== null) return cached as T;

const result = await fn();
// Fire-and-forget write — don't block the response
kv.put(key, JSON.stringify(result)).catch(() => {});
const putOpts = opts?.ttl ? { expirationTtl: opts.ttl } : undefined;
kv.put(key, JSON.stringify(result), putOpts).catch(() => {});
return result;
}

Expand Down
12 changes: 8 additions & 4 deletions src/lib/server-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,14 @@ export const fetchRecentApps = createServerFn({ method: "GET" })
export const fetchPopularApps = createServerFn({ method: "GET" })
// eslint-disable-next-line @typescript-eslint/no-explicit-any
.handler(async (): Promise<any> => {
return kvCached(cacheKey("getPopularApps"), () => {
const db = getDb();
return getPopularApps(db);
});
return kvCached(
cacheKey("getPopularApps"),
() => {
const db = getDb();
return getPopularApps(db);
},
{ ttl: 86400 },
);
});

export const fetchComparisonBySlug = createServerFn({ method: "GET" })
Expand Down
6 changes: 3 additions & 3 deletions src/routes/-worker-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const SITE_URL = "https://unclouded.app";

type CacheRule = { pattern: RegExp; header: string };

// Data only changes on manual DB writes (seed/enrich), then cache:purge.
// Cache everything aggressively — 1 week fresh, 1 week stale fallback.
// Static content cached for 1 week, pages with dynamic data (homepage) for 1 day.
const ONE_WEEK = 604800;
const ONE_DAY = 86400;

const cacheRules: CacheRule[] = [
{ pattern: /^\/search/, header: "no-store" },
Expand All @@ -40,7 +40,7 @@ const cacheRules: CacheRule[] = [
},
{
pattern: /^\/(apps|alternatives|discover|desktop)?$/,
header: `public, s-maxage=${ONE_WEEK}, stale-while-revalidate=${ONE_WEEK}`,
header: `public, s-maxage=${ONE_DAY}, stale-while-revalidate=${ONE_DAY}`,
},
];

Expand Down
Loading