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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user