Skip to content

Commit

Permalink
Revert "Use next/after to increment views"
Browse files Browse the repository at this point in the history
This reverts commit 6561fd1.
  • Loading branch information
akhila-ariyachandra committed Jun 15, 2024
1 parent cccf7ce commit 6a27a92
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 82 deletions.
81 changes: 0 additions & 81 deletions app/_components/views.tsx

This file was deleted.

55 changes: 55 additions & 0 deletions app/_components/views/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use server";

import { db } from "@/_db/connection";
import { post } from "@/_db/schema";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { eq } from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { headers } from "next/headers";

const ratelimit = new Ratelimit({
redis: Redis.fromEnv(),
limiter: Ratelimit.slidingWindow(1, "60 s"),
analytics: true,
});

export const incrementViews = async (slug: string) => {
let ip = "";

const FALLBACK_IP_ADDRESS = "0.0.0.0";
const forwardedFor = headers().get("x-forwarded-for");

if (forwardedFor) {
ip = forwardedFor.split(",")[0] ?? FALLBACK_IP_ADDRESS;
} else {
ip = headers().get("x-real-ip") ?? FALLBACK_IP_ADDRESS;
}

const { success } = await ratelimit.limit(`${ip}-${slug}`);

if (!success) {
return false;
}

// Check if row exists
const results = await db.select().from(post).where(eq(post.slug, slug));
const result = results[0];
if (!result) {
// Create the record
await db.insert(post).values({
slug,
views: 1,
});
} else {
// Update the record
await db
.update(post)
.set({ views: result.views + 1 })
.where(eq(post.slug, slug));
}

revalidatePath("/blog", "layout");

return true;
};
1 change: 1 addition & 0 deletions app/_components/views/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "./views";
24 changes: 24 additions & 0 deletions app/_components/views/views-incrementer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use client";

import { useEffect } from "react";
import { incrementViews } from "./actions";

type ViewsIncrementerProps = {
slug: string;
incrementOnMount: boolean;
};

const ViewsIncrementer = ({
slug,
incrementOnMount,
}: ViewsIncrementerProps) => {
useEffect(() => {
if (incrementOnMount) {
incrementViews(slug);
}
}, [slug, incrementOnMount]);

return null;
};

export default ViewsIncrementer;
35 changes: 35 additions & 0 deletions app/_components/views/views.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { db } from "@/_db/connection";
import { post } from "@/_db/schema";
import { eq } from "drizzle-orm";
import { Suspense } from "react";
import ViewsIncrementer from "./views-incrementer";

type ViewsProps = {
slug: string;
};

const Views = async ({ slug }: ViewsProps) => {
const results = await db.select().from(post).where(eq(post.slug, slug));
const views = results[0]?.views ?? 0;

return <span>{views} views</span>;
};

type ViewsRootProps = {
slug: string;
incrementOnMount?: boolean;
};

const ViewsRoot = ({ slug, incrementOnMount = false }: ViewsRootProps) => {
return (
<>
<Suspense fallback={<span className="invisible">0 views</span>}>
<Views slug={slug} />
</Suspense>

<ViewsIncrementer slug={slug} incrementOnMount={incrementOnMount} />
</>
);
};

export default ViewsRoot;
1 change: 0 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const nextConfig = {
experimental: {
reactCompiler: true,
ppr: true,
after: true,
},
reactStrictMode: true,
images: {
Expand Down

0 comments on commit 6a27a92

Please sign in to comment.