diff --git a/src/app/home-image/route.tsx b/src/app/home-image/route.tsx index 37d0b5a0..6566e67c 100644 --- a/src/app/home-image/route.tsx +++ b/src/app/home-image/route.tsx @@ -7,6 +7,7 @@ import HomeImageResponse from '@/image-response/HomeImageResponse'; import { getIBMPlexMonoMedium } from '@/site/font'; import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; +import { isNextImageReadyBasedOnPhotos } from '@/photo'; export async function GET() { const [ @@ -21,8 +22,17 @@ export async function GET() { const { width, height } = IMAGE_OG_DIMENSION_SMALL; + // Make sure next/image can be reached from absolute urls, + // which may not exist on first pre-render + const isNextImageReady = await isNextImageReadyBasedOnPhotos(photos); + return new ImageResponse( - , + , { width, height, headers, fonts }, ); } diff --git a/src/app/template-image-tight/route.tsx b/src/app/template-image-tight/route.tsx index 944e9291..5d444fc9 100644 --- a/src/app/template-image-tight/route.tsx +++ b/src/app/template-image-tight/route.tsx @@ -8,6 +8,7 @@ import TemplateImageResponse from import { getIBMPlexMonoMedium } from '@/site/font'; import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; +import { isNextImageReadyBasedOnPhotos } from '@/photo'; export async function GET() { const [ @@ -25,10 +26,14 @@ export async function GET() { const { width, height } = IMAGE_OG_DIMENSION; + // Make sure next/image can be reached from absolute urls, + // which may not exist on first pre-render + const isNextImageReady = await isNextImageReadyBasedOnPhotos(photos); + return new ImageResponse( ( - {photos.slice(0, count).map(async ({ id, url }) => { - const src = getNextImageUrlForRequest(url, nextImageWidth); - // Make sure next/image can be reached from absolute url, - // which may not exist on first pre-render - const doesNextImageExist = await fetch(src) - .then(response => response.ok) - .catch(() => false); - return
+
- {doesNextImageExist - ? - :
} -
; - })} + }, + }} /> +
+ )}
); } diff --git a/src/photo/index.ts b/src/photo/index.ts index 7469c90e..a742c079 100644 --- a/src/photo/index.ts +++ b/src/photo/index.ts @@ -1,3 +1,4 @@ +import { getNextImageUrlForRequest } from '@/services/next-image'; import { FilmSimulation } from '@/simulation'; import { HIGH_DENSITY_GRID, SHOW_EXIF_DATA } from '@/site/config'; import { ABSOLUTE_PATH_FOR_HOME_IMAGE } from '@/site/paths'; @@ -264,3 +265,8 @@ export const getKeywordsForPhoto = (photo: Photo) => .concat((photo.semanticDescription ?? '').split(' ')) .filter(Boolean) .map(keyword => keyword.toLocaleLowerCase()); + +export const isNextImageReadyBasedOnPhotos = async (photos: Photo[]) => + photos.length > 0 && fetch(getNextImageUrlForRequest(photos[0].url, 640)) + .then(response => response.ok) + .catch(() => false);