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 { 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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'films',
'image',
getUniqueFilmSimulations,
simulations => simulations.map(({ simulation }) => ({ simulation })),
);
}
);
export async function GET(
_: Request,

View File

@ -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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'films',
'page',
getUniqueFilmSimulations,
simulations => simulations.map(({ simulation }) => ({ simulation })),
);
}
);
interface FilmSimulationProps {
params: Promise<{ simulation: FilmSimulation }>

View File

@ -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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'focal-lengths',
'image',
getUniqueFocalLengths,
focalLengths => focalLengths
.map(({ focal }) => ({ focal: formatFocalLength(focal)! })),
);
}
);
export async function GET(
_: Request,

View File

@ -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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'focal-lengths',
'page',
getUniqueFocalLengths,
focalLengths => focalLengths
.map(({ focal }) => ({ focal: focal.toString() })),
);
}
);
interface FocalLengthProps {
params: Promise<{ focal: string }>

View File

@ -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<Lens[]>) | undefined = undefined;
if (shouldGenerateStaticParamsForCategory('lenses', 'image')) {
generateStaticParams = () =>
staticallyGenerateCategory(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'lenses',
'image',
getUniqueLenses,
safelyGenerateLensStaticParams,
);
}
);
export async function GET(
_: Request,

View File

@ -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<Lens[]>) | undefined = undefined;
if (shouldGenerateStaticParamsForCategory('lenses', 'page')) {
generateStaticParams = () =>
staticallyGenerateCategory(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'lenses',
'page',
getUniqueLenses,
safelyGenerateLensStaticParams,
);
}
);
export async function generateMetadata({
params,

View File

@ -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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'recipes',
'image',
getUniqueRecipes,
recipes => recipes.map(({ recipe }) => ({ recipe })),
);
}
);
export async function GET(
_: Request,

View File

@ -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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'recipes',
'page',
getUniqueRecipes,
recipes => recipes.map(({ recipe }) => ({ recipe })),
);
}
);
interface RecipeProps {
params: Promise<{ recipe: string }>

View File

@ -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<Camera[]>) | undefined = undefined;
if (shouldGenerateStaticParamsForCategory('cameras', 'image')) {
generateStaticParams = () =>
staticallyGenerateCategory(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'cameras',
'image',
getUniqueCameras,
cameras => cameras.map(({ camera }) => camera),
);
}
);
export async function GET(
_: Request,

View File

@ -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<Camera[]>) | undefined = undefined;
if (shouldGenerateStaticParamsForCategory('cameras', 'page')) {
generateStaticParams = () =>
staticallyGenerateCategory(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'cameras',
'page',
getUniqueCameras,
cameras => cameras.map(({ camera }) => camera),
);
}
);
export async function generateMetadata({
params,

View File

@ -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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'tags',
'image',
getUniqueTags,
tags => tags.map(({ tag }) => ({ tag })),
);
}
);
export async function GET(
_: Request,

View File

@ -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(
export const generateStaticParams = staticallyGenerateCategoryIfConfigured(
'tags',
'page',
getUniqueTags,
tags => tags.map(({ tag }) => ({ tag })),
);
}
);
interface TagProps {
params: Promise<{ tag: string }>

View File

@ -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,12 +21,14 @@ export const shouldGenerateStaticParamsForCategory = (
(type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES)
);
export const staticallyGenerateCategory = async <T, K>(
export const staticallyGenerateCategoryIfConfigured = <T, K>(
key: CategoryKey,
type: StaticOutput,
getData: () => Promise<T[]>,
formatData: (data: T[]) => K[],
): Promise<K[]> => {
): (() => Promise<K[]>) | undefined =>
shouldGenerateStaticParamsForCategory(key, type)
? async () => {
const data = (await getData()).slice(0, GENERATE_STATIC_PARAMS_LIMIT);
if (IS_BUILDING) {
@ -35,4 +37,5 @@ export const staticallyGenerateCategory = async <T, K>(
}
return formatData(data);
};
}
: undefined;