1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-06-27 17:25:43 -04:00

save a redis query when fetching all view counts

This commit is contained in:
2025-05-06 20:24:55 -04:00
parent a08ec532b3
commit 0df56d127a
6 changed files with 40 additions and 32 deletions

View File

@ -104,7 +104,7 @@ const Page = () => {
title="Jake's Bulletin Board, circa 2003"
className="[--primary:#9932cc] dark:[--primary:#d588fb]"
>
backend programming
backend coding
</Link>{" "}
when my only source of income was{" "}
<Link
@ -117,7 +117,7 @@ const Page = () => {
>
the Tooth Fairy
</Link>
. <span className="text-muted-foreground">I&rsquo;ve improved a bit since then, I think? 🤷</span>
. <span className="text-muted-foreground">I&rsquo;ve improved a bit since then, I think?</span>
</p>
<p className="my-3 text-base leading-relaxed md:text-[0.975rem]">

View File

@ -12,14 +12,17 @@ const Menu = ({ className, ...rest }: ComponentPropsWithoutRef<"ul">) => {
return (
<ul
className={cn("flex max-w-2/3 flex-row justify-between md:max-w-none md:justify-end md:space-x-4", className)}
className={cn(
"flex max-w-2/3 flex-row justify-between md:max-w-none md:justify-end md:space-x-4 max-sm:[&>li]:first-of-type:hidden",
className
)}
{...rest}
>
{menuItems.map((item) => {
const isCurrent = item.href?.split("/")[1] === segment;
return (
<li className="inline-block max-sm:first-of-type:hidden" key={item.href}>
<li className="inline-block" key={item.href}>
<MenuItem {...item} current={isCurrent} />
</li>
);

View File

@ -1,4 +1,4 @@
import { CodeIcon, HomeIcon, MailIcon, PencilLineIcon, type LucideIcon } from "lucide-react";
import { HomeIcon, PencilLineIcon, CodeXmlIcon, MailIcon, type LucideIcon } from "lucide-react";
export type MenuItemConfig = {
text?: string;
@ -20,7 +20,7 @@ export const menuItems: MenuItemConfig[] = [
{
text: "Projects",
href: "/projects",
icon: CodeIcon,
icon: CodeXmlIcon,
},
{
text: "Contact",

View File

@ -128,32 +128,37 @@ export const getViews: {
): // eslint-disable-next-line @typescript-eslint/no-explicit-any
Promise<any> => {
if (typeof slug === "string") {
const views = await kv.get<string>(`hits:${slug}`);
try {
const views = await kv.get<string>(`hits:${POSTS_DIR}/${slug}`);
return views ? parseInt(views, 10) : undefined;
return views ? parseInt(views, 10) : undefined;
} catch (error) {
console.error(`Failed to retrieve view count for post with slug "${slug}":`, error);
return undefined;
}
}
if (!slug) {
const slugs = await kv.scan(0, {
match: "hits:*",
type: "string",
// set an arbitrary yet generous upper limit, just in case...
count: 99,
});
try {
const allSlugs = await getSlugs();
const pages: Record<string, number> = {};
// get the value (number of hits) for each key (the slug of the page)
const values = await kv.mget<string[]>(...slugs[1]);
// get the value (number of views) for each key (the slug of the page)
const values = await kv.mget<string[]>(...allSlugs.map((slug) => `hits:${POSTS_DIR}/${slug}`));
const pages: Record<string, number> = {};
// pair the slugs with their view counts
allSlugs.forEach(
(slug, index) => (pages[slug.replace(`hits:${POSTS_DIR}/`, "")] = parseInt(values[index], 10))
);
// pair the slugs with their hit values
slugs[1].forEach(
(slug, index) =>
(pages[slug.split(":").pop()?.replace(`${POSTS_DIR}/`, "") as string] = parseInt(values[index], 10))
);
return pages;
return pages;
} catch (error) {
console.error("Failed to retrieve view counts:", error);
return undefined;
}
}
throw new Error("getViews() called with invalid argument.");
}
);

View File

@ -86,7 +86,7 @@
"@jakejarvis/eslint-config": "^4.0.7",
"@tailwindcss/postcss": "^4.1.5",
"@types/mdx": "^2.0.13",
"@types/node": "^22.15.12",
"@types/node": "^22.15.14",
"@types/prop-types": "^15.7.14",
"@types/react": "^19.1.3",
"@types/react-dom": "^19.1.3",

14
pnpm-lock.yaml generated
View File

@ -208,8 +208,8 @@ importers:
specifier: ^2.0.13
version: 2.0.13
'@types/node':
specifier: ^22.15.12
version: 22.15.12
specifier: ^22.15.14
version: 22.15.14
'@types/prop-types':
specifier: ^15.7.14
version: 15.7.14
@ -1242,8 +1242,8 @@ packages:
'@types/nlcst@2.0.3':
resolution: {integrity: sha512-vSYNSDe6Ix3q+6Z7ri9lyWqgGhJTmzRjZRqyq15N0Z/1/UnVsno9G/N40NBijoYx2seFDIl0+B2mgAb9mezUCA==}
'@types/node@22.15.12':
resolution: {integrity: sha512-K0fpC/ZVeb8G9rm7bH7vI0KAec4XHEhBam616nVJCV51bKzJ6oA3luG4WdKoaztxe70QaNjS/xBmcDLmr4PiGw==}
'@types/node@22.15.14':
resolution: {integrity: sha512-BL1eyu/XWsFGTtDWOYULQEs4KR0qdtYfCxYAUYRoB7JP7h9ETYLgQTww6kH8Sj2C0pFGgrpM0XKv6/kbIzYJ1g==}
'@types/prop-types@15.7.14':
resolution: {integrity: sha512-gNMvNH49DJ7OJYv+KAKn0Xp45p8PLl6zo2YnvDIbTd4J6MER2BmWN49TG7n9LvkyihINxeKW8+3bfS2yDC9dzQ==}
@ -5221,7 +5221,7 @@ snapshots:
'@types/concat-stream@2.0.3':
dependencies:
'@types/node': 22.15.12
'@types/node': 22.15.14
'@types/debug@4.1.12':
dependencies:
@ -5257,7 +5257,7 @@ snapshots:
dependencies:
'@types/unist': 3.0.3
'@types/node@22.15.12':
'@types/node@22.15.14':
dependencies:
undici-types: 6.21.0
@ -8799,7 +8799,7 @@ snapshots:
'@types/concat-stream': 2.0.3
'@types/debug': 4.1.12
'@types/is-empty': 1.2.3
'@types/node': 22.15.12
'@types/node': 22.15.14
'@types/unist': 3.0.3
concat-stream: 2.0.0
debug: 4.4.0