Consolidate static param functions

This commit is contained in:
Sam Becker 2025-03-18 19:28:39 -05:00
parent c4b1e349e1
commit 9b27a0be08
13 changed files with 101 additions and 206 deletions

View File

@ -10,23 +10,14 @@ import { getIBMPlexMono } from '@/app/font';
import { ImageResponse } from 'next/og'; import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
import { getUniqueFilmSimulations } from '@/photo/db/query'; import { getUniqueFilmSimulations } from '@/photo/db/query';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ simulation: FilmSimulation }[]>) | undefined = undefined; 'films',
'image',
if (shouldGenerateStaticParamsForCategory('films', 'image')) { getUniqueFilmSimulations,
generateStaticParams = () => simulations => simulations.map(({ simulation }) => ({ simulation })),
staticallyGenerateCategory( );
'films',
'image',
getUniqueFilmSimulations,
simulations => simulations.map(({ simulation }) => ({ simulation })),
);
}
export async function GET( export async function GET(
_: Request, _: Request,

View File

@ -7,26 +7,17 @@ import { Metadata } from 'next/types';
import { cache } from 'react'; import { cache } from 'react';
import { PATH_ROOT } from '@/app/paths'; import { PATH_ROOT } from '@/app/paths';
import { redirect } from 'next/navigation'; import { redirect } from 'next/navigation';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
const getPhotosFilmSimulationDataCachedCached = const getPhotosFilmSimulationDataCachedCached =
cache(getPhotosFilmSimulationDataCached); cache(getPhotosFilmSimulationDataCached);
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ simulation: FilmSimulation }[]>) | undefined = undefined; 'films',
'page',
if (shouldGenerateStaticParamsForCategory('films', 'page')) { getUniqueFilmSimulations,
generateStaticParams = () => simulations => simulations.map(({ simulation }) => ({ simulation })),
staticallyGenerateCategory( );
'films',
'page',
getUniqueFilmSimulations,
simulations => simulations.map(({ simulation }) => ({ simulation })),
);
}
interface FilmSimulationProps { interface FilmSimulationProps {
params: Promise<{ simulation: FilmSimulation }> params: Promise<{ simulation: FilmSimulation }>

View File

@ -10,24 +10,15 @@ import FocalLengthImageResponse from
'@/image-response/FocalLengthImageResponse'; '@/image-response/FocalLengthImageResponse';
import { formatFocalLength, getFocalLengthFromString } from '@/focal'; import { formatFocalLength, getFocalLengthFromString } from '@/focal';
import { getUniqueFocalLengths } from '@/photo/db/query'; import { getUniqueFocalLengths } from '@/photo/db/query';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ focal: string }[]>) | undefined = undefined; 'focal-lengths',
'image',
if (shouldGenerateStaticParamsForCategory('focal-lengths', 'image')) { getUniqueFocalLengths,
generateStaticParams = () => focalLengths => focalLengths
staticallyGenerateCategory( .map(({ focal }) => ({ focal: formatFocalLength(focal)! })),
'focal-lengths', );
'image',
getUniqueFocalLengths,
focalLengths => focalLengths
.map(({ focal }) => ({ focal: formatFocalLength(focal)! })),
);
}
export async function GET( export async function GET(
_: Request, _: Request,

View File

@ -7,10 +7,7 @@ import { PATH_ROOT } from '@/app/paths';
import type { Metadata } from 'next'; import type { Metadata } from 'next';
import { redirect } from 'next/navigation'; import { redirect } from 'next/navigation';
import { cache } from 'react'; import { cache } from 'react';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
const getPhotosFocalDataCachedCached = cache((focal: number) => const getPhotosFocalDataCachedCached = cache((focal: number) =>
getPhotosFocalLengthDataCached({ getPhotosFocalLengthDataCached({
@ -18,19 +15,13 @@ const getPhotosFocalDataCachedCached = cache((focal: number) =>
limit: INFINITE_SCROLL_GRID_INITIAL, limit: INFINITE_SCROLL_GRID_INITIAL,
})); }));
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ focal: string }[]>) | undefined = undefined; 'focal-lengths',
'page',
if (shouldGenerateStaticParamsForCategory('focal-lengths', 'page')) { getUniqueFocalLengths,
generateStaticParams = () => focalLengths => focalLengths
staticallyGenerateCategory( .map(({ focal }) => ({ focal: focal.toString() })),
'focal-lengths', );
'page',
getUniqueFocalLengths,
focalLengths => focalLengths
.map(({ focal }) => ({ focal: focal.toString() })),
);
}
interface FocalLengthProps { interface FocalLengthProps {
params: Promise<{ focal: string }> params: Promise<{ focal: string }>

View File

@ -9,28 +9,18 @@ import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
import { getUniqueLenses } from '@/photo/db/query'; import { getUniqueLenses } from '@/photo/db/query';
import { import {
getLensFromParams, getLensFromParams,
Lens,
LensProps, LensProps,
safelyGenerateLensStaticParams, safelyGenerateLensStaticParams,
} from '@/lens'; } from '@/lens';
import LensImageResponse from '@/image-response/LensImageResponse'; import LensImageResponse from '@/image-response/LensImageResponse';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<Lens[]>) | undefined = undefined; 'lenses',
'image',
if (shouldGenerateStaticParamsForCategory('lenses', 'image')) { getUniqueLenses,
generateStaticParams = () => safelyGenerateLensStaticParams,
staticallyGenerateCategory( );
'lenses',
'image',
getUniqueLenses,
safelyGenerateLensStaticParams,
);
}
export async function GET( export async function GET(
_: Request, _: Request,

View File

@ -7,13 +7,11 @@ import { getPhotosLensDataCached } from '@/lens/data';
import LensOverview from '@/lens/LensOverview'; import LensOverview from '@/lens/LensOverview';
import { import {
getLensFromParams, getLensFromParams,
Lens,
LensProps, LensProps,
safelyGenerateLensStaticParams, safelyGenerateLensStaticParams,
} from '@/lens'; } from '@/lens';
import { import {
shouldGenerateStaticParamsForCategory, staticallyGenerateCategoryIfConfigured,
staticallyGenerateCategory,
} from '@/category/server'; } from '@/category/server';
const getPhotosLensDataCachedCached = cache(( const getPhotosLensDataCachedCached = cache((
@ -25,18 +23,12 @@ const getPhotosLensDataCachedCached = cache((
INFINITE_SCROLL_GRID_INITIAL, INFINITE_SCROLL_GRID_INITIAL,
)); ));
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<Lens[]>) | undefined = undefined; 'lenses',
'page',
if (shouldGenerateStaticParamsForCategory('lenses', 'page')) { getUniqueLenses,
generateStaticParams = () => safelyGenerateLensStaticParams,
staticallyGenerateCategory( );
'lenses',
'page',
getUniqueLenses,
safelyGenerateLensStaticParams,
);
}
export async function generateMetadata({ export async function generateMetadata({
params, params,

View File

@ -8,23 +8,14 @@ import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
import { getUniqueRecipes } from '@/photo/db/query'; import { getUniqueRecipes } from '@/photo/db/query';
import RecipeImageResponse from '@/image-response/RecipeImageResponse'; import RecipeImageResponse from '@/image-response/RecipeImageResponse';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ recipe: string }[]>) | undefined = undefined; 'recipes',
'image',
if (shouldGenerateStaticParamsForCategory('recipes', 'image')) { getUniqueRecipes,
generateStaticParams = () => recipes => recipes.map(({ recipe }) => ({ recipe })),
staticallyGenerateCategory( );
'recipes',
'image',
getUniqueRecipes,
recipes => recipes.map(({ recipe }) => ({ recipe })),
);
}
export async function GET( export async function GET(
_: Request, _: Request,

View File

@ -7,25 +7,16 @@ import { cache } from 'react';
import { generateMetaForRecipe } from '@/recipe'; import { generateMetaForRecipe } from '@/recipe';
import RecipeOverview from '@/recipe/RecipeOverview'; import RecipeOverview from '@/recipe/RecipeOverview';
import { getPhotosRecipeDataCached } from '@/recipe/data'; import { getPhotosRecipeDataCached } from '@/recipe/data';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
const getPhotosRecipeDataCachedCached = cache(getPhotosRecipeDataCached); const getPhotosRecipeDataCachedCached = cache(getPhotosRecipeDataCached);
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ recipe: string }[]>) | undefined = undefined; 'recipes',
'page',
if (shouldGenerateStaticParamsForCategory('recipes', 'page')) { getUniqueRecipes,
generateStaticParams = () => recipes => recipes.map(({ recipe }) => ({ recipe })),
staticallyGenerateCategory( );
'recipes',
'page',
getUniqueRecipes,
recipes => recipes.map(({ recipe }) => ({ recipe })),
);
}
interface RecipeProps { interface RecipeProps {
params: Promise<{ recipe: string }> params: Promise<{ recipe: string }>

View File

@ -1,5 +1,5 @@
import { getPhotosCached } from '@/photo/cache'; import { getPhotosCached } from '@/photo/cache';
import { Camera, CameraProps, formatCameraParams } from '@/camera'; import { CameraProps, formatCameraParams } from '@/camera';
import { import {
IMAGE_OG_DIMENSION_SMALL, IMAGE_OG_DIMENSION_SMALL,
MAX_PHOTOS_TO_SHOW_PER_CATEGORY, MAX_PHOTOS_TO_SHOW_PER_CATEGORY,
@ -9,23 +9,14 @@ import { getIBMPlexMono } from '@/app/font';
import { ImageResponse } from 'next/og'; import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
import { getUniqueCameras } from '@/photo/db/query'; import { getUniqueCameras } from '@/photo/db/query';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<Camera[]>) | undefined = undefined; 'cameras',
'image',
if (shouldGenerateStaticParamsForCategory('cameras', 'image')) { getUniqueCameras,
generateStaticParams = () => cameras => cameras.map(({ camera }) => camera),
staticallyGenerateCategory( );
'cameras',
'image',
getUniqueCameras,
cameras => cameras.map(({ camera }) => camera),
);
}
export async function GET( export async function GET(
_: Request, _: Request,

View File

@ -1,15 +1,12 @@
import { Metadata } from 'next/types'; import { Metadata } from 'next/types';
import { Camera, CameraProps } from '@/camera'; import { CameraProps } from '@/camera';
import { generateMetaForCamera } from '@/camera/meta'; import { generateMetaForCamera } from '@/camera/meta';
import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo'; import { INFINITE_SCROLL_GRID_INITIAL } from '@/photo';
import { getPhotosCameraDataCached } from '@/camera/data'; import { getPhotosCameraDataCached } from '@/camera/data';
import CameraOverview from '@/camera/CameraOverview'; import CameraOverview from '@/camera/CameraOverview';
import { cache } from 'react'; import { cache } from 'react';
import { getUniqueCameras } from '@/photo/db/query'; import { getUniqueCameras } from '@/photo/db/query';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
const getPhotosCameraDataCachedCached = cache(( const getPhotosCameraDataCachedCached = cache((
make: string, make: string,
@ -20,18 +17,12 @@ const getPhotosCameraDataCachedCached = cache((
INFINITE_SCROLL_GRID_INITIAL, INFINITE_SCROLL_GRID_INITIAL,
)); ));
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<Camera[]>) | undefined = undefined; 'cameras',
'page',
if (shouldGenerateStaticParamsForCategory('cameras', 'page')) { getUniqueCameras,
generateStaticParams = () => cameras => cameras.map(({ camera }) => camera),
staticallyGenerateCategory( );
'cameras',
'page',
getUniqueCameras,
cameras => cameras.map(({ camera }) => camera),
);
}
export async function generateMetadata({ export async function generateMetadata({
params, params,

View File

@ -8,23 +8,14 @@ import { getIBMPlexMono } from '@/app/font';
import { ImageResponse } from 'next/og'; import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache'; import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
import { getUniqueTags } from '@/photo/db/query'; import { getUniqueTags } from '@/photo/db/query';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ tag: string }[]>) | undefined = undefined; 'tags',
'image',
if (shouldGenerateStaticParamsForCategory('tags', 'image')) { getUniqueTags,
generateStaticParams = () => tags => tags.map(({ tag }) => ({ tag })),
staticallyGenerateCategory( );
'tags',
'image',
getUniqueTags,
tags => tags.map(({ tag }) => ({ tag })),
);
}
export async function GET( export async function GET(
_: Request, _: Request,

View File

@ -7,26 +7,17 @@ import { getPhotosTagDataCached } from '@/tag/data';
import type { Metadata } from 'next'; import type { Metadata } from 'next';
import { redirect } from 'next/navigation'; import { redirect } from 'next/navigation';
import { cache } from 'react'; import { cache } from 'react';
import { import { staticallyGenerateCategoryIfConfigured } from '@/category/server';
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
const getPhotosTagDataCachedCached = cache((tag: string) => const getPhotosTagDataCachedCached = cache((tag: string) =>
getPhotosTagDataCached({ tag, limit: INFINITE_SCROLL_GRID_INITIAL})); getPhotosTagDataCached({ tag, limit: INFINITE_SCROLL_GRID_INITIAL}));
export let generateStaticParams: export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
(() => Promise<{ tag: string }[]>) | undefined = undefined; 'tags',
'page',
if (shouldGenerateStaticParamsForCategory('tags', 'page')) { getUniqueTags,
generateStaticParams = () => tags => tags.map(({ tag }) => ({ tag })),
staticallyGenerateCategory( );
'tags',
'page',
getUniqueTags,
tags => tags.map(({ tag }) => ({ tag })),
);
}
interface TagProps { interface TagProps {
params: Promise<{ tag: string }> params: Promise<{ tag: string }>

View File

@ -11,7 +11,7 @@ import { depluralize, pluralize } from '@/utility/string';
type StaticOutput = 'page' | 'image'; type StaticOutput = 'page' | 'image';
export const shouldGenerateStaticParamsForCategory = ( const shouldGenerateStaticParamsForCategory = (
key: CategoryKey, key: CategoryKey,
type: StaticOutput, type: StaticOutput,
): boolean => ): boolean =>
@ -21,18 +21,21 @@ export const shouldGenerateStaticParamsForCategory = (
(type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES) (type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES)
); );
export const staticallyGenerateCategory = async <T, K>( export const staticallyGenerateCategoryIfConfigured = <T, K>(
key: CategoryKey, key: CategoryKey,
type: StaticOutput, type: StaticOutput,
getData: () => Promise<T[]>, getData: () => Promise<T[]>,
formatData: (data: T[]) => K[], formatData: (data: T[]) => K[],
): Promise<K[]> => { ): (() => Promise<K[]>) | undefined =>
const data = (await getData()).slice(0, GENERATE_STATIC_PARAMS_LIMIT); shouldGenerateStaticParamsForCategory(key, type)
? async () => {
const data = (await getData()).slice(0, GENERATE_STATIC_PARAMS_LIMIT);
if (IS_BUILDING) { if (IS_BUILDING) {
const meta = pluralize(data.length, `${depluralize(key)} ${type}`); const meta = pluralize(data.length, `${depluralize(key)} ${type}`);
console.log(`Statically generating ${meta}`); console.log(`Statically generating ${meta}`);
} }
return formatData(data); return formatData(data);
}; }
: undefined;