diff --git a/src/image-response/components/ImagePhotoGrid.tsx b/src/image-response/components/ImagePhotoGrid.tsx index f1b893a7..385e4012 100644 --- a/src/image-response/components/ImagePhotoGrid.tsx +++ b/src/image-response/components/ImagePhotoGrid.tsx @@ -7,6 +7,7 @@ import { doAllPhotosHaveOptimizedFiles, getOptimizedPhotoUrl, } from '@/photo/storage'; +import { fetchBase64ImageFromUrl } from '@/utility/image'; export default async function ImagePhotoGrid({ photos, @@ -53,7 +54,24 @@ export default async function ImagePhotoGrid({ const doOptimizedFilesExist = await doAllPhotosHaveOptimizedFiles(photos); - const renderPhoto = ({ id, url }: Photo, width: number, height: number) => + const photoData = await Promise.all(photos.map(async ({ id, url }) => { + const data = await fetchBase64ImageFromUrl( + getOptimizedPhotoUrl({ + imageUrl: url, + size: nextImageWidth, + addBypassSecret: IS_PREVIEW, + compatibilityMode: !doOptimizedFilesExist, + }), + { mode: 'no-cors' }, + ); + return { id, data }; + })); + + const renderPhoto = ( + { id, data }: typeof photoData[number], + width: number, + height: number, + ) =>
- {renderPhoto(photos[0], cellWidth, cellHeight * 2)} + {renderPhoto(photoData[0], cellWidth, cellHeight * 2)}
{/* Small images (R) */}
- {photos.slice(1).map(photo => + {photoData.slice(1).map(photo => renderPhoto(photo, cellWidth, cellHeight), )}
- : photos.slice(0, count).map(photo => + : photoData.slice(0, count).map(photo => renderPhoto(photo, cellWidth, cellHeight), )} diff --git a/src/utility/image.ts b/src/utility/image.ts index f6234a4f..840fcf6c 100644 --- a/src/utility/image.ts +++ b/src/utility/image.ts @@ -1,3 +1,14 @@ export const removeBase64Prefix = (base64: string) => { return base64.match(/^data:image\/[a-z]{3,4};base64,(.+)$/)?.[1] ?? base64; }; + +export const fetchBase64ImageFromUrl = ( + url: string, + fetchOptions?: RequestInit, +) => + fetch(url, fetchOptions) + .then(async response => { + const blob = await response.arrayBuffer(); + // eslint-disable-next-line max-len + return `data:${response.headers.get('content-type')};base64,${Buffer.from(blob).toString('base64')}`; + });