diff --git a/app/film/[simulation]/image/route.tsx b/app/film/[simulation]/image/route.tsx index faad7909..dd8cd57f 100644 --- a/app/film/[simulation]/image/route.tsx +++ b/app/film/[simulation]/image/route.tsx @@ -10,23 +10,14 @@ import { getIBMPlexMono } from '@/app/font'; import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getUniqueFilmSimulations } from '@/photo/db/query'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; -export let generateStaticParams: - (() => Promise<{ simulation: FilmSimulation }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('films', 'image')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'films', - 'image', - getUniqueFilmSimulations, - simulations => simulations.map(({ simulation }) => ({ simulation })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'films', + 'image', + getUniqueFilmSimulations, + simulations => simulations.map(({ simulation }) => ({ simulation })), +); export async function GET( _: Request, diff --git a/app/film/[simulation]/page.tsx b/app/film/[simulation]/page.tsx index b1c892cc..79349f54 100644 --- a/app/film/[simulation]/page.tsx +++ b/app/film/[simulation]/page.tsx @@ -7,26 +7,17 @@ import { Metadata } from 'next/types'; import { cache } from 'react'; import { PATH_ROOT } from '@/app/paths'; import { redirect } from 'next/navigation'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; const getPhotosFilmSimulationDataCachedCached = cache(getPhotosFilmSimulationDataCached); -export let generateStaticParams: - (() => Promise<{ simulation: FilmSimulation }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('films', 'page')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'films', - 'page', - getUniqueFilmSimulations, - simulations => simulations.map(({ simulation }) => ({ simulation })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'films', + 'page', + getUniqueFilmSimulations, + simulations => simulations.map(({ simulation }) => ({ simulation })), +); interface FilmSimulationProps { params: Promise<{ simulation: FilmSimulation }> diff --git a/app/focal/[focal]/image/route.tsx b/app/focal/[focal]/image/route.tsx index f447f4cd..516861f5 100644 --- a/app/focal/[focal]/image/route.tsx +++ b/app/focal/[focal]/image/route.tsx @@ -10,24 +10,15 @@ import FocalLengthImageResponse from '@/image-response/FocalLengthImageResponse'; import { formatFocalLength, getFocalLengthFromString } from '@/focal'; import { getUniqueFocalLengths } from '@/photo/db/query'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; -export let generateStaticParams: - (() => Promise<{ focal: string }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('focal-lengths', 'image')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'focal-lengths', - 'image', - getUniqueFocalLengths, - focalLengths => focalLengths - .map(({ focal }) => ({ focal: formatFocalLength(focal)! })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'focal-lengths', + 'image', + getUniqueFocalLengths, + focalLengths => focalLengths + .map(({ focal }) => ({ focal: formatFocalLength(focal)! })), +); export async function GET( _: Request, diff --git a/app/focal/[focal]/page.tsx b/app/focal/[focal]/page.tsx index 32656624..5a561d65 100644 --- a/app/focal/[focal]/page.tsx +++ b/app/focal/[focal]/page.tsx @@ -7,10 +7,7 @@ import { PATH_ROOT } from '@/app/paths'; import type { Metadata } from 'next'; import { redirect } from 'next/navigation'; import { cache } from 'react'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; const getPhotosFocalDataCachedCached = cache((focal: number) => getPhotosFocalLengthDataCached({ @@ -18,19 +15,13 @@ const getPhotosFocalDataCachedCached = cache((focal: number) => limit: INFINITE_SCROLL_GRID_INITIAL, })); -export let generateStaticParams: - (() => Promise<{ focal: string }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('focal-lengths', 'page')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'focal-lengths', - 'page', - getUniqueFocalLengths, - focalLengths => focalLengths - .map(({ focal }) => ({ focal: focal.toString() })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'focal-lengths', + 'page', + getUniqueFocalLengths, + focalLengths => focalLengths + .map(({ focal }) => ({ focal: focal.toString() })), +); interface FocalLengthProps { params: Promise<{ focal: string }> diff --git a/app/lens/[make]/[model]/image/route.tsx b/app/lens/[make]/[model]/image/route.tsx index 4fccfbaf..1867b4d1 100644 --- a/app/lens/[make]/[model]/image/route.tsx +++ b/app/lens/[make]/[model]/image/route.tsx @@ -9,28 +9,18 @@ import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getUniqueLenses } from '@/photo/db/query'; import { getLensFromParams, - Lens, LensProps, safelyGenerateLensStaticParams, } from '@/lens'; import LensImageResponse from '@/image-response/LensImageResponse'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; -export let generateStaticParams: - (() => Promise) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('lenses', 'image')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'lenses', - 'image', - getUniqueLenses, - safelyGenerateLensStaticParams, - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'lenses', + 'image', + getUniqueLenses, + safelyGenerateLensStaticParams, +); export async function GET( _: Request, diff --git a/app/lens/[make]/[model]/page.tsx b/app/lens/[make]/[model]/page.tsx index 647b83a6..9dd2eec1 100644 --- a/app/lens/[make]/[model]/page.tsx +++ b/app/lens/[make]/[model]/page.tsx @@ -7,13 +7,11 @@ import { getPhotosLensDataCached } from '@/lens/data'; import LensOverview from '@/lens/LensOverview'; import { getLensFromParams, - Lens, LensProps, safelyGenerateLensStaticParams, } from '@/lens'; import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, + staticallyGenerateCategoryIfConfigured, } from '@/category/server'; const getPhotosLensDataCachedCached = cache(( @@ -25,18 +23,12 @@ const getPhotosLensDataCachedCached = cache(( INFINITE_SCROLL_GRID_INITIAL, )); -export let generateStaticParams: - (() => Promise) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('lenses', 'page')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'lenses', - 'page', - getUniqueLenses, - safelyGenerateLensStaticParams, - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'lenses', + 'page', + getUniqueLenses, + safelyGenerateLensStaticParams, +); export async function generateMetadata({ params, diff --git a/app/recipe/[recipe]/image/route.tsx b/app/recipe/[recipe]/image/route.tsx index 7c9be7d9..7e53acd5 100644 --- a/app/recipe/[recipe]/image/route.tsx +++ b/app/recipe/[recipe]/image/route.tsx @@ -8,23 +8,14 @@ import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getUniqueRecipes } from '@/photo/db/query'; import RecipeImageResponse from '@/image-response/RecipeImageResponse'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; -export let generateStaticParams: - (() => Promise<{ recipe: string }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('recipes', 'image')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'recipes', - 'image', - getUniqueRecipes, - recipes => recipes.map(({ recipe }) => ({ recipe })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'recipes', + 'image', + getUniqueRecipes, + recipes => recipes.map(({ recipe }) => ({ recipe })), +); export async function GET( _: Request, diff --git a/app/recipe/[recipe]/page.tsx b/app/recipe/[recipe]/page.tsx index bdcc1079..7aae7a58 100644 --- a/app/recipe/[recipe]/page.tsx +++ b/app/recipe/[recipe]/page.tsx @@ -7,25 +7,16 @@ import { cache } from 'react'; import { generateMetaForRecipe } from '@/recipe'; import RecipeOverview from '@/recipe/RecipeOverview'; import { getPhotosRecipeDataCached } from '@/recipe/data'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; const getPhotosRecipeDataCachedCached = cache(getPhotosRecipeDataCached); -export let generateStaticParams: - (() => Promise<{ recipe: string }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('recipes', 'page')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'recipes', - 'page', - getUniqueRecipes, - recipes => recipes.map(({ recipe }) => ({ recipe })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'recipes', + 'page', + getUniqueRecipes, + recipes => recipes.map(({ recipe }) => ({ recipe })), +); interface RecipeProps { params: Promise<{ recipe: string }> diff --git a/app/shot-on/[make]/[model]/image/route.tsx b/app/shot-on/[make]/[model]/image/route.tsx index a28b7cb9..e1f6b368 100644 --- a/app/shot-on/[make]/[model]/image/route.tsx +++ b/app/shot-on/[make]/[model]/image/route.tsx @@ -1,5 +1,5 @@ import { getPhotosCached } from '@/photo/cache'; -import { Camera, CameraProps, formatCameraParams } from '@/camera'; +import { CameraProps, formatCameraParams } from '@/camera'; import { IMAGE_OG_DIMENSION_SMALL, MAX_PHOTOS_TO_SHOW_PER_CATEGORY, @@ -9,23 +9,14 @@ import { getIBMPlexMono } from '@/app/font'; import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getUniqueCameras } from '@/photo/db/query'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; -export let generateStaticParams: - (() => Promise) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('cameras', 'image')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'cameras', - 'image', - getUniqueCameras, - cameras => cameras.map(({ camera }) => camera), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'cameras', + 'image', + getUniqueCameras, + cameras => cameras.map(({ camera }) => camera), +); export async function GET( _: Request, diff --git a/app/shot-on/[make]/[model]/page.tsx b/app/shot-on/[make]/[model]/page.tsx index c0e3a20c..85d4bd1f 100644 --- a/app/shot-on/[make]/[model]/page.tsx +++ b/app/shot-on/[make]/[model]/page.tsx @@ -1,15 +1,12 @@ import { Metadata } from 'next/types'; -import { Camera, CameraProps } from '@/camera'; +import { CameraProps } from '@/camera'; import { generateMetaForCamera } from '@/camera/meta'; import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; import { getPhotosCameraDataCached } from '@/camera/data'; import CameraOverview from '@/camera/CameraOverview'; import { cache } from 'react'; import { getUniqueCameras } from '@/photo/db/query'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; const getPhotosCameraDataCachedCached = cache(( make: string, @@ -20,18 +17,12 @@ const getPhotosCameraDataCachedCached = cache(( INFINITE_SCROLL_GRID_INITIAL, )); -export let generateStaticParams: - (() => Promise) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('cameras', 'page')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'cameras', - 'page', - getUniqueCameras, - cameras => cameras.map(({ camera }) => camera), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'cameras', + 'page', + getUniqueCameras, + cameras => cameras.map(({ camera }) => camera), +); export async function generateMetadata({ params, diff --git a/app/tag/[tag]/image/route.tsx b/app/tag/[tag]/image/route.tsx index 1dc33eda..48e11d83 100644 --- a/app/tag/[tag]/image/route.tsx +++ b/app/tag/[tag]/image/route.tsx @@ -8,23 +8,14 @@ import { getIBMPlexMono } from '@/app/font'; import { ImageResponse } from 'next/og'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getUniqueTags } from '@/photo/db/query'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; -export let generateStaticParams: - (() => Promise<{ tag: string }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('tags', 'image')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'tags', - 'image', - getUniqueTags, - tags => tags.map(({ tag }) => ({ tag })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'tags', + 'image', + getUniqueTags, + tags => tags.map(({ tag }) => ({ tag })), +); export async function GET( _: Request, diff --git a/app/tag/[tag]/page.tsx b/app/tag/[tag]/page.tsx index a645a9a4..ab090c2c 100644 --- a/app/tag/[tag]/page.tsx +++ b/app/tag/[tag]/page.tsx @@ -7,26 +7,17 @@ import { getPhotosTagDataCached } from '@/tag/data'; import type { Metadata } from 'next'; import { redirect } from 'next/navigation'; import { cache } from 'react'; -import { - shouldGenerateStaticParamsForCategory, - staticallyGenerateCategory, -} from '@/category/server'; +import { staticallyGenerateCategoryIfConfigured } from '@/category/server'; const getPhotosTagDataCachedCached = cache((tag: string) => getPhotosTagDataCached({ tag, limit: INFINITE_SCROLL_GRID_INITIAL})); -export let generateStaticParams: - (() => Promise<{ tag: string }[]>) | undefined = undefined; - -if (shouldGenerateStaticParamsForCategory('tags', 'page')) { - generateStaticParams = () => - staticallyGenerateCategory( - 'tags', - 'page', - getUniqueTags, - tags => tags.map(({ tag }) => ({ tag })), - ); -} +export const generateStaticParams = staticallyGenerateCategoryIfConfigured( + 'tags', + 'page', + getUniqueTags, + tags => tags.map(({ tag }) => ({ tag })), +); interface TagProps { params: Promise<{ tag: string }> diff --git a/src/category/server.ts b/src/category/server.ts index 46e49c87..21697043 100644 --- a/src/category/server.ts +++ b/src/category/server.ts @@ -11,7 +11,7 @@ import { depluralize, pluralize } from '@/utility/string'; type StaticOutput = 'page' | 'image'; -export const shouldGenerateStaticParamsForCategory = ( +const shouldGenerateStaticParamsForCategory = ( key: CategoryKey, type: StaticOutput, ): boolean => @@ -21,18 +21,21 @@ export const shouldGenerateStaticParamsForCategory = ( (type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES) ); -export const staticallyGenerateCategory = async ( +export const staticallyGenerateCategoryIfConfigured = ( key: CategoryKey, type: StaticOutput, getData: () => Promise, formatData: (data: T[]) => K[], -): Promise => { - const data = (await getData()).slice(0, GENERATE_STATIC_PARAMS_LIMIT); +): (() => Promise) | undefined => + shouldGenerateStaticParamsForCategory(key, type) + ? 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}`); - } + if (IS_BUILDING) { + const meta = pluralize(data.length, `${depluralize(key)} ${type}`); + console.log(`Statically generating ${meta}`); + } - return formatData(data); -}; + return formatData(data); + } + : undefined;