1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-03 14:06:40 -04:00

edge functions didn't make sense for database calls

This commit is contained in:
2023-10-08 21:38:09 -04:00
parent 23844c6843
commit 179ee92f6c
11 changed files with 203 additions and 239 deletions

View File

@ -1,33 +1,16 @@
import { NextResponse } from "next/server";
import { prisma } from "../../lib/helpers/prisma";
import type { NextRequest } from "next/server";
import type { NextApiHandler } from "next";
import type { PageStats } from "../../types";
export const config = {
runtime: "edge",
regions: ["iad1"], // the vercel postgres database lives in DC
};
const handler: NextApiHandler<PageStats> = async (req, res) => {
const { slug } = req.query;
// eslint-disable-next-line import/no-anonymous-default-export
export default async (req: NextRequest) => {
const slug = req.nextUrl.searchParams.get("slug");
if (!slug) {
return NextResponse.json({ message: "Missing `slug` parameter." }, { status: 400 });
if (typeof slug !== "string" || slug === "") {
// @ts-expect-error
return res.status(400).json({ message: "Missing `slug` parameter." });
}
// add one to this page's count and return the new number
return NextResponse.json(await incrementPageHits(slug), {
status: 200,
headers: {
// disable caching on both ends. see:
// https://vercel.com/docs/concepts/functions/edge-functions/edge-caching
"Cache-Control": "private, no-cache, no-store, must-revalidate",
},
});
};
const incrementPageHits = async (slug: string): Promise<PageStats> => {
// +1 hit!
const { hits } = await prisma.hits.upsert({
where: { slug },
create: { slug },
@ -38,6 +21,12 @@ const incrementPageHits = async (slug: string): Promise<PageStats> => {
},
});
// send client the *new* hit count
return { hits };
// disable caching on both ends. see:
// https://vercel.com/docs/concepts/functions/edge-functions/edge-caching
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
// add one to this page's count and return the new number
return res.status(200).json({ hits });
};
export default handler;

View File

@ -1,23 +1,15 @@
import { prisma } from "../../lib/helpers/prisma";
import { NextResponse } from "next/server";
import type { NextApiHandler } from "next";
import type { SiteStats } from "../../types";
export const config = {
runtime: "edge",
regions: ["iad1"], // the vercel postgres database lives in DC
};
// eslint-disable-next-line import/no-anonymous-default-export
export default async () => {
// simultaneously fetch the entire hits db and notes from the filesystem
const handler: NextApiHandler<SiteStats> = async (req, res) => {
// fetch all rows from db sorted by most hits
const pages = await prisma.hits.findMany({
orderBy: [
{
hits: "desc",
},
],
// cache db results for 5 minutes. prisma accelerate only:
// https://www.prisma.io/docs/data-platform/accelerate/concepts#cache-strategies
cacheStrategy: { swr: 300, ttl: 60 },
});
const total = { hits: 0 };
@ -28,5 +20,10 @@ export default async () => {
total.hits += page.hits;
});
return NextResponse.json({ total, pages }, { status: 200 });
// let Vercel edge cache results for 15 mins
res.setHeader("Cache-Control", "public, max-age=0, s-maxage=900, stale-while-revalidate");
return res.status(200).json({ total, pages });
};
export default handler;