mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-10-27 02:16:23 -04:00
- Replace `"use cache"` with `"use cache: remote"` on functions that run inside dynamic holes (`getContributions`, `getRepos`, `getPostStats`, `getCachedComments`) so entries are shared across server instances
- Switch comment and view server actions from `revalidatePath`/`revalidateTag` to `updateTag` + `refresh` for read-your-writes invalidation
- Extract a `commentsTag(pageSlug)` helper so each page's comment cache is expired independently instead of blowing the whole "comments" tag
- Refactor `getComments` to delegate to a cached inner function that throws on failure (so errors are never cached); `getCommentCount` now reuses that cache entry instead of issuing a separate COUNT query
- Drop `lib/data/views.ts` (`getAllViewCounts`) — the `/api/hits` route now calls `getPostStats()` instead
- Remove `revalidateTag("views")` from `incrementViews`; view counts in `getPostStats` are allowed to lag by a cache TTL
- Remove the `Suspense`/`Skeleton` wrapper on the projects page since `getContributions` is now a remote-cached call
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import "server-only";
|
|
import { sql } from "drizzle-orm";
|
|
import { cacheLife, cacheTag } from "next/cache";
|
|
|
|
import { db } from "@/lib/db";
|
|
import { comment, page } from "@/lib/db/schema";
|
|
|
|
export type PostStatsData = {
|
|
views: Record<string, number>;
|
|
comments: Record<string, number>;
|
|
};
|
|
|
|
/**
|
|
* View and comment counts for ALL pages, keyed by slug (e.g. "notes/dark-mode").
|
|
* Cached briefly (views can lag by a minute or so); comment changes expire it via `updateTag("comments")`.
|
|
*/
|
|
export const getPostStats = async (): Promise<PostStatsData> => {
|
|
"use cache: remote";
|
|
cacheLife("minutes");
|
|
cacheTag("comments");
|
|
|
|
try {
|
|
const [pageRows, commentRows] = await Promise.all([
|
|
db.select({ slug: page.slug, views: page.views }).from(page),
|
|
db
|
|
.select({
|
|
pageSlug: comment.pageSlug,
|
|
count: sql<number>`cast(count(${comment.id}) as int)`,
|
|
})
|
|
.from(comment)
|
|
.groupBy(comment.pageSlug),
|
|
]);
|
|
|
|
return {
|
|
views: Object.fromEntries(pageRows.map((row) => [row.slug, row.views])),
|
|
comments: Object.fromEntries(commentRows.map((row) => [row.pageSlug, row.count])),
|
|
};
|
|
} catch (error) {
|
|
console.error("[data/stats] error fetching post stats:", error);
|
|
return { views: {}, comments: {} };
|
|
}
|
|
};
|