-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cd983bc
commit 0ab4f7a
Showing
7 changed files
with
79 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
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 { headers } from "next/headers"; | ||
import { unstable_after as after } from "next/server"; | ||
import { Suspense } from "react"; | ||
|
||
type ViewsProps = { | ||
slug: string; | ||
increment?: boolean; | ||
}; | ||
|
||
const Views = ({ slug, increment = false }: ViewsProps) => { | ||
return ( | ||
<Suspense fallback={<span className="invisible">0 views</span>}> | ||
<ViewsBase slug={slug} increment={increment} /> | ||
</Suspense> | ||
); | ||
}; | ||
|
||
export default Views; | ||
|
||
const ratelimit = new Ratelimit({ | ||
redis: Redis.fromEnv(), | ||
limiter: Ratelimit.slidingWindow(1, "60 s"), | ||
analytics: true, | ||
}); | ||
|
||
const ViewsBase = async ({ slug, increment }: ViewsProps) => { | ||
const headersStore = await headers(); | ||
|
||
const results = await db.select().from(post).where(eq(post.slug, slug)); | ||
const views = results[0]?.views ?? 0; | ||
|
||
if (increment) { | ||
after(async () => { | ||
let ip = ""; | ||
|
||
const FALLBACK_IP_ADDRESS = "0.0.0.0"; | ||
const forwardedFor = headersStore.get("x-forwarded-for"); | ||
|
||
if (forwardedFor) { | ||
ip = forwardedFor.split(",")[0] ?? FALLBACK_IP_ADDRESS; | ||
} else { | ||
ip = headersStore.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)); | ||
} | ||
}); | ||
} | ||
|
||
return <span>{views} views</span>; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters