perf(home): parallelize private visitor tracking
This commit is contained in:
+11
-27
@@ -3,39 +3,23 @@ import { Card, CardHeader } from "@/components/ui/card";
|
||||
import Image from "next/image";
|
||||
import { getFollowerCounts } from "@/lib/followers";
|
||||
import { LinkCard } from "@/components/LinkCard";
|
||||
import { getRedisClient } from "@/lib/redis";
|
||||
import { headers } from "next/headers";
|
||||
import { connection } from "next/server";
|
||||
import { Users } from "lucide-react";
|
||||
import { Kbd } from "@/components/ui/kbd";
|
||||
import Background from "@/public/background/space.webp";
|
||||
import { trackUniqueVisitor } from "@/lib/visitor/unique-visitors";
|
||||
|
||||
export default async function Home() {
|
||||
await connection();
|
||||
|
||||
const { counts } = await getFollowerCounts();
|
||||
|
||||
const headersList = await headers();
|
||||
const ip = headersList.get("x-forwarded-for") || headersList.get("x-real-ip") || "unknown";
|
||||
|
||||
let visitorCount = 0;
|
||||
try {
|
||||
const redis = await getRedisClient();
|
||||
if (ip !== "unknown") {
|
||||
const added = await redis.sadd("erika:unique_visitors", ip);
|
||||
if (added > 0) {
|
||||
// New visitor — refresh the count
|
||||
visitorCount = await redis.scard("erika:unique_visitors");
|
||||
} else {
|
||||
// Existing visitor — use cached count or fetch
|
||||
visitorCount = await redis.scard("erika:unique_visitors");
|
||||
}
|
||||
} else {
|
||||
visitorCount = await redis.scard("erika:unique_visitors");
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to track visitor:", e);
|
||||
}
|
||||
const visitorCountPromise = headers()
|
||||
.then(trackUniqueVisitor)
|
||||
.catch((error) => {
|
||||
console.error("Failed to track visitor:", error);
|
||||
return 0;
|
||||
});
|
||||
const [{ counts }, visitorCount] = await Promise.all([
|
||||
getFollowerCounts(),
|
||||
visitorCountPromise,
|
||||
]);
|
||||
|
||||
// Use configured profile data.
|
||||
const profileName = config.profile.name;
|
||||
|
||||
Reference in New Issue
Block a user