1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-06-27 17:25:43 -04:00
Files
jarv.is/app/api/hits/route.ts
2025-05-14 09:49:55 -04:00

36 lines
871 B
TypeScript

import { NextResponse } from "next/server";
import { unstable_cache as cache } from "next/cache";
import { getViews as _getViews } from "@/lib/server/views";
const getViews = cache(_getViews, undefined, {
revalidate: 300, // 5 minutes
tags: ["hits"],
});
export const GET = async (): Promise<
NextResponse<{
total: {
hits: number;
};
pages: Array<{
slug: string;
hits: number;
}>;
}>
> => {
// note: while hits have been renamed to views in most places, this API shouldn't change due to it being snapshotted
const views = await getViews();
const total = {
hits: Object.values(views).reduce((acc, curr) => acc + curr, 0),
};
const pages = Object.entries(views).map(([slug, views]) => ({
slug,
hits: views,
}));
pages.sort((a, b) => b.hits - a.hits);
return NextResponse.json({ total, pages });
};