Apply Promise.all strategy wherever appropriate

This commit is contained in:
Sam Becker 2023-09-21 12:11:51 -05:00
parent 1502ec5421
commit a8f88c5d40
11 changed files with 84 additions and 68 deletions

View File

@ -37,18 +37,16 @@ export default async function AdminPage({
photos,
count,
blobUploadUrls,
blobPhotoUrls,
] = await Promise.all([
await getPhotos({ sortBy: 'createdAt', limit }),
await getPhotosCount(),
await getBlobUploadUrls(),
getPhotos({ sortBy: 'createdAt', limit }),
getPhotosCount(),
getBlobUploadUrls(),
DEBUG_PHOTO_BLOBS ? getBlobPhotoUrls() : [],
]);
const showMorePhotos = count > photos.length;
const blobPhotoUrls = DEBUG_PHOTO_BLOBS
? await getBlobPhotoUrls()
: [];
return (
<SiteGrid
contentMain={

View File

@ -27,11 +27,15 @@ export default async function GridPage({
}) {
const { offset, limit } = getPhotosLimitForQuery(searchParams.next);
const photos = await getPhotosCached({ limit });
const count = await getPhotosCountCached();
const tags = await getUniqueTagsCached();
const [
photos,
count,
tags,
] = await Promise.all([
getPhotosCached({ limit }),
getPhotosCountCached(),
getUniqueTagsCached(),
]);
const showMorePhotos = count > photos.length;

View File

@ -10,11 +10,13 @@ import { ImageResponse } from 'next/server';
export const runtime = 'edge';
export async function GET(request: Request) {
const photos = await getPhotosCached({
limit: MAX_PHOTOS_TO_SHOW_HOME,
});
const headers = await getImageCacheHeadersForAuth(await auth());
const [
photos,
headers,
] = await Promise.all([
getPhotosCached({ limit: MAX_PHOTOS_TO_SHOW_HOME }),
getImageCacheHeadersForAuth(await auth()),
]);
const { width, height } = IMAGE_OG_SMALL_SIZE;

View File

@ -12,9 +12,13 @@ export default async function GridPage({
}) {
const { offset, limit } = getPhotosLimitForQuery(searchParams.next);
const photos = await getPhotosCached({ limit });
const count = await getPhotosCountCached();
const [
photos,
count,
] = await Promise.all([
getPhotosCached({ limit }),
getPhotosCountCached(),
]);
const showMorePhotos = count > photos.length;

View File

@ -8,14 +8,15 @@ import { ImageResponse } from 'next/server';
export const runtime = 'edge';
export async function GET(request: Request, context: any){
const photo = await getPhotoCached(context.params.photoId);
const {
fontFamily,
fonts,
} = await getIBMPlexMonoMedium();
const headers = await getImageCacheHeadersForAuth(await auth());
const [
photo,
{ fontFamily, fonts },
headers,
] = await Promise.all([
getPhotoCached(context.params.photoId),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
]);
if (!photo) { return new Response('Photo not found', { status: 404 }); }

View File

@ -21,10 +21,14 @@ export default async function HomePage({
}) {
const { offset, limit } = getPhotosLimitForQuery(searchParams.next, 12);
const photos = await getPhotosCached({ limit });
const count = await getPhotosCountCached();
const [
photos,
count,
] = await Promise.all([
getPhotosCached({ limit }),
getPhotosCountCached(),
]);
const showMorePhotos = count > photos.length;
return (

View File

@ -11,17 +11,18 @@ import { ImageResponse } from 'next/server';
export const runtime = 'edge';
export async function GET(request: Request, context: any) {
const photos = await getPhotosCached({
limit: MAX_PHOTOS_TO_SHOW_PER_TAG,
tag: context.params.tag,
});
const {
fontFamily,
fonts,
} = await getIBMPlexMonoMedium();
const headers = await getImageCacheHeadersForAuth(await auth());
const [
photos,
{ fontFamily, fonts },
headers,
] = await Promise.all([
getPhotosCached({
limit: MAX_PHOTOS_TO_SHOW_PER_TAG,
tag: context.params.tag,
}),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
]);
const { width, height } = IMAGE_OG_SMALL_SIZE;

View File

@ -12,17 +12,18 @@ import { ImageResponse } from 'next/server';
export const runtime = 'edge';
export async function GET(request: Request) {
const photos = await getPhotosCached({
sortBy: 'priority',
limit: MAX_PHOTOS_TO_SHOW_TEMPLATE_TIGHT,
});
const {
fontFamily,
fonts,
} = await getIBMPlexMonoMedium();
const headers = await getImageCacheHeadersForAuth(await auth());
const [
photos,
{ fontFamily, fonts },
headers,
] = await Promise.all([
getPhotosCached({
sortBy: 'priority',
limit: MAX_PHOTOS_TO_SHOW_TEMPLATE_TIGHT,
}),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
]);
const { width, height } = IMAGE_OG_SIZE;

View File

@ -12,17 +12,15 @@ import { ImageResponse } from 'next/server';
export const runtime = 'edge';
export async function GET(request: Request) {
const photos = await getPhotosCached({
sortBy: 'priority',
limit: MAX_PHOTOS_TO_SHOW_TEMPLATE,
});
const {
fontFamily,
fonts,
} = await getIBMPlexMonoMedium();
const headers = await getImageCacheHeadersForAuth(await auth());
const [
photos,
{ fontFamily, fonts },
headers,
] = await Promise.all([
getPhotosCached({ sortBy: 'priority', limit: MAX_PHOTOS_TO_SHOW_TEMPLATE }),
getIBMPlexMonoMedium(),
getImageCacheHeadersForAuth(await auth()),
]);
const { width, height } = GRID_OG_SIZE;

2
src/cache/index.ts vendored
View File

@ -74,7 +74,7 @@ export const getUniqueTagsCached: typeof getUniqueTags = (...args) =>
}
)();
export const getImageCacheHeadersForAuth = async (session?: Session) => {
export const getImageCacheHeadersForAuth = (session?: Session) => {
return {
'Cache-Control': !session?.user
? 's-maxage=3600, stale-while-revalidate=59'

View File

@ -23,6 +23,7 @@ export async function createPhotoAction(formData: FormData) {
);
if (updatedUrl) { photo.url = updatedUrl; }
await sqlInsertPhotoIntoDb(photo);
revalidatePhotosTag();
@ -41,8 +42,10 @@ export async function updatePhotoAction(formData: FormData) {
}
export async function deletePhotoAction(formData: FormData) {
await deleteBlobPhoto(formData.get('url') as string);
await sqlDeletePhoto(formData.get('id') as string);
await Promise.all([
deleteBlobPhoto(formData.get('url') as string),
sqlDeletePhoto(formData.get('id') as string),
]);
revalidatePhotosTag();
};