From a926b74d6537eb8c9ceeb578134115fc0f2a8da0 Mon Sep 17 00:00:00 2001 From: Taken Date: Mon, 8 Sep 2025 19:49:05 +0200 Subject: [PATCH] Added caching --- next.config.ts | 1 + src/app/(stats)/player/[ign]/_client.tsx | 9 +++++++++ src/app/(stats)/player/[ign]/page.tsx | 13 +++++++------ src/lib/hypixel/api/guild.ts | 14 +++++++++++++- src/lib/hypixel/api/mojang.ts | 12 +++++++++++- src/lib/hypixel/api/player.ts | 11 +++++++++++ 6 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 src/app/(stats)/player/[ign]/_client.tsx diff --git a/next.config.ts b/next.config.ts index d80b7a8..ab1939e 100644 --- a/next.config.ts +++ b/next.config.ts @@ -6,6 +6,7 @@ const nextConfig: NextConfig = { ignoreDuringBuilds: true }, experimental: { + useCache: true, reactCompiler: { compilationMode: "all" }, diff --git a/src/app/(stats)/player/[ign]/_client.tsx b/src/app/(stats)/player/[ign]/_client.tsx new file mode 100644 index 0000000..2ecef9b --- /dev/null +++ b/src/app/(stats)/player/[ign]/_client.tsx @@ -0,0 +1,9 @@ +"use client" + +import { usePathname } from "next/navigation" + +export function PlayerPageLoadText() { + const path = usePathname() + + return

{`Loading stats for ${path.split("/").at(-1)}`}

+} diff --git a/src/app/(stats)/player/[ign]/page.tsx b/src/app/(stats)/player/[ign]/page.tsx index 33aa44d..159bd40 100644 --- a/src/app/(stats)/player/[ign]/page.tsx +++ b/src/app/(stats)/player/[ign]/page.tsx @@ -9,6 +9,7 @@ import { getExactLevel } from "@/lib/hypixel/general/level" import { Loader2Icon, ShieldAlert } from "lucide-react" import { Metadata } from "next" import { Suspense } from "react" +import { PlayerPageLoadText } from "./_client" import Sidebar from "./_components/Sidebar" import BedwarsStats from "./_stats/bedwars/bedwars" import BuildBattleStats from "./_stats/build-battle/build-battle" @@ -27,7 +28,7 @@ export async function generateMetadata({ params }: { params: Promise<{ ign: stri return { title: user !== null ? user.name : "Player not found" } } -export default async function PlayerPage({ params }: PageProps<"/player/[ign]">) { +export default function PlayerPage({ params }: PageProps<"/player/[ign]">) { const maintenance = env.MAINTENANCE_MODE if (maintenance) { @@ -39,23 +40,23 @@ export default async function PlayerPage({ params }: PageProps<"/player/[ign]">) ) } - const { ign } = await params - return ( -

{`Loading stats for ${ign}`}

+ } > - +
) } -async function SuspendedPage({ ign: pign }: { ign: string }) { +async function SuspendedPage({ params }: Pick, "params">) { + const { ign: pign } = await params + const mc = await getUuid(pign) if (!mc) { return ( diff --git a/src/lib/hypixel/api/guild.ts b/src/lib/hypixel/api/guild.ts index aa92caa..effe2e3 100644 --- a/src/lib/hypixel/api/guild.ts +++ b/src/lib/hypixel/api/guild.ts @@ -1,9 +1,20 @@ +import { cacheLife } from "next/dist/server/use-cache/cache-life" import { env } from "../../env/server" import { guildSchema } from "../../schema/guild" const guildApi = "https://api.hypixel.net/v2/guild" export async function getGuild(id: string, type: "id" | "player" | "name" = "player") { + "use cache" + + if (process.env.NODE_ENV === "production") { + cacheLife({ + stale: 1000 * 60, + revalidate: 1000 * 60 * 5, + expire: 1000 * 60 * 5 + }) + } + const res = await fetch(`${guildApi}?${type}=${id}`, { headers: { "API-Key": env.HYPIXEL_API_KEY @@ -17,4 +28,5 @@ export async function getGuild(id: string, type: "id" | "player" | "name" = "pla if (!success) return null return data.guild -} \ No newline at end of file +} + diff --git a/src/lib/hypixel/api/mojang.ts b/src/lib/hypixel/api/mojang.ts index dbcc56e..775e0f5 100644 --- a/src/lib/hypixel/api/mojang.ts +++ b/src/lib/hypixel/api/mojang.ts @@ -1,3 +1,4 @@ +import { cacheLife } from "next/dist/server/use-cache/cache-life" import z from "zod" const mojangApi = "https://api.mojang.com/users/profiles/minecraft" @@ -8,6 +9,16 @@ const schema = z.object({ }) export async function getUuid(ign: string) { + "use cache" + + if (process.env.NODE_ENV === "production") { + cacheLife({ + stale: 1000 * 60, + revalidate: 1000 * 60 * 5, + expire: 1000 * 60 * 5 + }) + } + const res = await fetch(`${mojangApi}/${ign}`) if (!res.ok) return null @@ -20,4 +31,3 @@ export async function getUuid(ign: string) { return parsed.data } - diff --git a/src/lib/hypixel/api/player.ts b/src/lib/hypixel/api/player.ts index 380dd29..1b9c0fc 100644 --- a/src/lib/hypixel/api/player.ts +++ b/src/lib/hypixel/api/player.ts @@ -1,9 +1,20 @@ +import { cacheLife } from "next/dist/server/use-cache/cache-life" import { env } from "../../env/server" import { playerSchema } from "../../schema/player" const playerApi = "https://api.hypixel.net/v2/player" export async function getPlayer(uuid: string) { + "use cache" + + if (process.env.NODE_ENV === "production") { + cacheLife({ + stale: 1000 * 60, + revalidate: 1000 * 60 * 5, + expire: 1000 * 60 * 5 + }) + } + const res = await fetch(`${playerApi}?uuid=${uuid}`, { headers: { "API-Key": env.HYPIXEL_API_KEY