Added caching

This commit is contained in:
2025-09-08 19:49:05 +02:00
parent 5781880be4
commit a926b74d65
6 changed files with 52 additions and 8 deletions

View File

@@ -6,6 +6,7 @@ const nextConfig: NextConfig = {
ignoreDuringBuilds: true ignoreDuringBuilds: true
}, },
experimental: { experimental: {
useCache: true,
reactCompiler: { reactCompiler: {
compilationMode: "all" compilationMode: "all"
}, },

View File

@@ -0,0 +1,9 @@
"use client"
import { usePathname } from "next/navigation"
export function PlayerPageLoadText() {
const path = usePathname()
return <p>{`Loading stats for ${path.split("/").at(-1)}`}</p>
}

View File

@@ -9,6 +9,7 @@ import { getExactLevel } from "@/lib/hypixel/general/level"
import { Loader2Icon, ShieldAlert } from "lucide-react" import { Loader2Icon, ShieldAlert } from "lucide-react"
import { Metadata } from "next" import { Metadata } from "next"
import { Suspense } from "react" import { Suspense } from "react"
import { PlayerPageLoadText } from "./_client"
import Sidebar from "./_components/Sidebar" import Sidebar from "./_components/Sidebar"
import BedwarsStats from "./_stats/bedwars/bedwars" import BedwarsStats from "./_stats/bedwars/bedwars"
import BuildBattleStats from "./_stats/build-battle/build-battle" 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" } 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 const maintenance = env.MAINTENANCE_MODE
if (maintenance) { if (maintenance) {
@@ -39,23 +40,23 @@ export default async function PlayerPage({ params }: PageProps<"/player/[ign]">)
) )
} }
const { ign } = await params
return ( return (
<Suspense <Suspense
fallback={ fallback={
<div className="flex flex-col justify-center items-center h-screen"> <div className="flex flex-col justify-center items-center h-screen">
<Loader2Icon className="animate-spin size-30" /> <Loader2Icon className="animate-spin size-30" />
<p>{`Loading stats for ${ign}`}</p> <PlayerPageLoadText />
</div> </div>
} }
> >
<SuspendedPage ign={ign} /> <SuspendedPage params={params} />
</Suspense> </Suspense>
) )
} }
async function SuspendedPage({ ign: pign }: { ign: string }) { async function SuspendedPage({ params }: Pick<PageProps<"/player/[ign]">, "params">) {
const { ign: pign } = await params
const mc = await getUuid(pign) const mc = await getUuid(pign)
if (!mc) { if (!mc) {
return ( return (

View File

@@ -1,9 +1,20 @@
import { cacheLife } from "next/dist/server/use-cache/cache-life"
import { env } from "../../env/server" import { env } from "../../env/server"
import { guildSchema } from "../../schema/guild" import { guildSchema } from "../../schema/guild"
const guildApi = "https://api.hypixel.net/v2/guild" const guildApi = "https://api.hypixel.net/v2/guild"
export async function getGuild(id: string, type: "id" | "player" | "name" = "player") { 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}`, { const res = await fetch(`${guildApi}?${type}=${id}`, {
headers: { headers: {
"API-Key": env.HYPIXEL_API_KEY "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 if (!success) return null
return data.guild return data.guild
} }

View File

@@ -1,3 +1,4 @@
import { cacheLife } from "next/dist/server/use-cache/cache-life"
import z from "zod" import z from "zod"
const mojangApi = "https://api.mojang.com/users/profiles/minecraft" const mojangApi = "https://api.mojang.com/users/profiles/minecraft"
@@ -8,6 +9,16 @@ const schema = z.object({
}) })
export async function getUuid(ign: string) { 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}`) const res = await fetch(`${mojangApi}/${ign}`)
if (!res.ok) return null if (!res.ok) return null
@@ -20,4 +31,3 @@ export async function getUuid(ign: string) {
return parsed.data return parsed.data
} }

View File

@@ -1,9 +1,20 @@
import { cacheLife } from "next/dist/server/use-cache/cache-life"
import { env } from "../../env/server" import { env } from "../../env/server"
import { playerSchema } from "../../schema/player" import { playerSchema } from "../../schema/player"
const playerApi = "https://api.hypixel.net/v2/player" const playerApi = "https://api.hypixel.net/v2/player"
export async function getPlayer(uuid: string) { 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}`, { const res = await fetch(`${playerApi}?uuid=${uuid}`, {
headers: { headers: {
"API-Key": env.HYPIXEL_API_KEY "API-Key": env.HYPIXEL_API_KEY