Skip to content

Commit

Permalink
feat: Update ViewCounter component to use Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Oct 31, 2024
1 parent 30f618a commit f8419e8
Showing 1 changed file with 8 additions and 14 deletions.
22 changes: 8 additions & 14 deletions components/ViewCounter.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
"use client";
import { useEffect } from "react";
import useSWR from "swr";
import type { Views } from "@prisma/client";
import { Redis } from "@upstash/redis";

interface ViewCounterProps {
interface Props {
slug: string;
}

const fetcher = (...args: [RequestInfo, RequestInit?]) =>
fetch(...args).then((res) => res.json());
const redis = Redis.fromEnv();

const ViewCounter = ({ slug }: ViewCounterProps) => {
const { data } = useSWR<Views>(`/api/views/${slug}`, fetcher);
const view = async (slug: string) => await redis.incr(`pageviews:${slug}`);

useEffect(() => {
fetch(`/api/views/${slug}`, { method: "POST" });
}, [slug]);
const ViewCounter = async ({ slug }: Props) => {
await view(slug);

const viewCount = data?.count || 0;
const views = (await redis.get<number>(`pageviews:${slug}`)) || 0;

return <div>{viewCount} views</div>;
return <p className="text-sm text-neutral-400">{views} views</p>;
};

export default ViewCounter;

0 comments on commit f8419e8

Please sign in to comment.