⚡ Bolt: Memoize auth checks in Map API (#236)
Introduced a request-scoped `Map` (`authCache`) in `app/api/map/route.ts` to memoize `isAuthenticated` calls. The `getMapData` loop iterates over all albums across all locations. Albums within the same subpage slug trigger repeated `isAuthenticated` checks, which execute expensive HMAC cryptographic operations, causing performance degradation for large datasets. Eliminates redundant cryptographic operations during map data fetching, reducing CPU utilization and response times for the Map API. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
@@ -44,12 +44,16 @@ export async function GET(request: NextRequest) {
|
||||
const locations = await getMapData();
|
||||
|
||||
// Filter locations and albums based on auth
|
||||
const authCache = new Map<string, boolean>();
|
||||
const publicLocations = locations
|
||||
.map((loc) => {
|
||||
// Only keep albums the user is allowed to see
|
||||
const allowedAlbums = loc.albums.filter((a) => {
|
||||
if (!a.subpageSlug) return true; // Standalone albums are public
|
||||
return isAuthenticated(a.subpageSlug, getCookie);
|
||||
if (authCache.has(a.subpageSlug)) return authCache.get(a.subpageSlug)!;
|
||||
const result = isAuthenticated(a.subpageSlug, getCookie);
|
||||
authCache.set(a.subpageSlug, result);
|
||||
return result;
|
||||
});
|
||||
|
||||
if (allowedAlbums.length === 0) return null;
|
||||
|
||||
Reference in New Issue
Block a user