1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-04-26 15:28:28 -04:00

separate page/site stats API endpoints to lower memory usage

This commit is contained in:
Jake Jarvis 2022-07-10 17:26:22 -04:00
parent 87305b5424
commit 9be41b5e5c
Signed by: jake
GPG Key ID: 2B0C9CF251E69A39
6 changed files with 58 additions and 44 deletions

View File

@ -1 +1 @@
16.15.1
16.16.0

View File

@ -12,7 +12,7 @@ const HitCounter = ({ slug }: HitCounterProps) => {
// use immutable SWR to avoid double (or more) counting views:
// https://swr.vercel.app/docs/revalidation#disable-automatic-revalidations
const { data, error } = useSWRImmutable<PageStats>(
`/api/hits/?${new URLSearchParams({
`/api/count/?${new URLSearchParams({
slug,
})}`,
fetcher

View File

@ -118,7 +118,7 @@
},
"packageManager": "yarn@1.22.19",
"volta": {
"node": "16.15.1",
"node": "16.16.0",
"yarn": "1.22.19"
}
}

47
pages/api/count.ts Normal file
View File

@ -0,0 +1,47 @@
import { prisma } from "../../lib/helpers/prisma";
import { logServerError } from "../../lib/helpers/sentry";
import type { NextApiRequest, NextApiResponse } from "next";
import type { PageStats } from "../../types";
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (!req.query?.slug) {
return res.status(400).json({ message: "Missing `slug` parameter." });
}
// add one to this page's count and return the new number
const result = await incrementPageHits(req.query.slug as string);
// disable caching on both ends
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
// send result as JSON
return res.status(200).json(result);
} catch (error) {
// extract just the error message to send back to client
const message = error instanceof Error ? error.message : error;
// log full error to console and sentry
await logServerError(error);
// 500 Internal Server Error
return res.status(500).json({ message });
}
};
const incrementPageHits = async (slug: string): Promise<PageStats> => {
const { hits } = await prisma.hits.upsert({
where: { slug },
create: { slug },
update: {
hits: {
increment: 1,
},
},
});
// send client the *new* hit count
return { hits };
};
export default handler;

View File

@ -3,37 +3,21 @@ import { getAllNotes } from "../../lib/helpers/parse-notes";
import { logServerError } from "../../lib/helpers/sentry";
import { NOTES_DIR } from "../../lib/config/constants";
import type { NextApiRequest, NextApiResponse } from "next";
import type { PageStats, DetailedPageStats, SiteStats } from "../../types";
import type { DetailedPageStats, SiteStats } from "../../types";
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
try {
if (req.method !== "GET") {
// 405 Method Not Allowed
return res.status(405).end();
}
// return overall site stats if slug not specified
const result = await getSiteStats();
const { slug } = req.query;
let data;
if (slug) {
// add one to this page's count and return the new number
data = await incrementPageHits(slug as string);
// disable caching on both ends
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
} else {
// return overall site stats if slug not specified
data = await getSiteStats();
// let Vercel edge cache results for 15 mins
res.setHeader("Cache-Control", "public, max-age=0, s-maxage=900, stale-while-revalidate");
}
// let Vercel edge cache results for 15 mins
res.setHeader("Cache-Control", "public, max-age=0, s-maxage=900, stale-while-revalidate");
// send result as JSON
return res.status(200).json(data);
return res.status(200).json(result);
} catch (error) {
// extract just the error message to send back to client
const message = error instanceof Error ? error.message : "Unknown error.";
const message = error instanceof Error ? error.message : error;
// log full error to console and sentry
await logServerError(error);
@ -43,21 +27,6 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}
};
const incrementPageHits = async (slug: string): Promise<PageStats> => {
const { hits } = await prisma.hits.upsert({
where: { slug },
create: { slug },
update: {
hits: {
increment: 1,
},
},
});
// send client the *new* hit count
return { hits };
};
const getSiteStats = async (): Promise<SiteStats> => {
// simultaneously fetch the entire hits db and notes from the filesystem
const [hits, notes] = await Promise.all([

View File

@ -79,9 +79,7 @@ export const getStaticProps: GetStaticProps<{
console.warn(`ERROR: I can't fetch any GitHub projects without "GH_PUBLIC_TOKEN" set! Skipping for now...`);
return {
props: {
repos: [],
},
notFound: true,
};
}