From 53ad393541ab97d84e8829cfb4da791acb7128de Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Thu, 29 Jan 2026 14:22:51 +0100 Subject: [PATCH] refactor: reduce all_brief payload to first image's alt and mediapath Only include the necessary image fields for Card.svelte instead of the entire images array to reduce API response size. --- src/routes/api/recipes/items/all_brief/+server.ts | 5 ++++- src/routes/api/rezepte/items/all_brief/+server.ts | 10 +++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/routes/api/recipes/items/all_brief/+server.ts b/src/routes/api/recipes/items/all_brief/+server.ts index 481d26d..295472e 100644 --- a/src/routes/api/recipes/items/all_brief/+server.ts +++ b/src/routes/api/recipes/items/all_brief/+server.ts @@ -14,6 +14,7 @@ export const GET: RequestHandler = async ({params}) => { ).lean(); // Map to brief format with English data + // Only include first image's alt and mediapath to reduce payload const found_brief = recipes.map((recipe: any) => ({ _id: recipe._id, name: recipe.translations.en.name, @@ -25,7 +26,9 @@ export const GET: RequestHandler = async ({params}) => { season: recipe.season || [], dateModified: recipe.dateModified, germanShortName: recipe.short_name, // For language switcher - images: recipe.images || [] + images: recipe.images?.[0] + ? [{ alt: recipe.images[0].alt, mediapath: recipe.images[0].mediapath }] + : [] })) as BriefRecipeType[]; return json(JSON.parse(JSON.stringify(rand_array(found_brief)))); diff --git a/src/routes/api/rezepte/items/all_brief/+server.ts b/src/routes/api/rezepte/items/all_brief/+server.ts index 594f1ca..4ff2e08 100644 --- a/src/routes/api/rezepte/items/all_brief/+server.ts +++ b/src/routes/api/rezepte/items/all_brief/+server.ts @@ -17,7 +17,15 @@ export const GET: RequestHandler = async ({params}) => { } else { // Cache miss - fetch from DB await dbConnect(); - recipes = await Recipe.find({}, 'name short_name tags category icon description season dateModified images').lean() as BriefRecipeType[]; + const dbRecipes = await Recipe.find({}, 'name short_name tags category icon description season dateModified images').lean() as BriefRecipeType[]; + + // Only include first image's alt and mediapath to reduce payload + recipes = dbRecipes.map(recipe => ({ + ...recipe, + images: recipe.images?.[0] + ? [{ alt: recipe.images[0].alt, mediapath: recipe.images[0].mediapath }] + : [] + })) as BriefRecipeType[]; // Store in cache (1 hour TTL) await cache.set(cacheKey, JSON.stringify(recipes), 3600);