mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-10-28 15:05:47 -04:00
optimize /api/hits/ a smidge
This commit is contained in:
@@ -25,7 +25,7 @@
|
|||||||
"@hcaptcha/react-hcaptcha": "^1.3.1",
|
"@hcaptcha/react-hcaptcha": "^1.3.1",
|
||||||
"@novnc/novnc": "github:novnc/novnc#cdfb33665195eb9a73fb00feb6ebaccd1068cd50",
|
"@novnc/novnc": "github:novnc/novnc#cdfb33665195eb9a73fb00feb6ebaccd1068cd50",
|
||||||
"@octokit/graphql": "^4.8.0",
|
"@octokit/graphql": "^4.8.0",
|
||||||
"@octokit/graphql-schema": "^10.74.0",
|
"@octokit/graphql-schema": "^10.74.1",
|
||||||
"@primer/octicons": "^17.3.0",
|
"@primer/octicons": "^17.3.0",
|
||||||
"@prisma/client": "^3.15.2",
|
"@prisma/client": "^3.15.2",
|
||||||
"@react-spring/web": "^9.4.5",
|
"@react-spring/web": "^9.4.5",
|
||||||
@@ -87,13 +87,13 @@
|
|||||||
"@types/react-is": "^17.0.3",
|
"@types/react-is": "^17.0.3",
|
||||||
"@types/remove-markdown": "^0.3.1",
|
"@types/remove-markdown": "^0.3.1",
|
||||||
"@types/uglify-js": "^3.16.0",
|
"@types/uglify-js": "^3.16.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^5.29.0",
|
"@typescript-eslint/eslint-plugin": "^5.30.0",
|
||||||
"@typescript-eslint/parser": "^5.29.0",
|
"@typescript-eslint/parser": "^5.30.0",
|
||||||
"cross-env": "^7.0.3",
|
"cross-env": "^7.0.3",
|
||||||
"eslint": "~8.18.0",
|
"eslint": "~8.18.0",
|
||||||
"eslint-config-next": "12.1.7-canary.46",
|
"eslint-config-next": "12.1.7-canary.46",
|
||||||
"eslint-config-prettier": "~8.5.0",
|
"eslint-config-prettier": "~8.5.0",
|
||||||
"eslint-plugin-prettier": "~4.0.0",
|
"eslint-plugin-prettier": "~4.1.0",
|
||||||
"lint-staged": "^13.0.3",
|
"lint-staged": "^13.0.3",
|
||||||
"prettier": "^2.7.1",
|
"prettier": "^2.7.1",
|
||||||
"prisma": "^3.15.2",
|
"prisma": "^3.15.2",
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { getAllNotes } from "../../lib/helpers/parse-notes";
|
|
||||||
import { prisma } from "../../lib/helpers/prisma";
|
import { prisma } from "../../lib/helpers/prisma";
|
||||||
|
import { getAllNotes } from "../../lib/helpers/parse-notes";
|
||||||
import { logServerError } from "../../lib/helpers/sentry";
|
import { logServerError } from "../../lib/helpers/sentry";
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
|
||||||
@@ -26,26 +26,24 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { slug } = req.query;
|
const { slug } = req.query;
|
||||||
|
let data;
|
||||||
|
|
||||||
if (slug) {
|
if (slug) {
|
||||||
const hits = await incrementPageHits(slug as string);
|
// add one to this page's count and return the new number
|
||||||
|
data = await incrementPageHits(slug as string);
|
||||||
|
|
||||||
// disable caching on both ends
|
// disable caching on both ends
|
||||||
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
|
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
|
||||||
res.setHeader("Pragma", "no-cache");
|
|
||||||
|
|
||||||
// return in JSON format
|
|
||||||
return res.status(200).json({ hits });
|
|
||||||
} else {
|
} else {
|
||||||
// return overall site stats if slug not specified
|
// return overall site stats if slug not specified
|
||||||
const siteStats = await getSiteStats();
|
data = await getSiteStats();
|
||||||
|
|
||||||
// let Vercel edge cache results for 15 mins
|
// let Vercel edge cache results for 15 mins
|
||||||
res.setHeader("Cache-Control", "public, max-age=0, s-maxage=900, stale-while-revalidate");
|
res.setHeader("Cache-Control", "public, max-age=0, s-maxage=900, stale-while-revalidate");
|
||||||
|
|
||||||
// return in JSON format
|
|
||||||
return res.status(200).json(siteStats);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// send result as JSON
|
||||||
|
return res.status(200).json(data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// extract just the error message to send back to client
|
// 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 : "Unknown error.";
|
||||||
@@ -58,14 +56,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const incrementPageHits = async (slug: string): Promise<number> => {
|
const incrementPageHits = async (slug: string): Promise<Partial<PageStats>> => {
|
||||||
const pageHits = await prisma.hits.upsert({
|
const { hits } = await prisma.hits.upsert({
|
||||||
where: {
|
where: { slug },
|
||||||
slug,
|
create: { slug },
|
||||||
},
|
|
||||||
create: {
|
|
||||||
slug,
|
|
||||||
},
|
|
||||||
update: {
|
update: {
|
||||||
hits: {
|
hits: {
|
||||||
increment: 1,
|
increment: 1,
|
||||||
@@ -74,27 +68,27 @@ const incrementPageHits = async (slug: string): Promise<number> => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// send client the *new* hit count
|
// send client the *new* hit count
|
||||||
return pageHits.hits;
|
return { hits };
|
||||||
};
|
};
|
||||||
|
|
||||||
const getSiteStats = async (): Promise<SiteStats> => {
|
const getSiteStats = async (): Promise<SiteStats> => {
|
||||||
const notes = await getAllNotes();
|
const [pages, notes] = await Promise.all([
|
||||||
const pages: SiteStats["pages"] = await prisma.hits.findMany({
|
prisma.hits.findMany({
|
||||||
orderBy: [
|
orderBy: [
|
||||||
{
|
{
|
||||||
hits: "desc",
|
hits: "desc",
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
}),
|
||||||
|
getAllNotes(),
|
||||||
|
]);
|
||||||
|
|
||||||
const siteStats: SiteStats = {
|
const total = { hits: 0 };
|
||||||
total: { hits: 0 },
|
|
||||||
pages,
|
|
||||||
};
|
|
||||||
|
|
||||||
pages.forEach((page) => {
|
pages.forEach((page: PageStats) => {
|
||||||
// match URLs from RSS feed with db to populate some metadata
|
// match URLs from RSS feed with db to populate some metadata
|
||||||
const match = notes.find((note) => `notes/${note.slug}` === page.slug);
|
const match = notes.find((note) => `notes/${note.slug}` === page.slug);
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
page.title = match.title;
|
page.title = match.title;
|
||||||
page.url = match.permalink;
|
page.url = match.permalink;
|
||||||
@@ -102,12 +96,12 @@ const getSiteStats = async (): Promise<SiteStats> => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add these hits to running tally
|
// add these hits to running tally
|
||||||
siteStats.total.hits += page.hits;
|
total.hits += page.hits;
|
||||||
|
|
||||||
return page;
|
return page;
|
||||||
});
|
});
|
||||||
|
|
||||||
return siteStats;
|
return { total, pages };
|
||||||
};
|
};
|
||||||
|
|
||||||
export default handler;
|
export default handler;
|
||||||
|
|||||||
Reference in New Issue
Block a user