add cache to the player data fetching on the player page to speedup initial page loads
Some checks failed
Deploy / deploy (push) Has been cancelled

This commit is contained in:
Lee 2024-09-30 11:27:33 +01:00
parent b5071fd420
commit 5f1ee985fb

@ -8,6 +8,7 @@ import { redirect } from "next/navigation";
import { Colors } from "@/common/colors"; import { Colors } from "@/common/colors";
import ScoreSaberPlayerScoresPageToken from "@/common/model/token/scoresaber/score-saber-player-scores-page-token"; import ScoreSaberPlayerScoresPageToken from "@/common/model/token/scoresaber/score-saber-player-scores-page-token";
import { getAverageColor } from "@/common/image-utils"; import { getAverageColor } from "@/common/image-utils";
import { cache } from "react";
const UNKNOWN_PLAYER = { const UNKNOWN_PLAYER = {
title: "ScoreSaber Reloaded - Unknown Player", title: "ScoreSaber Reloaded - Unknown Player",
@ -30,32 +31,34 @@ type Props = {
* @param fetchScores whether to fetch the scores * @param fetchScores whether to fetch the scores
* @returns the player data and scores * @returns the player data and scores
*/ */
async function getPlayerData({ params }: Props, fetchScores: boolean = true) { const getPlayerData = cache(
const { slug } = await params; async ({ params }: Props, fetchScores: boolean = true) => {
const id = slug[0]; // The players id const { slug } = await params;
const sort: ScoreSort = (slug[1] as ScoreSort) || "recent"; // The sorting method const id = slug[0]; // The players id
const page = parseInt(slug[2]) || 1; // The page number const sort: ScoreSort = (slug[1] as ScoreSort) || "recent"; // The sorting method
const search = (slug[3] as string) || ""; // The search query const page = parseInt(slug[2]) || 1; // The page number
const search = (slug[3] as string) || ""; // The search query
const player = (await scoresaberService.lookupPlayer(id, false))?.player; const player = (await scoresaberService.lookupPlayer(id, false))?.player;
let scores: ScoreSaberPlayerScoresPageToken | undefined; let scores: ScoreSaberPlayerScoresPageToken | undefined;
if (fetchScores) { if (fetchScores) {
scores = await scoresaberService.lookupPlayerScores({ scores = await scoresaberService.lookupPlayerScores({
playerId: id, playerId: id,
sort, sort,
page, page,
search, search,
}); });
} }
return { return {
sort: sort, sort: sort,
page: page, page: page,
search: search, search: search,
player: player, player: player,
scores: scores, scores: scores,
}; };
} },
);
export async function generateMetadata(props: Props): Promise<Metadata> { export async function generateMetadata(props: Props): Promise<Metadata> {
const { player } = await getPlayerData(props, false); const { player } = await getPlayerData(props, false);