From 0ccc9bb4cb3aa2fb9be70d29f74de7e0c928a20c Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Mon, 4 Mar 2024 11:17:44 -0600 Subject: [PATCH] Wrap more requests in React cache --- src/app/tag/[tag]/[photoId]/layout.tsx | 8 +++++--- src/photo/cache.ts | 1 + src/tag/data.ts | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/app/tag/[tag]/[photoId]/layout.tsx b/src/app/tag/[tag]/[photoId]/layout.tsx index eb834147..93709098 100644 --- a/src/app/tag/[tag]/[photoId]/layout.tsx +++ b/src/app/tag/[tag]/[photoId]/layout.tsx @@ -12,7 +12,9 @@ import { import PhotoDetailPage from '@/photo/PhotoDetailPage'; import { getPhotoCached } from '@/photo/cache'; import { getPhotosTagDataCached } from '@/tag/data'; -import { ReactNode } from 'react'; +import { ReactNode, cache } from 'react'; + +const getPhotoCachedCached = cache(getPhotoCached); interface PhotoTagProps { params: { photoId: string, tag: string } @@ -21,7 +23,7 @@ interface PhotoTagProps { export async function generateMetadata({ params: { photoId, tag }, }: PhotoTagProps): Promise { - const photo = await getPhotoCached(photoId); + const photo = await getPhotoCachedCached(photoId); if (!photo) { return {}; } @@ -52,7 +54,7 @@ export default async function PhotoTagPage({ params: { photoId, tag }, children, }: PhotoTagProps & { children: ReactNode }) { - const photo = await getPhotoCached(photoId); + const photo = await getPhotoCachedCached(photoId); if (!photo) { redirect(PATH_ROOT); } diff --git a/src/photo/cache.ts b/src/photo/cache.ts index fc21fbd5..b9c25491 100644 --- a/src/photo/cache.ts +++ b/src/photo/cache.ts @@ -134,6 +134,7 @@ export const getPhotosCached = ( getPhotos, [KEY_PHOTOS, ...getPhotosCacheKeys(...args)], )(...args).then(parseCachedPhotosDates); +export const getPhotosCachedCached = cache(getPhotosCached); const getPhotosNearIdCached = ( ...args: Parameters diff --git a/src/tag/data.ts b/src/tag/data.ts index 49b803fc..22a53fd3 100644 --- a/src/tag/data.ts +++ b/src/tag/data.ts @@ -1,5 +1,5 @@ import { - getPhotosCached, + getPhotosCachedCached, getPhotosTagMetaCached, } from '@/photo/cache'; import { @@ -16,7 +16,7 @@ export const getPhotosTagDataCached = ({ limit?: number, }) => Promise.all([ - getPhotosCached({ tag, limit }), + getPhotosCachedCached({ tag, limit }), getPhotosTagMetaCached(tag), ]);