From af707a8c790edc618e542fee6f7405a00c226775 Mon Sep 17 00:00:00 2001 From: Liam Date: Wed, 8 Nov 2023 22:24:38 +0000 Subject: [PATCH] fix(ssr): only use 1 network request to fetch map ids --- src/app/api/beatsaver/mapdata/route.ts | 9 +++--- src/utils/scoresaber/api.ts | 38 ++++++++++++++------------ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/app/api/beatsaver/mapdata/route.ts b/src/app/api/beatsaver/mapdata/route.ts index 2c9eb82..8e32e58 100644 --- a/src/app/api/beatsaver/mapdata/route.ts +++ b/src/app/api/beatsaver/mapdata/route.ts @@ -19,14 +19,15 @@ export async function GET(request: Request) { const map = await BeatsaverAPI.fetchMapByHash(mapHash); if (map) { maps[mapHash] = map; + mapCache.set(mapHash, map); } if (map && idOnly) { maps[mapHash] = { id: map.id }; } } - - return new Response(JSON.stringify(maps), { - headers: { "content-type": "application/json;charset=UTF-8" }, - }); } + + return new Response(JSON.stringify(maps), { + headers: { "content-type": "application/json;charset=UTF-8" }, + }); } diff --git a/src/utils/scoresaber/api.ts b/src/utils/scoresaber/api.ts index a6a2465..f3fb960 100644 --- a/src/utils/scoresaber/api.ts +++ b/src/utils/scoresaber/api.ts @@ -162,24 +162,28 @@ async function fetchScoresWithBeatsaverData( string, ScoresaberScoreWithBeatsaverData > = {}; + + let url = `${ + isProduction() ? ssrSettings.siteUrl : "http://localhost:3000" + }/api/beatsaver/mapdata?hashes=`; for (const score of scores) { - const mapResponse = await fetch( - `${ - isProduction() ? ssrSettings.siteUrl : "http://localhost:3000" - }/api/beatsaver/mapdata?hashes=${score.leaderboard.songHash}&idonly=true`, - { - next: { - revalidate: 60 * 60 * 24 * 7, // 1 week - }, - }, - ); - const mapData = await mapResponse.json(); - const mapId = mapData[score.leaderboard.songHash].id; - scoresWithBeatsaverData[score.score.id] = { - score: score.score, - leaderboard: score.leaderboard, - mapId: mapId, - }; + url += `${score.leaderboard.songHash},`; + } + const mapResponse = await fetch(url, { + next: { + revalidate: 60 * 60 * 24 * 7, // 1 week + }, + }); + const mapJson = await mapResponse.json(); + for (const score of scores) { + const mapData = mapJson[score.leaderboard.songHash]; + if (mapData) { + scoresWithBeatsaverData[score.leaderboard.songHash] = { + score: score.score, + leaderboard: score.leaderboard, + mapId: mapData.id, + }; + } } return {