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.
This commit is contained in:
2026-01-29 14:22:51 +01:00
parent ec02e8873e
commit fc8b2c1204
2 changed files with 13 additions and 2 deletions
@@ -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))));
@@ -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);