From 1796e0a24f521f0e7dc602cd7586d1371e78544f Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Tue, 18 Mar 2025 21:07:21 -0500 Subject: [PATCH] Refactor/standardize core photo static generation --- app/p/[photoId]/image/route.tsx | 19 ++++-------------- app/p/[photoId]/page.tsx | 16 ++++------------ src/category/server.ts | 34 ++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 38 deletions(-) diff --git a/app/p/[photoId]/image/route.tsx b/app/p/[photoId]/image/route.tsx index 38d4dee2..366c7294 100644 --- a/app/p/[photoId]/image/route.tsx +++ b/app/p/[photoId]/image/route.tsx @@ -4,23 +4,12 @@ import PhotoImageResponse from '@/image-response/PhotoImageResponse'; import { getIBMPlexMono } from '@/app/font'; import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; -import { - IS_PRODUCTION, - STATICALLY_OPTIMIZED_PHOTO_OG_IMAGES, -} from '@/app/config'; -import { getPhotoIds } from '@/photo/db/query'; -import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { isNextImageReadyBasedOnPhotos } from '@/photo'; +import { staticallyGeneratePhotosIfConfigured } from '@/category/server'; -export let generateStaticParams: - (() => Promise<{ photoId: string }[]>) | undefined = undefined; - -if (STATICALLY_OPTIMIZED_PHOTO_OG_IMAGES && IS_PRODUCTION) { - generateStaticParams = async () => { - const photos = await getPhotoIds({ limit: GENERATE_STATIC_PARAMS_LIMIT }); - return photos.map(photoId => ({ photoId })); - }; -} +export const generateStaticParams = staticallyGeneratePhotosIfConfigured( + 'image', +); export async function GET( _: Request, diff --git a/app/p/[photoId]/page.tsx b/app/p/[photoId]/page.tsx index 01b77997..6f8f55f9 100644 --- a/app/p/[photoId]/page.tsx +++ b/app/p/[photoId]/page.tsx @@ -12,25 +12,17 @@ import { } from '@/app/paths'; import PhotoDetailPage from '@/photo/PhotoDetailPage'; import { getPhotosNearIdCached } from '@/photo/cache'; -import { IS_PRODUCTION, STATICALLY_OPTIMIZED_PHOTOS } from '@/app/config'; -import { getPhotoIds } from '@/photo/db/query'; -import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { cache } from 'react'; +import { staticallyGeneratePhotosIfConfigured } from '@/category/server'; export const maxDuration = 60; const getPhotosNearIdCachedCached = cache((photoId: string) => getPhotosNearIdCached(photoId, { limit: RELATED_GRID_PHOTOS_TO_SHOW + 2 })); -export let generateStaticParams: - (() => Promise<{ photoId: string }[]>) | undefined = undefined; - -if (STATICALLY_OPTIMIZED_PHOTOS && IS_PRODUCTION) { - generateStaticParams = async () => { - const photos = await getPhotoIds({ limit: GENERATE_STATIC_PARAMS_LIMIT }); - return photos.map(photoId => ({ photoId })); - }; -} +export const generateStaticParams = staticallyGeneratePhotosIfConfigured( + 'page', +); interface PhotoProps { params: Promise<{ photoId: string }> diff --git a/src/category/server.ts b/src/category/server.ts index 21697043..eb2d2842 100644 --- a/src/category/server.ts +++ b/src/category/server.ts @@ -7,19 +7,27 @@ import { STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, } from '@/app/config'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; +import { getPhotoIds } from '@/photo/db/query'; import { depluralize, pluralize } from '@/utility/string'; type StaticOutput = 'page' | 'image'; -const shouldGenerateStaticParamsForCategory = ( - key: CategoryKey, - type: StaticOutput, -): boolean => - CATEGORY_VISIBILITY.includes(key) && +const logStaticGenerationDetails = (count: number, content: string) => + console.log(`Statically generating ${pluralize(count, content)} ...`); + +export const staticallyGeneratePhotosIfConfigured = (type: StaticOutput) => IS_PRODUCTION && ( (type === 'page' && STATICALLY_OPTIMIZED_PHOTO_CATEGORIES) || (type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES) - ); + ) + ? async () => { + const photos = await getPhotoIds({ limit: GENERATE_STATIC_PARAMS_LIMIT }); + if (IS_BUILDING) { + logStaticGenerationDetails(photos.length, `photo ${type}`); + } + return photos.map(photoId => ({ photoId })); + } + : undefined; export const staticallyGenerateCategoryIfConfigured = ( key: CategoryKey, @@ -27,15 +35,19 @@ export const staticallyGenerateCategoryIfConfigured = ( getData: () => Promise, formatData: (data: T[]) => K[], ): (() => Promise) | undefined => - shouldGenerateStaticParamsForCategory(key, type) + CATEGORY_VISIBILITY.includes(key) && + IS_PRODUCTION && ( + (type === 'page' && STATICALLY_OPTIMIZED_PHOTO_CATEGORIES) || + (type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES) + ) ? async () => { const data = (await getData()).slice(0, GENERATE_STATIC_PARAMS_LIMIT); - if (IS_BUILDING) { - const meta = pluralize(data.length, `${depluralize(key)} ${type}`); - console.log(`Statically generating ${meta}`); + logStaticGenerationDetails( + data.length, + `${depluralize(key)} ${type}`, + ); } - return formatData(data); } : undefined;