From e317d4459363ac53cd4e386299ad071d64e7ff15 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Tue, 18 Mar 2025 09:12:50 -0500 Subject: [PATCH] Standardize static category page generation --- app/film/[simulation]/image/route.tsx | 11 ++++------- app/film/[simulation]/page.tsx | 11 ++++++----- app/focal/[focal]/image/route.tsx | 11 ++++------- app/focal/[focal]/page.tsx | 10 ++++++---- app/lens/[make]/[model]/image/route.tsx | 7 ++----- app/lens/[make]/[model]/page.tsx | 5 ++--- app/recipe/[recipe]/image/route.tsx | 11 ++++------- app/recipe/[recipe]/page.tsx | 10 ++++++---- app/shot-on/[make]/[model]/image/route.tsx | 11 ++++------- app/shot-on/[make]/[model]/page.tsx | 10 ++++++---- app/tag/[tag]/image/route.tsx | 11 ++++------- app/tag/[tag]/page.tsx | 10 ++++++---- src/photo/set.ts | 18 +++++++++++++++++- 13 files changed, 71 insertions(+), 65 deletions(-) diff --git a/app/film/[simulation]/image/route.tsx b/app/film/[simulation]/image/route.tsx index 70ac67aa..537ef6f4 100644 --- a/app/film/[simulation]/image/route.tsx +++ b/app/film/[simulation]/image/route.tsx @@ -11,20 +11,17 @@ import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { getUniqueFilmSimulations } from '@/photo/db/query'; -import { - STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, - IS_PRODUCTION, -} from '@/app/config'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; export let generateStaticParams: (() => Promise<{ simulation: FilmSimulation }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('films', 'image')) { generateStaticParams = async () => { const simulations = await getUniqueFilmSimulations(); return simulations - .slice(0, GENERATE_STATIC_PARAMS_LIMIT) - .map(({ simulation }) => ({ simulation })); + .map(({ simulation }) => ({ simulation })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/film/[simulation]/page.tsx b/app/film/[simulation]/page.tsx index ec6e74b6..4211d391 100644 --- a/app/film/[simulation]/page.tsx +++ b/app/film/[simulation]/page.tsx @@ -2,24 +2,25 @@ import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; import { getUniqueFilmSimulations } from '@/photo/db/query'; import { FilmSimulation, generateMetaForFilmSimulation } from '@/simulation'; import FilmSimulationOverview from '@/simulation/FilmSimulationOverview'; -import { IS_PRODUCTION } from '@/app/config'; import { getPhotosFilmSimulationDataCached } from '@/simulation/data'; -import { STATICALLY_OPTIMIZED_PHOTO_CATEGORIES } from '@/app/config'; import { Metadata } from 'next/types'; import { cache } from 'react'; import { PATH_ROOT } from '@/app/paths'; import { redirect } from 'next/navigation'; - +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; +import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; const getPhotosFilmSimulationDataCachedCached = cache(getPhotosFilmSimulationDataCached); export let generateStaticParams: (() => Promise<{ simulation: FilmSimulation }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORIES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('films', 'page')) { generateStaticParams = async () => { const simulations = await getUniqueFilmSimulations(); - return simulations.map(({ simulation }) => ({ simulation })); + return simulations + .map(({ simulation }) => ({ simulation })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/focal/[focal]/image/route.tsx b/app/focal/[focal]/image/route.tsx index 00733977..dd62822a 100644 --- a/app/focal/[focal]/image/route.tsx +++ b/app/focal/[focal]/image/route.tsx @@ -11,20 +11,17 @@ import FocalLengthImageResponse from import { formatFocalLength, getFocalLengthFromString } from '@/focal'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { getUniqueFocalLengths } from '@/photo/db/query'; -import { - STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, - IS_PRODUCTION, -} from '@/app/config'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; export let generateStaticParams: (() => Promise<{ focal: string }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('focal-lengths', 'image')) { generateStaticParams = async () => { const focalLengths= await getUniqueFocalLengths(); return focalLengths - .slice(0, GENERATE_STATIC_PARAMS_LIMIT) - .map(({ focal }) => ({ focal: formatFocalLength(focal)! })); + .map(({ focal }) => ({ focal: formatFocalLength(focal)! })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/focal/[focal]/page.tsx b/app/focal/[focal]/page.tsx index 42634217..19f7399f 100644 --- a/app/focal/[focal]/page.tsx +++ b/app/focal/[focal]/page.tsx @@ -2,13 +2,13 @@ import { generateMetaForFocalLength, getFocalLengthFromString } from '@/focal'; import FocalLengthOverview from '@/focal/FocalLengthOverview'; import { getPhotosFocalLengthDataCached } from '@/focal/data'; import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; -import { IS_PRODUCTION } from '@/app/config'; import { getUniqueFocalLengths } from '@/photo/db/query'; -import { STATICALLY_OPTIMIZED_PHOTO_CATEGORIES } from '@/app/config'; import { PATH_ROOT } from '@/app/paths'; import type { Metadata } from 'next'; import { redirect } from 'next/navigation'; import { cache } from 'react'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; +import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; const getPhotosFocalDataCachedCached = cache((focal: number) => getPhotosFocalLengthDataCached({ @@ -19,10 +19,12 @@ const getPhotosFocalDataCachedCached = cache((focal: number) => export let generateStaticParams: (() => Promise<{ focal: string }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORIES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('focal-lengths', 'page')) { generateStaticParams = async () => { const focalLengths = await getUniqueFocalLengths(); - return focalLengths.map(({ focal }) => ({ focal: focal.toString() })); + return focalLengths + .map(({ focal }) => ({ focal: focal.toString() })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/lens/[make]/[model]/image/route.tsx b/app/lens/[make]/[model]/image/route.tsx index e8ced2f0..3374be34 100644 --- a/app/lens/[make]/[model]/image/route.tsx +++ b/app/lens/[make]/[model]/image/route.tsx @@ -8,10 +8,6 @@ import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { getUniqueLenses } from '@/photo/db/query'; -import { - STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, - IS_PRODUCTION, -} from '@/app/config'; import { getLensFromParams, Lens, @@ -19,11 +15,12 @@ import { safelyGenerateLensStaticParams, } from '@/lens'; import LensImageResponse from '@/image-response/LensImageResponse'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; export let generateStaticParams: (() => Promise) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('lenses', 'image')) { generateStaticParams = async () => { const lenses = await getUniqueLenses(); return safelyGenerateLensStaticParams(lenses) diff --git a/app/lens/[make]/[model]/page.tsx b/app/lens/[make]/[model]/page.tsx index 2b506a7f..3af4937c 100644 --- a/app/lens/[make]/[model]/page.tsx +++ b/app/lens/[make]/[model]/page.tsx @@ -1,8 +1,6 @@ import { Metadata } from 'next/types'; import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; import { cache } from 'react'; -import { STATICALLY_OPTIMIZED_PHOTO_CATEGORIES } from '@/app/config'; -import { IS_PRODUCTION } from '@/app/config'; import { getUniqueLenses } from '@/photo/db/query'; import { generateMetaForLens } from '@/lens/meta'; import { getPhotosLensDataCached } from '@/lens/data'; @@ -14,6 +12,7 @@ import { safelyGenerateLensStaticParams, } from '@/lens'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; const getPhotosLensDataCachedCached = cache(( make: string | undefined, @@ -27,7 +26,7 @@ const getPhotosLensDataCachedCached = cache(( export let generateStaticParams: (() => Promise) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORIES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('lenses', 'page')) { generateStaticParams = async () => { const lenses = await getUniqueLenses(); return safelyGenerateLensStaticParams(lenses) diff --git a/app/recipe/[recipe]/image/route.tsx b/app/recipe/[recipe]/image/route.tsx index ba2c88a9..7b75e7b9 100644 --- a/app/recipe/[recipe]/image/route.tsx +++ b/app/recipe/[recipe]/image/route.tsx @@ -8,21 +8,18 @@ import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { getUniqueRecipes } from '@/photo/db/query'; -import { - STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, - IS_PRODUCTION, -} from '@/app/config'; import RecipeImageResponse from '@/image-response/RecipeImageResponse'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; export let generateStaticParams: (() => Promise<{ recipe: string }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('recipes', 'image')) { generateStaticParams = async () => { const recipes = await getUniqueRecipes(); return recipes - .slice(0, GENERATE_STATIC_PARAMS_LIMIT) - .map(({ recipe }) => ({ recipe })); + .map(({ recipe }) => ({ recipe })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/recipe/[recipe]/page.tsx b/app/recipe/[recipe]/page.tsx index 830e4779..037987f0 100644 --- a/app/recipe/[recipe]/page.tsx +++ b/app/recipe/[recipe]/page.tsx @@ -1,7 +1,5 @@ import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; import { getUniqueRecipes } from '@/photo/db/query'; -import { IS_PRODUCTION } from '@/app/config'; -import { STATICALLY_OPTIMIZED_PHOTO_CATEGORIES } from '@/app/config'; import { PATH_ROOT } from '@/app/paths'; import type { Metadata } from 'next'; import { redirect } from 'next/navigation'; @@ -9,16 +7,20 @@ import { cache } from 'react'; import { generateMetaForRecipe } from '@/recipe'; import RecipeOverview from '@/recipe/RecipeOverview'; import { getPhotosRecipeDataCached } from '@/recipe/data'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; +import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; const getPhotosRecipeDataCachedCached = cache(getPhotosRecipeDataCached); export let generateStaticParams: (() => Promise<{ recipe: string }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORIES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('recipes', 'page')) { generateStaticParams = async () => { const recipes = await getUniqueRecipes(); - return recipes.map(({ recipe }) => ({ recipe })); + return recipes + .map(({ recipe }) => ({ recipe })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/shot-on/[make]/[model]/image/route.tsx b/app/shot-on/[make]/[model]/image/route.tsx index 4384a284..e3f2e819 100644 --- a/app/shot-on/[make]/[model]/image/route.tsx +++ b/app/shot-on/[make]/[model]/image/route.tsx @@ -10,20 +10,17 @@ import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { getUniqueCameras } from '@/photo/db/query'; -import { - STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, - IS_PRODUCTION, -} from '@/app/config'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; export let generateStaticParams: (() => Promise<{ camera: Camera }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('cameras', 'image')) { generateStaticParams = async () => { const cameras = await getUniqueCameras(); return cameras - .slice(0, GENERATE_STATIC_PARAMS_LIMIT) - .map(({ camera }) => ({ camera })); + .map(({ camera }) => ({ camera })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/shot-on/[make]/[model]/page.tsx b/app/shot-on/[make]/[model]/page.tsx index 573380a4..50cb42e3 100644 --- a/app/shot-on/[make]/[model]/page.tsx +++ b/app/shot-on/[make]/[model]/page.tsx @@ -5,9 +5,9 @@ import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; import { getPhotosCameraDataCached } from '@/camera/data'; import CameraOverview from '@/camera/CameraOverview'; import { cache } from 'react'; -import { STATICALLY_OPTIMIZED_PHOTO_CATEGORIES } from '@/app/config'; -import { IS_PRODUCTION } from '@/app/config'; import { getUniqueCameras } from '@/photo/db/query'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; +import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; const getPhotosCameraDataCachedCached = cache(( make: string, @@ -21,10 +21,12 @@ const getPhotosCameraDataCachedCached = cache(( export let generateStaticParams: (() => Promise) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORIES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('cameras', 'page')) { generateStaticParams = async () => { const cameras = await getUniqueCameras(); - return cameras.map(({ camera: { make, model } }) => ({ make, model })); + return cameras + .map(({ camera: { make, model } }) => ({ make, model })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/tag/[tag]/image/route.tsx b/app/tag/[tag]/image/route.tsx index 180378a2..f0983aac 100644 --- a/app/tag/[tag]/image/route.tsx +++ b/app/tag/[tag]/image/route.tsx @@ -9,20 +9,17 @@ import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; import { getUniqueTags } from '@/photo/db/query'; -import { - STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, - IS_PRODUCTION, -} from '@/app/config'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; export let generateStaticParams: (() => Promise<{ tag: string }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('tags', 'image')) { generateStaticParams = async () => { const tags = await getUniqueTags(); return tags - .slice(0, GENERATE_STATIC_PARAMS_LIMIT) - .map(({ tag }) => ({ tag })); + .map(({ tag }) => ({ tag })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/app/tag/[tag]/page.tsx b/app/tag/[tag]/page.tsx index f5db21ea..f3d3b455 100644 --- a/app/tag/[tag]/page.tsx +++ b/app/tag/[tag]/page.tsx @@ -1,7 +1,5 @@ import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; import { getUniqueTags } from '@/photo/db/query'; -import { IS_PRODUCTION } from '@/app/config'; -import { STATICALLY_OPTIMIZED_PHOTO_CATEGORIES } from '@/app/config'; import { PATH_ROOT } from '@/app/paths'; import { generateMetaForTag } from '@/tag'; import TagOverview from '@/tag/TagOverview'; @@ -9,6 +7,8 @@ import { getPhotosTagDataCached } from '@/tag/data'; import type { Metadata } from 'next'; import { redirect } from 'next/navigation'; import { cache } from 'react'; +import { shouldGenerateStaticParamsForCategory } from '@/photo/set'; +import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db'; const getPhotosTagDataCachedCached = cache((tag: string) => getPhotosTagDataCached({ tag, limit: INFINITE_SCROLL_GRID_INITIAL})); @@ -16,10 +16,12 @@ const getPhotosTagDataCachedCached = cache((tag: string) => export let generateStaticParams: (() => Promise<{ tag: string }[]>) | undefined = undefined; -if (STATICALLY_OPTIMIZED_PHOTO_CATEGORIES && IS_PRODUCTION) { +if (shouldGenerateStaticParamsForCategory('tags', 'page')) { generateStaticParams = async () => { const tags = await getUniqueTags(); - return tags.map(({ tag }) => ({ tag })); + return tags + .map(({ tag }) => ({ tag })) + .slice(0, GENERATE_STATIC_PARAMS_LIMIT); }; } diff --git a/src/photo/set.ts b/src/photo/set.ts index 57081775..19752848 100644 --- a/src/photo/set.ts +++ b/src/photo/set.ts @@ -6,11 +6,17 @@ import { Lens, Lenses } from '@/lens'; import { Tags } from '@/tag'; import { FocalLengths } from '@/focal'; import { Recipes } from '@/recipe'; +import { + CATEGORY_VISIBILITY, + IS_PRODUCTION, + STATICALLY_OPTIMIZED_PHOTO_CATEGORIES, + STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES, +} from '@/app/config'; const CATEGORY_KEYS = [ - 'tags', 'cameras', 'lenses', + 'tags', 'recipes', 'films', 'focal-lengths', @@ -27,6 +33,16 @@ export const DEFAULT_CATEGORY_KEYS: CategoryKeys = [ 'films', ]; +export const shouldGenerateStaticParamsForCategory = ( + key: CategoryKey, + type: 'page' | 'image', +): boolean => + CATEGORY_VISIBILITY.includes(key) && + IS_PRODUCTION && ( + (type === 'page' && STATICALLY_OPTIMIZED_PHOTO_CATEGORIES) || + (type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES) + ); + export const getHiddenCategories = (keys: CategoryKeys): CategoryKeys => CATEGORY_KEYS.filter(key => !keys.includes(key));