Added caching
This commit is contained in:
9
src/app/(stats)/player/[ign]/_client.tsx
Normal file
9
src/app/(stats)/player/[ign]/_client.tsx
Normal 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>
|
||||
}
|
||||
@@ -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 (
|
||||
<Suspense
|
||||
fallback={
|
||||
<div className="flex flex-col justify-center items-center h-screen">
|
||||
<Loader2Icon className="animate-spin size-30" />
|
||||
<p>{`Loading stats for ${ign}`}</p>
|
||||
<PlayerPageLoadText />
|
||||
</div>
|
||||
}
|
||||
>
|
||||
<SuspendedPage ign={ign} />
|
||||
<SuspendedPage params={params} />
|
||||
</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)
|
||||
if (!mc) {
|
||||
return (
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user