Refactor cache, auth, storage dependencies/file structure

This commit is contained in:
Sam Becker 2024-02-09 13:30:35 -06:00
parent 8a89e07c42
commit 93119ba66f
51 changed files with 104 additions and 103 deletions

View File

@ -1,4 +1,4 @@
import { authCached } from '@/cache';
import { authCached } from '@/auth/cache';
import AdminPhotoMenuClient, { AdminPhotoMenuClientProps }
from './AdminPhotoMenuClient';

View File

@ -1,9 +1,9 @@
import AdminNav from '@/admin/AdminNav';
import {
getStorageUploadUrlsNoStore,
getPhotosCountIncludingHiddenCached,
getUniqueTagsCached,
} from '@/cache';
} from '@/photo/cache';
import { getStorageUploadUrlsNoStore } from '@/services/storage/cache';
import {
PATH_ADMIN_PHOTOS,
PATH_ADMIN_TAGS,

View File

@ -1,5 +1,5 @@
import { redirect } from 'next/navigation';
import { getPhotoNoStore, getUniqueTagsCached } from '@/cache';
import { getPhotoNoStore, getUniqueTagsCached } from '@/photo/cache';
import { PATH_ADMIN } from '@/site/paths';
import PhotoEditPageClient from '@/photo/PhotoEditPageClient';

View File

@ -14,10 +14,9 @@ import {
import { titleForPhoto } from '@/photo';
import MorePhotos from '@/photo/MorePhotos';
import {
getStoragePhotoUrlsNoStore,
getPhotosCached,
getPhotosCountIncludingHiddenCached,
} from '@/cache';
} from '@/photo/cache';
import { AiOutlineEyeInvisible } from 'react-icons/ai';
import {
PaginationParams,
@ -30,6 +29,7 @@ import StorageUrls from '@/admin/StorageUrls';
import { PRO_MODE_ENABLED } from '@/site/config';
import SubmitButtonWithStatus from '@/components/SubmitButtonWithStatus';
import IconGrSync from '@/site/IconGrSync';
import { getStoragePhotoUrlsNoStore } from '@/services/storage/cache';
const DEBUG_PHOTO_BLOBS = false;

View File

@ -1,6 +1,6 @@
import AdminChildPage from '@/components/AdminChildPage';
import { redirect } from 'next/navigation';
import { getPhotosCached, getPhotosTagCountCached } from '@/cache';
import { getPhotosCached, getPhotosTagCountCached } from '@/photo/cache';
import TagForm from '@/tag/TagForm';
import { PATH_ADMIN, PATH_ADMIN_TAGS, pathForTag } from '@/site/paths';
import PhotoTag from '@/tag/PhotoTag';

View File

@ -5,7 +5,7 @@ import AdminGrid from '@/admin/AdminGrid';
import { Fragment } from 'react';
import DeleteButton from '@/admin/DeleteButton';
import { photoQuantityText } from '@/photo';
import { getUniqueTagsHiddenCached } from '@/cache';
import { getUniqueTagsHiddenCached } from '@/photo/cache';
import PhotoTag from '@/tag/PhotoTag';
import { formatTag, isTagFavs, sortTagsObject } from '@/tag';
import EditButton from '@/admin/EditButton';

View File

@ -3,7 +3,7 @@ import AdminChildPage from '@/components/AdminChildPage';
import { PATH_ADMIN, PATH_ADMIN_UPLOADS } from '@/site/paths';
import { extractExifDataFromBlobPath } from '@/photo/server';
import { redirect } from 'next/navigation';
import { getUniqueTagsCached } from '@/cache';
import { getUniqueTagsCached } from '@/photo/cache';
interface Params {
params: { uploadPath: string }

View File

@ -1,5 +1,5 @@
import StorageUrls from '@/admin/StorageUrls';
import { getStorageUploadUrlsNoStore } from '@/cache';
import { getStorageUploadUrlsNoStore } from '@/services/storage/cache';
import SiteGrid from '@/components/SiteGrid';
export default async function AdminUploadsPage() {

View File

@ -1,4 +1,4 @@
import { getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import { API_PHOTO_REQUEST_LIMIT, formatPhotoForApi } from '@/site/api';
import {
BASE_URL,

View File

@ -1,5 +1,5 @@
import { auth } from '@/auth';
import { revalidateAdminPaths, revalidatePhotosKey } from '@/cache';
import { revalidateAdminPaths, revalidatePhotosKey } from '@/photo/cache';
import {
ACCEPTED_PHOTO_FILE_TYPES,
MAX_PHOTO_UPLOAD_SIZE_IN_BYTES,

View File

@ -10,7 +10,7 @@ import {
absolutePathForPhotoImage,
} from '@/site/paths';
import PhotoDetailPage from '@/photo/PhotoDetailPage';
import { getPhotoCached } from '@/cache';
import { getPhotoCached } from '@/photo/cache';
import { ReactNode } from 'react';
import { FilmSimulation } from '@/simulation';
import { getPhotosFilmSimulationDataCached } from '@/simulation/data';

View File

@ -1,4 +1,4 @@
import { getPhotoCached } from '@/cache';
import { getPhotoCached } from '@/photo/cache';
import PhotoShareModal from '@/photo/PhotoShareModal';
import { FilmSimulation } from '@/simulation';
import { PATH_ROOT } from '@/site/paths';

View File

@ -1,14 +1,14 @@
import { auth } from '@/auth';
import { getImageCacheHeadersForAuth, getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import {
IMAGE_OG_DIMENSION_SMALL,
MAX_PHOTOS_TO_SHOW_PER_TAG,
} from '@/photo/image-response';
} from '@/image-response';
import FilmSimulationImageResponse from
'@/photo/image-response/FilmSimulationImageResponse';
'@/image-response/FilmSimulationImageResponse';
import { FilmSimulation } from '@/simulation';
import { getIBMPlexMonoMedium } from '@/site/font';
import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
export const runtime = 'edge';
@ -25,7 +25,7 @@ export async function GET(
] = await Promise.all([
getPhotosCached({ limit: MAX_PHOTOS_TO_SHOW_PER_TAG, simulation }),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
getImageResponseCacheControlHeaders(),
]);
const { width, height } = IMAGE_OG_DIMENSION_SMALL;

View File

@ -1,9 +1,9 @@
import { getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import SiteGrid from '@/components/SiteGrid';
import { generateOgImageMetaForPhotos } from '@/photo';
import PhotoGrid from '@/photo/PhotoGrid';
import PhotosEmptyState from '@/photo/PhotosEmptyState';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/photo/image-response';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/image-response';
import { pathForGrid } from '@/site/paths';
import { Metadata } from 'next';
import {

View File

@ -1,12 +1,12 @@
import { auth } from '@/auth';
import { getImageCacheHeadersForAuth, getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import {
IMAGE_OG_DIMENSION_SMALL,
MAX_PHOTOS_TO_SHOW_OG,
} from '@/photo/image-response';
import HomeImageResponse from '@/photo/image-response/HomeImageResponse';
} from '@/image-response';
import HomeImageResponse from '@/image-response/HomeImageResponse';
import { getIBMPlexMonoMedium } from '@/site/font';
import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
export const runtime = 'edge';
@ -17,7 +17,7 @@ export async function GET() {
{ fontFamily, fonts },
] = await Promise.all([
getPhotosCached({ limit: MAX_PHOTOS_TO_SHOW_OG }),
getImageCacheHeadersForAuth(await auth()),
getImageResponseCacheControlHeaders(),
getIBMPlexMonoMedium(),
]);

View File

@ -1,4 +1,4 @@
import { getPhotosCached, getPhotosCountCached } from '@/cache';
import { getPhotosCached, getPhotosCountCached } from '@/photo/cache';
import MorePhotos from '@/photo/MorePhotos';
import StaggeredOgPhotos from '@/photo/StaggeredOgPhotos';
import {

View File

@ -1,9 +1,9 @@
import { auth } from '@/auth';
import { getImageCacheHeadersForAuth, getPhotoCached } from '@/cache';
import { IMAGE_OG_DIMENSION } from '@/photo/image-response';
import PhotoImageResponse from '@/photo/image-response/PhotoImageResponse';
import { getPhotoCached } from '@/photo/cache';
import { IMAGE_OG_DIMENSION } from '@/image-response';
import PhotoImageResponse from '@/image-response/PhotoImageResponse';
import { getIBMPlexMonoMedium } from '@/site/font';
import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
export const runtime = 'edge';
@ -18,7 +18,7 @@ export async function GET(
] = await Promise.all([
getPhotoCached(context.params.photoId),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
getImageResponseCacheControlHeaders(),
]);
if (!photo) { return new Response('Photo not found', { status: 404 }); }

View File

@ -11,7 +11,7 @@ import {
absolutePathForPhotoImage,
} from '@/site/paths';
import PhotoDetailPage from '@/photo/PhotoDetailPage';
import { getPhotoCached, getPhotosNearIdCached } from '@/cache';
import { getPhotoCached, getPhotosNearIdCached } from '@/photo/cache';
export const runtime = 'edge';

View File

@ -1,4 +1,4 @@
import { getPhotoCached } from '@/cache';
import { getPhotoCached } from '@/photo/cache';
import PhotoShareModal from '@/photo/PhotoShareModal';
import { PATH_ROOT } from '@/site/paths';
import { redirect } from 'next/navigation';

View File

@ -1,4 +1,4 @@
import { getPhotosCached, getPhotosCountCached } from '@/cache';
import { getPhotosCached, getPhotosCountCached } from '@/photo/cache';
import AnimateItems from '@/components/AnimateItems';
import MorePhotos from '@/photo/MorePhotos';
import SiteGrid from '@/components/SiteGrid';
@ -11,7 +11,7 @@ import {
} from '@/site/pagination';
import { pathForRoot } from '@/site/paths';
import { Metadata } from 'next';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/photo/image-response';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/image-response';
export const runtime = 'edge';

View File

@ -1,11 +1,11 @@
import { getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import InfoBlock from '@/components/InfoBlock';
import RedirectOnDesktop from '@/components/RedirectOnDesktop';
import SiteGrid from '@/components/SiteGrid';
import { generateOgImageMetaForPhotos } from '@/photo';
import PhotoGridSidebar from '@/photo/PhotoGridSidebar';
import { getPhotoSidebarDataCached } from '@/photo/data';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/photo/image-response';
import { MAX_PHOTOS_TO_SHOW_OG } from '@/image-response';
import { PATH_GRID } from '@/site/paths';
import { Metadata } from 'next';

View File

@ -10,7 +10,7 @@ import {
absolutePathForPhotoImage,
} from '@/site/paths';
import PhotoDetailPage from '@/photo/PhotoDetailPage';
import { getPhotoCached } from '@/cache';
import { getPhotoCached } from '@/photo/cache';
import { cameraFromPhoto } from '@/camera';
import { getPhotosCameraDataCached } from '@/camera/data';
import { ReactNode } from 'react';

View File

@ -1,4 +1,4 @@
import { getPhotoCached } from '@/cache';
import { getPhotoCached } from '@/photo/cache';
import { cameraFromPhoto } from '@/camera';
import PhotoShareModal from '@/photo/PhotoShareModal';
import { PATH_ROOT } from '@/site/paths';

View File

@ -1,13 +1,13 @@
import { auth } from '@/auth';
import { getImageCacheHeadersForAuth, getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import { getCameraFromKey } from '@/camera';
import {
IMAGE_OG_DIMENSION_SMALL,
MAX_PHOTOS_TO_SHOW_PER_TAG,
} from '@/photo/image-response';
import CameraImageResponse from '@/photo/image-response/CameraImageResponse';
} from '@/image-response';
import CameraImageResponse from '@/image-response/CameraImageResponse';
import { getIBMPlexMonoMedium } from '@/site/font';
import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
export const runtime = 'edge';
@ -27,7 +27,7 @@ export async function GET(
camera: camera,
}),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
getImageResponseCacheControlHeaders(),
]);
const { width, height } = IMAGE_OG_DIMENSION_SMALL;

View File

@ -10,7 +10,7 @@ import {
absolutePathForPhotoImage,
} from '@/site/paths';
import PhotoDetailPage from '@/photo/PhotoDetailPage';
import { getPhotoCached } from '@/cache';
import { getPhotoCached } from '@/photo/cache';
import { getPhotosTagDataCached } from '@/tag/data';
import { ReactNode } from 'react';

View File

@ -1,4 +1,4 @@
import { getPhotoCached } from '@/cache';
import { getPhotoCached } from '@/photo/cache';
import PhotoShareModal from '@/photo/PhotoShareModal';
import { PATH_ROOT } from '@/site/paths';
import { redirect } from 'next/navigation';

View File

@ -1,12 +1,12 @@
import { auth } from '@/auth';
import { getImageCacheHeadersForAuth, getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import {
IMAGE_OG_DIMENSION_SMALL,
MAX_PHOTOS_TO_SHOW_PER_TAG,
} from '@/photo/image-response';
import TagImageResponse from '@/photo/image-response/TagImageResponse';
} from '@/image-response';
import TagImageResponse from '@/image-response/TagImageResponse';
import { getIBMPlexMonoMedium } from '@/site/font';
import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
export const runtime = 'edge';
@ -23,7 +23,7 @@ export async function GET(
] = await Promise.all([
getPhotosCached({ limit: MAX_PHOTOS_TO_SHOW_PER_TAG, tag }),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
getImageResponseCacheControlHeaders(),
]);
const { width, height } = IMAGE_OG_DIMENSION_SMALL;

View File

@ -1,13 +1,13 @@
import { auth } from '@/auth';
import { getImageCacheHeadersForAuth, getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import {
IMAGE_OG_DIMENSION,
MAX_PHOTOS_TO_SHOW_TEMPLATE_TIGHT,
} from '@/photo/image-response';
} from '@/image-response';
import TemplateImageResponse from
'@/photo/image-response/TemplateImageResponse';
'@/image-response/TemplateImageResponse';
import { getIBMPlexMonoMedium } from '@/site/font';
import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
export const runtime = 'edge';
@ -22,7 +22,7 @@ export async function GET() {
limit: MAX_PHOTOS_TO_SHOW_TEMPLATE_TIGHT,
}),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
getImageResponseCacheControlHeaders(),
]);
const { width, height } = IMAGE_OG_DIMENSION;

View File

@ -1,13 +1,13 @@
import { auth } from '@/auth';
import { getImageCacheHeadersForAuth, getPhotosCached } from '@/cache';
import { getPhotosCached } from '@/photo/cache';
import {
GRID_OG_DIMENSION,
MAX_PHOTOS_TO_SHOW_TEMPLATE,
} from '@/photo/image-response';
} from '@/image-response';
import TemplateImageResponse from
'@/photo/image-response/TemplateImageResponse';
'@/image-response/TemplateImageResponse';
import { getIBMPlexMonoMedium } from '@/site/font';
import { ImageResponse } from 'next/og';
import { getImageResponseCacheControlHeaders } from '@/image-response/cache';
export const runtime = 'edge';
@ -19,7 +19,7 @@ export async function GET() {
] = await Promise.all([
getPhotosCached({ sortBy: 'priority', limit: MAX_PHOTOS_TO_SHOW_TEMPLATE }),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
getImageResponseCacheControlHeaders(),
]);
const { width, height } = GRID_OG_DIMENSION;

4
src/auth/cache.ts Normal file
View File

@ -0,0 +1,4 @@
import { cache } from 'react';
import { auth } from '@/auth';
export const authCached = cache(auth);

View File

@ -7,7 +7,7 @@ import {
getPhotosCached,
getPhotosCameraCountCached,
getPhotosCameraDateRangeCached,
} from '@/cache';
} from '@/photo/cache';
import { pathForCamera } from '@/site/paths';
export const getPhotosCameraDataCached = ({

View File

@ -5,7 +5,7 @@ import { clsx } from 'clsx/lite';
import Link from 'next/link';
import { BiError } from 'react-icons/bi';
import Spinner from '@/components/Spinner';
import { IMAGE_OG_DIMENSION } from '../photo/image-response';
import { IMAGE_OG_DIMENSION } from '../image-response';
export type OGLoadingState = 'unloaded' | 'loading' | 'loaded' | 'failed';

View File

@ -1,4 +1,4 @@
import { Photo } from '..';
import { Photo } from '../photo';
import ImageCaption from './components/ImageCaption';
import ImagePhotoGrid from './components/ImagePhotoGrid';
import ImageContainer from './components/ImageContainer';

View File

@ -1,4 +1,4 @@
import { Photo } from '..';
import { Photo } from '../photo';
import ImageCaption from './components/ImageCaption';
import ImagePhotoGrid from './components/ImagePhotoGrid';
import ImageContainer from './components/ImageContainer';

View File

@ -1,5 +1,5 @@
import { SITE_DOMAIN_OR_TITLE } from '@/site/config';
import { Photo } from '..';
import { Photo } from '../photo';
import ImageCaption from './components/ImageCaption';
import ImageContainer from './components/ImageContainer';
import ImagePhotoGrid from './components/ImagePhotoGrid';

View File

@ -1,4 +1,4 @@
import { Photo, shouldShowExifDataForPhoto } from '..';
import { Photo, shouldShowExifDataForPhoto } from '../photo';
import { AiFillApple } from 'react-icons/ai';
import ImageCaption from './components/ImageCaption';
import ImagePhotoGrid from './components/ImagePhotoGrid';

View File

@ -1,4 +1,4 @@
import { Photo } from '..';
import { Photo } from '../photo';
import { FaStar, FaTag } from 'react-icons/fa';
import ImageCaption from './components/ImageCaption';
import ImagePhotoGrid from './components/ImagePhotoGrid';

View File

@ -1,4 +1,4 @@
import { Photo } from '..';
import { Photo } from '../photo';
import IconFullFrame from '@/site/IconFullFrame';
import IconGrid from '@/site/IconGrid';
import ImagePhotoGrid from './components/ImagePhotoGrid';

View File

@ -0,0 +1,9 @@
export const getImageResponseCacheControlHeaders = (
shouldCache = process.env.NEXT_PUBLIC_VERCEL_ENV === 'production',
) => {
return {
'Cache-Control': shouldCache
? 's-maxage=3600, stale-while-revalidate=59'
: 's-maxage=1, stale-while-revalidate=59',
};
};

View File

@ -22,7 +22,7 @@ import {
revalidateAdminPaths,
revalidateAllKeysAndPaths,
revalidatePhotosKey,
} from '@/cache';
} from '@/photo/cache';
import { PATH_ADMIN_PHOTOS, PATH_ADMIN_TAGS } from '@/site/paths';
import { extractExifDataFromBlobPath } from './server';
import { TAG_FAVS, isTagFavs } from '@/tag';

View File

@ -24,12 +24,8 @@ import {
getPhotosNearId,
} from '@/services/vercel-postgres';
import { parseCachedPhotoDates, parseCachedPhotosDates } from '@/photo';
import { getStoragePhotoUrls, getStorageUploadUrls } from '@/services/storage';
import type { Session } from 'next-auth';
import { createCameraKey } from '@/camera';
import { PATHS_ADMIN } from '@/site/paths';
import { cache } from 'react';
import { auth } from '@/auth';
// Table key
const KEY_PHOTOS = 'photos';
@ -209,31 +205,9 @@ export const getUniqueFilmSimulationsCached =
[KEY_PHOTOS, KEY_FILM_SIMULATIONS],
);
export const authCached = cache(auth);
// No Store
// No store
export const getPhotoNoStore = (...args: Parameters<typeof getPhoto>) => {
unstable_noStore();
return getPhoto(...args);
};
export const getStorageUploadUrlsNoStore: typeof getStorageUploadUrls =
(...args) => {
unstable_noStore();
return getStorageUploadUrls(...args);
};
export const getStoragePhotoUrlsNoStore: typeof getStoragePhotoUrls =
(...args) => {
unstable_noStore();
return getStoragePhotoUrls(...args);
};
export const getImageCacheHeadersForAuth = (session: Session | null) => {
return {
'Cache-Control': !session?.user
? 's-maxage=3600, stale-while-revalidate=59'
: 's-maxage=1, stale-while-revalidate=59',
};
};

View File

@ -3,7 +3,7 @@ import {
getUniqueCamerasCached,
getUniqueFilmSimulationsCached,
getUniqueTagsCached,
} from '@/cache';
} from '@/photo/cache';
import { SHOW_FILM_SIMULATIONS } from '@/site/config';
import { TAG_FAVS } from '@/tag';

View File

@ -0,0 +1,14 @@
import { unstable_noStore } from 'next/cache';
import { getStoragePhotoUrls, getStorageUploadUrls } from '@/services/storage';
export const getStorageUploadUrlsNoStore: typeof getStorageUploadUrls =
(...args) => {
unstable_noStore();
return getStorageUploadUrls(...args);
};
export const getStoragePhotoUrlsNoStore: typeof getStoragePhotoUrls =
(...args) => {
unstable_noStore();
return getStoragePhotoUrls(...args);
};

View File

@ -2,7 +2,7 @@ import {
getPhotosCached,
getPhotosFilmSimulationCountCached,
getPhotosFilmSimulationDateRangeCached,
} from '@/cache';
} from '@/photo/cache';
import {
PaginationSearchParams,
getPaginationForSearchParams,

View File

@ -1,4 +1,4 @@
import { authCached } from '@/cache';
import { authCached } from '@/auth/cache';
import FooterClient from './FooterClient';
export default async function Footer() {

View File

@ -1,4 +1,4 @@
import { authCached } from '@/cache';
import { authCached } from '@/auth/cache';
import NavClient from './NavClient';
export default async function Nav() {

View File

@ -2,7 +2,7 @@ import {
getPhotosCached,
getPhotosTagCountCached,
getPhotosTagDateRangeCached,
} from '@/cache';
} from '@/photo/cache';
import {
PaginationSearchParams,
getPaginationForSearchParams,