Refactor/standardize core photo static generation
This commit is contained in:
parent
9b27a0be08
commit
1796e0a24f
@ -4,23 +4,12 @@ import PhotoImageResponse from '@/image-response/PhotoImageResponse';
|
||||
import { getIBMPlexMono } from '@/app/font';
|
||||
import { ImageResponse } from 'next/og';
|
||||
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
|
||||
import {
|
||||
IS_PRODUCTION,
|
||||
STATICALLY_OPTIMIZED_PHOTO_OG_IMAGES,
|
||||
} from '@/app/config';
|
||||
import { getPhotoIds } from '@/photo/db/query';
|
||||
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
|
||||
import { isNextImageReadyBasedOnPhotos } from '@/photo';
|
||||
import { staticallyGeneratePhotosIfConfigured } from '@/category/server';
|
||||
|
||||
export let generateStaticParams:
|
||||
(() => Promise<{ photoId: string }[]>) | undefined = undefined;
|
||||
|
||||
if (STATICALLY_OPTIMIZED_PHOTO_OG_IMAGES && IS_PRODUCTION) {
|
||||
generateStaticParams = async () => {
|
||||
const photos = await getPhotoIds({ limit: GENERATE_STATIC_PARAMS_LIMIT });
|
||||
return photos.map(photoId => ({ photoId }));
|
||||
};
|
||||
}
|
||||
export const generateStaticParams = staticallyGeneratePhotosIfConfigured(
|
||||
'image',
|
||||
);
|
||||
|
||||
export async function GET(
|
||||
_: Request,
|
||||
|
||||
@ -12,25 +12,17 @@ import {
|
||||
} from '@/app/paths';
|
||||
import PhotoDetailPage from '@/photo/PhotoDetailPage';
|
||||
import { getPhotosNearIdCached } from '@/photo/cache';
|
||||
import { IS_PRODUCTION, STATICALLY_OPTIMIZED_PHOTOS } from '@/app/config';
|
||||
import { getPhotoIds } from '@/photo/db/query';
|
||||
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
|
||||
import { cache } from 'react';
|
||||
import { staticallyGeneratePhotosIfConfigured } from '@/category/server';
|
||||
|
||||
export const maxDuration = 60;
|
||||
|
||||
const getPhotosNearIdCachedCached = cache((photoId: string) =>
|
||||
getPhotosNearIdCached(photoId, { limit: RELATED_GRID_PHOTOS_TO_SHOW + 2 }));
|
||||
|
||||
export let generateStaticParams:
|
||||
(() => Promise<{ photoId: string }[]>) | undefined = undefined;
|
||||
|
||||
if (STATICALLY_OPTIMIZED_PHOTOS && IS_PRODUCTION) {
|
||||
generateStaticParams = async () => {
|
||||
const photos = await getPhotoIds({ limit: GENERATE_STATIC_PARAMS_LIMIT });
|
||||
return photos.map(photoId => ({ photoId }));
|
||||
};
|
||||
}
|
||||
export const generateStaticParams = staticallyGeneratePhotosIfConfigured(
|
||||
'page',
|
||||
);
|
||||
|
||||
interface PhotoProps {
|
||||
params: Promise<{ photoId: string }>
|
||||
|
||||
@ -7,19 +7,27 @@ import {
|
||||
STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES,
|
||||
} from '@/app/config';
|
||||
import { GENERATE_STATIC_PARAMS_LIMIT } from '@/photo/db';
|
||||
import { getPhotoIds } from '@/photo/db/query';
|
||||
import { depluralize, pluralize } from '@/utility/string';
|
||||
|
||||
type StaticOutput = 'page' | 'image';
|
||||
|
||||
const shouldGenerateStaticParamsForCategory = (
|
||||
key: CategoryKey,
|
||||
type: StaticOutput,
|
||||
): boolean =>
|
||||
CATEGORY_VISIBILITY.includes(key) &&
|
||||
const logStaticGenerationDetails = (count: number, content: string) =>
|
||||
console.log(`Statically generating ${pluralize(count, content)} ...`);
|
||||
|
||||
export const staticallyGeneratePhotosIfConfigured = (type: StaticOutput) =>
|
||||
IS_PRODUCTION && (
|
||||
(type === 'page' && STATICALLY_OPTIMIZED_PHOTO_CATEGORIES) ||
|
||||
(type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES)
|
||||
);
|
||||
)
|
||||
? async () => {
|
||||
const photos = await getPhotoIds({ limit: GENERATE_STATIC_PARAMS_LIMIT });
|
||||
if (IS_BUILDING) {
|
||||
logStaticGenerationDetails(photos.length, `photo ${type}`);
|
||||
}
|
||||
return photos.map(photoId => ({ photoId }));
|
||||
}
|
||||
: undefined;
|
||||
|
||||
export const staticallyGenerateCategoryIfConfigured = <T, K>(
|
||||
key: CategoryKey,
|
||||
@ -27,15 +35,19 @@ export const staticallyGenerateCategoryIfConfigured = <T, K>(
|
||||
getData: () => Promise<T[]>,
|
||||
formatData: (data: T[]) => K[],
|
||||
): (() => Promise<K[]>) | undefined =>
|
||||
shouldGenerateStaticParamsForCategory(key, type)
|
||||
CATEGORY_VISIBILITY.includes(key) &&
|
||||
IS_PRODUCTION && (
|
||||
(type === 'page' && STATICALLY_OPTIMIZED_PHOTO_CATEGORIES) ||
|
||||
(type === 'image' && STATICALLY_OPTIMIZED_PHOTO_CATEGORY_OG_IMAGES)
|
||||
)
|
||||
? 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}`);
|
||||
logStaticGenerationDetails(
|
||||
data.length,
|
||||
`${depluralize(key)} ${type}`,
|
||||
);
|
||||
}
|
||||
|
||||
return formatData(data);
|
||||
}
|
||||
: undefined;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user