Make og image access resilient on first deploy

This commit is contained in:
Sam Becker 2024-05-08 11:47:20 -05:00
parent 1763d7c063
commit 583df5698e
2 changed files with 15 additions and 2 deletions

View File

@ -6,6 +6,7 @@ import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
import { STATICALLY_OPTIMIZED } from '@/site/config'; import { STATICALLY_OPTIMIZED } from '@/site/config';
import { GENERATE_STATIC_PARAMS_LIMIT, getPhotoIds } from '@/photo/db'; import { GENERATE_STATIC_PARAMS_LIMIT, getPhotoIds } from '@/photo/db';
import { isNextImageReadyBasedOnPhotos } from '@/photo';
export let generateStaticParams: export let generateStaticParams:
(() => Promise<{ photoId: string }[]>) | undefined = undefined; (() => Promise<{ photoId: string }[]>) | undefined = undefined;
@ -35,8 +36,18 @@ export async function GET(
const { width, height } = IMAGE_OG_DIMENSION; 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([photo]);
return new ImageResponse( return new ImageResponse(
<PhotoImageResponse {...{ photo, width, height, fontFamily }} />, <PhotoImageResponse {...{
photo,
width,
height,
fontFamily,
isNextImageReady,
}} />,
{ width, height, fonts, headers }, { width, height, fonts, headers },
); );
} }

View File

@ -12,11 +12,13 @@ export default function PhotoImageResponse({
width, width,
height, height,
fontFamily, fontFamily,
isNextImageReady = true,
}: { }: {
photo: Photo photo: Photo
width: NextImageSize width: NextImageSize
height: number height: number
fontFamily: string fontFamily: string
isNextImageReady: boolean
}) { }) {
const model = photo.model const model = photo.model
? formatCameraModelTextShort(cameraFromPhoto(photo)) ? formatCameraModelTextShort(cameraFromPhoto(photo))
@ -25,7 +27,7 @@ export default function PhotoImageResponse({
return ( return (
<ImageContainer {...{ width, height }}> <ImageContainer {...{ width, height }}>
<ImagePhotoGrid {...{ <ImagePhotoGrid {...{
photos: [photo], photos: isNextImageReady ? [photo] : [],
width, width,
height, height,
...OG_TEXT_BOTTOM_ALIGNMENT && { imagePosition: 'top' }, ...OG_TEXT_BOTTOM_ALIGNMENT && { imagePosition: 'top' },