Add new static generation routine to cameras with logging

This commit is contained in:
Sam Becker 2025-03-18 18:30:19 -05:00
parent c6f28616c9
commit f0d9ea28b6
14 changed files with 77 additions and 40 deletions

View File

@ -11,7 +11,7 @@ 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 { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
export let generateStaticParams:
(() => Promise<{ simulation: FilmSimulation }[]>) | undefined = undefined;

View File

@ -7,7 +7,7 @@ import { Metadata } from 'next/types';
import { cache } from 'react';
import { PATH_ROOT } from '@/app/paths';
import { redirect } from 'next/navigation';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
const getPhotosFilmSimulationDataCachedCached =

View File

@ -11,7 +11,7 @@ import FocalLengthImageResponse from
import { formatFocalLength, getFocalLengthFromString } from '@/focal';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
import { getUniqueFocalLengths } from '@/photo/db/query';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
export let generateStaticParams:
(() => Promise<{ focal: string }[]>) | undefined = undefined;

View File

@ -7,7 +7,7 @@ import { PATH_ROOT } from '@/app/paths';
import type { Metadata } from 'next';
import { redirect } from 'next/navigation';
import { cache } from 'react';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
const getPhotosFocalDataCachedCached = cache((focal: number) =>

View File

@ -15,7 +15,7 @@ import {
safelyGenerateLensStaticParams,
} from '@/lens';
import LensImageResponse from '@/image-response/LensImageResponse';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
export let generateStaticParams:
(() => Promise<Lens[]>) | undefined = undefined;

View File

@ -12,7 +12,7 @@ import {
safelyGenerateLensStaticParams,
} from '@/lens';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
const getPhotosLensDataCachedCached = cache((
make: string | undefined,

View File

@ -9,7 +9,7 @@ import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
import { getUniqueRecipes } from '@/photo/db/query';
import RecipeImageResponse from '@/image-response/RecipeImageResponse';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
export let generateStaticParams:
(() => Promise<{ recipe: string }[]>) | undefined = undefined;

View File

@ -7,7 +7,7 @@ import { cache } from 'react';
import { generateMetaForRecipe } from '@/recipe';
import RecipeOverview from '@/recipe/RecipeOverview';
import { getPhotosRecipeDataCached } from '@/recipe/data';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
const getPhotosRecipeDataCachedCached = cache(getPhotosRecipeDataCached);

View File

@ -8,20 +8,23 @@ import CameraImageResponse from '@/image-response/CameraImageResponse';
import { getIBMPlexMono } from '@/app/font';
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 { shouldGenerateStaticParamsForCategory } from '@/app/config';
import {
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
export let generateStaticParams:
(() => Promise<{ camera: Camera }[]>) | undefined = undefined;
(() => Promise<Camera[]>) | undefined = undefined;
if (shouldGenerateStaticParamsForCategory('cameras', 'image')) {
generateStaticParams = async () => {
const cameras = await getUniqueCameras();
return cameras
.map(({ camera }) => ({ camera }))
.slice(0, GENERATE_STATIC_PARAMS_LIMIT);
};
generateStaticParams = () =>
staticallyGenerateCategory(
'cameras',
'image',
getUniqueCameras,
cameras => cameras.map(({ camera }) => camera),
);
}
export async function GET(

View File

@ -6,8 +6,10 @@ import { getPhotosCameraDataCached } from '@/camera/data';
import CameraOverview from '@/camera/CameraOverview';
import { cache } from 'react';
import { getUniqueCameras } from '@/photo/db/query';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
import {
shouldGenerateStaticParamsForCategory,
staticallyGenerateCategory,
} from '@/category/server';
const getPhotosCameraDataCachedCached = cache((
make: string,
@ -22,12 +24,13 @@ export let generateStaticParams:
(() => Promise<Camera[]>) | undefined = undefined;
if (shouldGenerateStaticParamsForCategory('cameras', 'page')) {
generateStaticParams = async () => {
const cameras = await getUniqueCameras();
return cameras
.map(({ camera: { make, model } }) => ({ make, model }))
.slice(0, GENERATE_STATIC_PARAMS_LIMIT);
};
generateStaticParams = () =>
staticallyGenerateCategory(
'cameras',
'page',
getUniqueCameras,
cameras => cameras.map(({ camera }) => camera),
);
}
export async function generateMetadata({

View File

@ -9,7 +9,7 @@ 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 { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
export let generateStaticParams:
(() => Promise<{ tag: string }[]>) | undefined = undefined;

View File

@ -7,7 +7,7 @@ import { getPhotosTagDataCached } from '@/tag/data';
import type { Metadata } from 'next';
import { redirect } from 'next/navigation';
import { cache } from 'react';
import { shouldGenerateStaticParamsForCategory } from '@/app/config';
import { shouldGenerateStaticParamsForCategory } from '@/category/server';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
const getPhotosTagDataCachedCached = cache((tag: string) =>

View File

@ -2,7 +2,7 @@ import {
AI_AUTO_GENERATED_FIELDS_DEFAULT,
parseAiAutoGeneratedFieldsString,
} from '@/photo/ai';
import { CategoryKey, getOrderedCategoriesFromString } from '@/category';
import { getOrderedCategoriesFromString } from '@/category';
import type { StorageType } from '@/platforms/storage';
import { makeUrlAbsolute, shortenUrl } from '@/utility/url';
@ -60,9 +60,9 @@ export const IS_PRODUCTION = process.env.NODE_ENV === 'production' && (
VERCEL_ENV === 'production' ||
!VERCEL_ENV
);
export const IS_DEVELOPMENT = process.env.NODE_ENV === 'development';
export const IS_PREVIEW = VERCEL_ENV === 'preview';
export const IS_BUILDING = process.env.NEXT_PHASE === 'phase-production-build';
export const VERCEL_BYPASS_KEY = 'x-vercel-protection-bypass';
export const VERCEL_BYPASS_SECRET = process.env.VERCEL_AUTOMATION_BYPASS_SECRET;
@ -185,16 +185,6 @@ export const HAS_STATIC_OPTIMIZATION =
STATICALLY_OPTIMIZED_PHOTO_CATEGORIES ||
STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES;
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 PRESERVE_ORIGINAL_UPLOADS =
process.env.NEXT_PUBLIC_PRESERVE_ORIGINAL_UPLOADS === '1' ||
// Legacy environment variable name
@ -270,7 +260,9 @@ export const OG_TEXT_BOTTOM_ALIGNMENT =
export const ADMIN_DEBUG_TOOLS_ENABLED = process.env.ADMIN_DEBUG_TOOLS === '1';
export const ADMIN_DB_OPTIMIZE_ENABLED = process.env.ADMIN_DB_OPTIMIZE === '1';
export const ADMIN_SQL_DEBUG_ENABLED = process.env.ADMIN_SQL_DEBUG === '1';
export const ADMIN_SQL_DEBUG_ENABLED =
process.env.ADMIN_SQL_DEBUG === '1' &&
!IS_BUILDING;
export const APP_CONFIGURATION = {
// Storage

39
src/category/server.ts Normal file
View File

@ -0,0 +1,39 @@
import { CategoryKey } from '.';
import {
CATEGORY_VISIBILITY,
IS_BUILDING,
IS_PRODUCTION,
STATICALLY_OPTIMIZED_PHOTO_CATEGORIES,
STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES,
} from '@/app/config';
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
import { pluralize } from '@/utility/string';
type StaticOutput = 'page' | 'image';
export const shouldGenerateStaticParamsForCategory = (
key: CategoryKey,
type: StaticOutput,
): boolean =>
CATEGORY_VISIBILITY.includes(key) &&
IS_PRODUCTION && (
(type === 'page' && STATICALLY_OPTIMIZED_PHOTO_CATEGORIES) ||
(type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES)
);
export const staticallyGenerateCategory = async <T, K>(
key: CategoryKey,
type: StaticOutput,
getData: () => Promise<T[]>,
formatData: (data: T[]) => K[],
): Promise<K[]> => {
const data = (await getData()).slice(0, GENERATE_STATIC_PARAMS_LIMIT);
if (IS_BUILDING) {
console.log(
`Statically generating ${key} (${pluralize(data.length, type)})`,
);
}
return formatData(data);
};