diff --git a/apps/api/src/lib/geekdaily.ts b/apps/api/src/lib/geekdaily.ts index 10b56bb..d5681f8 100644 --- a/apps/api/src/lib/geekdaily.ts +++ b/apps/api/src/lib/geekdaily.ts @@ -19,19 +19,92 @@ import { buildPaginatedMeta, resolvePagination, type PaginationInput } from './p import { combineFilters, toContainsPattern } from './query-filters.js'; import { toIsoString } from './utils.js'; -const loadItemsByEpisodeId = async () => { +const mapEpisodeItem = (row: typeof geekdailyEpisodeItems.$inferSelect) => ({ + title: row.title, + authorName: row.authorName, + sourceUrl: row.sourceUrl, + summary: row.summary, +}); + +const mapSearchEpisodeItem = (row: Pick) => ({ + title: row.title, + authorName: row.authorName, + summary: row.summary, +}); + +const PUBLIC_GEEKDAILY_CACHE_TTL_MS = 5 * 60 * 1000; + +const publicGeekDailyCache = new Map }>(); + +const withPublicGeekDailyCache = async (key: string, loader: () => Promise) => { + const now = Date.now(); + const cached = publicGeekDailyCache.get(key); + if (cached && cached.expiresAt > now) { + return cached.value as Promise; + } + + const promise = loader().catch((error) => { + const current = publicGeekDailyCache.get(key); + if (current?.value === promise) { + publicGeekDailyCache.delete(key); + } + throw error; + }); + + publicGeekDailyCache.set(key, { + expiresAt: now + PUBLIC_GEEKDAILY_CACHE_TTL_MS, + value: promise, + }); + + return promise; +}; + +export const invalidatePublicGeekDailyCache = () => { + publicGeekDailyCache.clear(); +}; + +const loadItemsByEpisodeIds = async (episodeIds?: string[]) => { const db = getDb(); - const rows = await db.select().from(geekdailyEpisodeItems).orderBy(asc(geekdailyEpisodeItems.sortOrder)); + if (episodeIds && episodeIds.length === 0) { + return new Map[]>(); + } + + const query = db.select().from(geekdailyEpisodeItems); + const rows = episodeIds + ? await query.where(inArray(geekdailyEpisodeItems.episodeId, episodeIds)).orderBy(asc(geekdailyEpisodeItems.sortOrder)) + : await query.orderBy(asc(geekdailyEpisodeItems.sortOrder)); const result = new Map(); for (const row of rows) { const current = result.get(row.episodeId) ?? []; - current.push({ - title: row.title, - authorName: row.authorName, - sourceUrl: row.sourceUrl, - summary: row.summary, - }); + current.push(mapEpisodeItem(row)); + result.set(row.episodeId, current); + } + + return result; +}; + +const loadSearchItemsByEpisodeIds = async (episodeIds: string[]) => { + const db = getDb(); + if (episodeIds.length === 0) { + return new Map[]>(); + } + + const rows = await db + .select({ + episodeId: geekdailyEpisodeItems.episodeId, + title: geekdailyEpisodeItems.title, + authorName: geekdailyEpisodeItems.authorName, + summary: geekdailyEpisodeItems.summary, + }) + .from(geekdailyEpisodeItems) + .where(inArray(geekdailyEpisodeItems.episodeId, episodeIds)) + .orderBy(asc(geekdailyEpisodeItems.sortOrder)); + const result = new Map[]>(); + + for (const row of rows) { + const current = result.get(row.episodeId) ?? []; + current.push(mapSearchEpisodeItem(row)); result.set(row.episodeId, current); } @@ -67,6 +140,9 @@ const getEpisodeRecordBySlug = async (slug: string) => { const getEditors = (value: unknown) => Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string' && item.trim().length > 0) : []; +const getTags = (value: unknown) => + Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string' && item.trim().length > 0) : []; + const resolveEpisodeEditors = (actor: AuditActor, fallback: unknown = []) => { const actorName = actor.actorDisplayName?.trim(); if (actorName) { @@ -97,6 +173,22 @@ const mapEpisodeDetail = (row: any, items: any[]) => ({ updatedAt: toIsoString(row.updatedAt) ?? new Date().toISOString(), }); +const mapPublicEpisodePreview = (row: any, items: any[]) => ({ + slug: getGeekDailyEpisodeSlug(row.episodeNumber), + episodeNumber: row.episodeNumber, + title: row.title, + summary: row.summary, + publishedAt: toIsoString(row.publishedAt) ?? new Date().toISOString(), + editors: getEditors(row.editorsJson), + tags: getTags(row.tagsJson), + items, +}); + +const mapPublicEpisode = (row: any, items: any[]) => ({ + ...mapPublicEpisodePreview(row, items), + body: row.bodyMarkdown, +}); + const ensureEpisodeNumberAvailable = async (episodeNumber: number, currentId?: string) => { const db = getDb(); const rows = await db @@ -203,7 +295,7 @@ export const getAdminGeekDailyEpisode = async (id: string) => { const db = getDb(); const [rows, itemsByEpisode] = await Promise.all([ db.select().from(geekdailyEpisodes).where(eq(geekdailyEpisodes.id, id)).limit(1), - loadItemsByEpisodeId(), + loadItemsByEpisodeIds([id]), ]); const row = rows[0] ?? null; return row ? mapEpisodeDetail(row, itemsByEpisode.get(row.id) ?? []) : null; @@ -231,6 +323,7 @@ export const createAdminGeekDailyEpisode = async (input: GeekDailyEpisodeInput, .returning(); await replaceItems(created.id, input.items); + invalidatePublicGeekDailyCache(); await createAuditEntry({ ...actor, @@ -271,6 +364,7 @@ export const updateAdminGeekDailyEpisode = async (id: string, input: GeekDailyEp .where(eq(geekdailyEpisodes.id, id)); await replaceItems(id, input.items); + invalidatePublicGeekDailyCache(); await createAuditEntry({ ...actor, @@ -307,6 +401,7 @@ export const publishAdminGeekDailyEpisode = async (id: string, actor: AuditActor updatedAt: new Date(), }) .where(eq(geekdailyEpisodes.id, id)); + invalidatePublicGeekDailyCache(); await createAuditEntry({ ...actor, @@ -334,6 +429,7 @@ export const archiveAdminGeekDailyEpisode = async (id: string, actor: AuditActor updatedAt: new Date(), }) .where(eq(geekdailyEpisodes.id, id)); + invalidatePublicGeekDailyCache(); await createAuditEntry({ ...actor, @@ -347,68 +443,126 @@ export const archiveAdminGeekDailyEpisode = async (id: string, actor: AuditActor }; export const listPublicGeekDailyEpisodes = async (limit = -1) => { - const db = getDb(); - const query = db - .select() - .from(geekdailyEpisodes) - .where(eq(geekdailyEpisodes.status, 'published')) - .orderBy(desc(geekdailyEpisodes.episodeNumber)); - const rows = limit > 0 ? await query.limit(limit) : await query; - const itemsByEpisode = await loadItemsByEpisodeId(); - - return rows.map((row) => ({ - slug: getGeekDailyEpisodeSlug(row.episodeNumber), - episodeNumber: row.episodeNumber, - title: row.title, - summary: row.summary, - publishedAt: toIsoString(row.publishedAt) ?? new Date().toISOString(), - editors: getEditors(row.editorsJson), - tags: Array.isArray(row.tagsJson) ? row.tagsJson : [], - body: row.bodyMarkdown, - items: itemsByEpisode.get(row.id) ?? [], - })); + return withPublicGeekDailyCache(`episodes:${limit}`, async () => { + const db = getDb(); + const query = db + .select({ + id: geekdailyEpisodes.id, + episodeNumber: geekdailyEpisodes.episodeNumber, + title: geekdailyEpisodes.title, + summary: geekdailyEpisodes.summary, + bodyMarkdown: geekdailyEpisodes.bodyMarkdown, + editorsJson: geekdailyEpisodes.editorsJson, + tagsJson: geekdailyEpisodes.tagsJson, + publishedAt: geekdailyEpisodes.publishedAt, + }) + .from(geekdailyEpisodes) + .where(eq(geekdailyEpisodes.status, 'published')) + .orderBy(desc(geekdailyEpisodes.episodeNumber)); + const rows = limit > 0 ? await query.limit(limit) : await query; + const itemsByEpisode = await loadItemsByEpisodeIds(rows.map((row) => row.id)); + + return rows.map((row) => mapPublicEpisode(row, itemsByEpisode.get(row.id) ?? [])); + }); +}; + +export const listPublicGeekDailyEpisodePreviews = async (limit = -1) => { + return withPublicGeekDailyCache(`previews:${limit}`, async () => { + const db = getDb(); + const query = db + .select({ + id: geekdailyEpisodes.id, + episodeNumber: geekdailyEpisodes.episodeNumber, + title: geekdailyEpisodes.title, + summary: geekdailyEpisodes.summary, + editorsJson: geekdailyEpisodes.editorsJson, + tagsJson: geekdailyEpisodes.tagsJson, + publishedAt: geekdailyEpisodes.publishedAt, + }) + .from(geekdailyEpisodes) + .where(eq(geekdailyEpisodes.status, 'published')) + .orderBy(desc(geekdailyEpisodes.episodeNumber)); + const rows = limit > 0 ? await query.limit(limit) : await query; + const itemsByEpisode = await loadItemsByEpisodeIds(rows.map((row) => row.id)); + + return rows.map((row) => mapPublicEpisodePreview(row, itemsByEpisode.get(row.id) ?? [])); + }); }; export const getPublicGeekDailyEpisodeBySlug = async (slug: string) => { - const [row, itemsByEpisode] = await Promise.all([getEpisodeRecordBySlug(slug), loadItemsByEpisodeId()]); - if (!row || row.status !== 'published') { - return null; - } + return withPublicGeekDailyCache(`episode:${slug}`, async () => { + const row = await getEpisodeRecordBySlug(slug); + if (!row || row.status !== 'published') { + return null; + } + const itemsByEpisode = await loadItemsByEpisodeIds([row.id]); - return { - slug: getGeekDailyEpisodeSlug(row.episodeNumber), - episodeNumber: row.episodeNumber, - title: row.title, - summary: row.summary, - publishedAt: toIsoString(row.publishedAt) ?? new Date().toISOString(), - editors: getEditors(row.editorsJson), - tags: Array.isArray(row.tagsJson) ? row.tagsJson : [], - body: row.bodyMarkdown, - items: itemsByEpisode.get(row.id) ?? [], - }; + return mapPublicEpisode(row, itemsByEpisode.get(row.id) ?? []); + }); +}; + +export const getPublicGeekDailyOverview = async () => { + return withPublicGeekDailyCache('overview', async () => { + const db = getDb(); + const [countRow, yearRows, tagRows] = await Promise.all([ + db.select({ value: count() }).from(geekdailyEpisodes).where(eq(geekdailyEpisodes.status, 'published')), + db + .select({ + year: sql`extract(year from ${geekdailyEpisodes.publishedAt})::int`, + }) + .from(geekdailyEpisodes) + .where(eq(geekdailyEpisodes.status, 'published')) + .groupBy(sql`1`) + .orderBy(desc(sql`1`)), + db + .select({ + tagsJson: geekdailyEpisodes.tagsJson, + }) + .from(geekdailyEpisodes) + .where(eq(geekdailyEpisodes.status, 'published')), + ]); + + const years = yearRows.map((row) => row.year).filter(Number.isFinite); + const featuredTags = [...new Set(tagRows.flatMap((row) => getTags(row.tagsJson)))].slice(0, 8); + + return { + totalEpisodes: countRow[0]?.value ?? 0, + years, + featuredTags, + }; + }); }; export const getGeekDailySearchDocuments = async () => { - const episodes = await listPublicGeekDailyEpisodes(); - return episodes.map((episode) => ({ - slug: episode.slug, - episodeNumber: episode.episodeNumber, - title: episode.title, - summary: episode.summary, - body: episode.body, - tags: episode.tags, - publishedAt: episode.publishedAt, - year: String(new Date(episode.publishedAt).getUTCFullYear()), - itemTitles: episode.items.map((item: any) => item.title), - searchableText: [ - episode.title, - episode.summary, - episode.body, - episode.editors.join(' '), - episode.tags.join(' '), - ...episode.items.flatMap((item: any) => [item.title, item.authorName, item.summary]), - ] - .join(' ') - .toLowerCase(), - })); + return withPublicGeekDailyCache('search-documents', async () => { + const db = getDb(); + const rows = await db + .select({ + id: geekdailyEpisodes.id, + episodeNumber: geekdailyEpisodes.episodeNumber, + title: geekdailyEpisodes.title, + summary: geekdailyEpisodes.summary, + tagsJson: geekdailyEpisodes.tagsJson, + publishedAt: geekdailyEpisodes.publishedAt, + }) + .from(geekdailyEpisodes) + .where(eq(geekdailyEpisodes.status, 'published')) + .orderBy(desc(geekdailyEpisodes.episodeNumber)); + const itemsByEpisode = await loadSearchItemsByEpisodeIds(rows.map((row) => row.id)); + + return rows.map((row) => { + const publishedAt = toIsoString(row.publishedAt) ?? new Date().toISOString(); + + return { + slug: getGeekDailyEpisodeSlug(row.episodeNumber), + episodeNumber: row.episodeNumber, + title: row.title, + summary: row.summary, + tags: getTags(row.tagsJson), + publishedAt, + year: String(new Date(publishedAt).getUTCFullYear()), + items: itemsByEpisode.get(row.id) ?? [], + }; + }); + }); }; diff --git a/apps/api/src/routes/public.ts b/apps/api/src/routes/public.ts index b6ea1d8..04273ec 100644 --- a/apps/api/src/routes/public.ts +++ b/apps/api/src/routes/public.ts @@ -4,11 +4,18 @@ import { ok } from '../lib/http.js'; import { getPublicArticleBySlug, listPublicArticles } from '../lib/articles.js'; import { listPublicContributorGroups } from '../lib/contributors.js'; import { getPublicEventBySlug, listPublicEvents } from '../lib/events.js'; -import { getGeekDailySearchDocuments, getPublicGeekDailyEpisodeBySlug, listPublicGeekDailyEpisodes } from '../lib/geekdaily.js'; +import { + getGeekDailySearchDocuments, + getPublicGeekDailyEpisodeBySlug, + getPublicGeekDailyOverview, + listPublicGeekDailyEpisodePreviews, + listPublicGeekDailyEpisodes, +} from '../lib/geekdaily.js'; import { getPublicJobBySlug, listPublicJobs } from '../lib/jobs.js'; import { getPublicAboutPage, getPublicSiteConfig } from '../lib/site.js'; export const publicRoutes = new Hono(); +const publicCacheControl = 'public, max-age=300, stale-while-revalidate=3600'; const getPositiveLimit = (value: string | undefined, fallback: number) => { const parsed = Number.parseInt(value ?? '', 10); @@ -134,16 +141,25 @@ publicRoutes.get('/contributors', async (c) => c.json(ok(await listPublicContrib publicRoutes.get('/geekdaily', async (c) => { const limit = getPositiveLimit(c.req.query('limit'), 0); - const rows = await listPublicGeekDailyEpisodes(); - return c.json(ok(limit > 0 ? rows.slice(0, limit) : rows)); + c.header('Cache-Control', publicCacheControl); + return c.json(ok(await listPublicGeekDailyEpisodePreviews(limit))); +}); + +publicRoutes.get('/geekdaily/overview', async (c) => { + c.header('Cache-Control', publicCacheControl); + return c.json(ok(await getPublicGeekDailyOverview())); }); -publicRoutes.get('/geekdaily/search', async (c) => c.json(ok(await getGeekDailySearchDocuments()))); +publicRoutes.get('/geekdaily/search', async (c) => { + c.header('Cache-Control', publicCacheControl); + return c.json(ok(await getGeekDailySearchDocuments())); +}); publicRoutes.get('/geekdaily/:slug', async (c) => { const record = await getPublicGeekDailyEpisodeBySlug(c.req.param('slug')); if (!record) { return c.json({ error: { code: 'NOT_FOUND', message: 'geekdaily episode not found' } }, 404); } + c.header('Cache-Control', publicCacheControl); return c.json(ok(record)); }); diff --git a/apps/web/src/lib/content.ts b/apps/web/src/lib/content.ts index 6ac4fd0..2d72830 100644 --- a/apps/web/src/lib/content.ts +++ b/apps/web/src/lib/content.ts @@ -45,6 +45,12 @@ interface PublicGeekDailyItemPayload { summary: string; } +type GeekDailyEpisodePreview = Omit; + +interface PublicGeekDailyPreviewPayload extends Omit { + items: PublicGeekDailyItemPayload[]; +} + interface PublicGeekDailyEpisodePayload extends Omit { items: PublicGeekDailyItemPayload[]; } @@ -54,12 +60,20 @@ interface GeekDailySearchDocument { episodeNumber: number; title: string; summary: string; - body: string; tags: string[]; publishedAt: string; year: string; - itemTitles: string[]; - searchableText: string; + items: Array<{ + title: string; + authorName: string; + summary: string; + }>; +} + +interface GeekDailyArchiveOverviewPayload { + totalEpisodes: number; + years: number[]; + featuredTags: string[]; } interface HomeFeedPayload { @@ -122,6 +136,16 @@ const mapGeekDailyEpisode = (episode: PublicGeekDailyEpisodePayload): GeekDailyE })), }); +const mapGeekDailyEpisodePreview = (episode: PublicGeekDailyPreviewPayload): GeekDailyEpisodePreview => ({ + ...episode, + items: episode.items.map((item) => ({ + title: item.title, + author: item.authorName, + sourceUrl: item.sourceUrl, + summary: item.summary, + })), +}); + export async function getSiteSettings() { return mapSiteSettings(await fetchPublicApi('/api/public/v1/site-config')); } @@ -188,10 +212,10 @@ export async function getContributorsByRole() { return fetchPublicApi('/api/public/v1/contributors'); } -export async function getGeekDailyEpisodes(limit = -1): Promise { +export async function getGeekDailyEpisodes(limit = -1): Promise { const suffix = limit > 0 ? `?limit=${limit}` : ''; - const episodes = await fetchPublicApi(`/api/public/v1/geekdaily${suffix}`); - return episodes.map(mapGeekDailyEpisode); + const episodes = await fetchPublicApi(`/api/public/v1/geekdaily${suffix}`); + return episodes.map(mapGeekDailyEpisodePreview); } export async function getGeekDailyEpisodeBySlug(slug: string) { @@ -203,15 +227,7 @@ export async function getGeekDailyEpisodeBySlug(slug: string) { } export async function getGeekDailyArchiveOverview() { - const documents = await getGeekDailySearchDocuments(); - const years = [...new Set(documents.map((record) => Number(record.year)).filter(Number.isFinite))]; - const featuredTags = [...new Set(documents.flatMap((record) => record.tags))].slice(0, 8); - - return { - totalEpisodes: documents.length, - years, - featuredTags, - }; + return fetchPublicApi('/api/public/v1/geekdaily/overview'); } export async function getGeekDailySearchDocuments() { diff --git a/apps/web/src/pages/api/geekdaily-search.json.ts b/apps/web/src/pages/api/geekdaily-search.json.ts index ec8bcdf..1c1d1c9 100644 --- a/apps/web/src/pages/api/geekdaily-search.json.ts +++ b/apps/web/src/pages/api/geekdaily-search.json.ts @@ -6,7 +6,7 @@ export async function GET() { return new Response(JSON.stringify(documents), { headers: { 'content-type': 'application/json; charset=utf-8', - 'cache-control': 'public, max-age=300', + 'cache-control': 'public, max-age=300, stale-while-revalidate=3600', }, }); } diff --git a/apps/web/src/pages/geekdaily/[slug].astro b/apps/web/src/pages/geekdaily/[slug].astro index 2e4313b..10811b6 100644 --- a/apps/web/src/pages/geekdaily/[slug].astro +++ b/apps/web/src/pages/geekdaily/[slug].astro @@ -33,7 +33,7 @@ const extractGeekDailyBodyNote = (bodyMarkdown: string) => { }; const slug = Astro.params.slug ?? ''; -const [site, episode, episodes] = await Promise.all([getSiteSettings(), getGeekDailyEpisodeBySlug(slug), getGeekDailyEpisodes()]); +const [site, episode, episodes] = await Promise.all([getSiteSettings(), getGeekDailyEpisodeBySlug(slug), getGeekDailyEpisodes(3)]); const related = episode ? episodes.filter((item) => item.slug !== episode.slug).slice(0, 2) : []; const episodeNote = episode ? extractGeekDailyBodyNote(episode.body) : ''; const episodeActionLinks = [ @@ -48,6 +48,8 @@ const episodeRelatedLinks = related.map((item) => ({ if (!episode) { Astro.response.status = 404; } + +Astro.response.headers.set('Cache-Control', 'public, max-age=300, stale-while-revalidate=3600'); --- {episode.title}

{episode.summary}

-

{episode.body}

    {episode.items.slice(0, 3).map((item) =>
  • {item.title}
  • )}
@@ -114,18 +115,24 @@ const featuredTags = overview.featuredTags;