refactor: reduce all_brief payload to first image's alt and mediapath
All checks were successful
CI / update (push) Successful in 1m20s
All checks were successful
CI / update (push) Successful in 1m20s
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();
|
).lean();
|
||||||
|
|
||||||
// Map to brief format with English data
|
// 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) => ({
|
const found_brief = recipes.map((recipe: any) => ({
|
||||||
_id: recipe._id,
|
_id: recipe._id,
|
||||||
name: recipe.translations.en.name,
|
name: recipe.translations.en.name,
|
||||||
@@ -25,7 +26,9 @@ export const GET: RequestHandler = async ({params}) => {
|
|||||||
season: recipe.season || [],
|
season: recipe.season || [],
|
||||||
dateModified: recipe.dateModified,
|
dateModified: recipe.dateModified,
|
||||||
germanShortName: recipe.short_name, // For language switcher
|
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[];
|
})) as BriefRecipeType[];
|
||||||
|
|
||||||
return json(JSON.parse(JSON.stringify(rand_array(found_brief))));
|
return json(JSON.parse(JSON.stringify(rand_array(found_brief))));
|
||||||
|
|||||||
@@ -17,7 +17,15 @@ export const GET: RequestHandler = async ({params}) => {
|
|||||||
} else {
|
} else {
|
||||||
// Cache miss - fetch from DB
|
// Cache miss - fetch from DB
|
||||||
await dbConnect();
|
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)
|
// Store in cache (1 hour TTL)
|
||||||
await cache.set(cacheKey, JSON.stringify(recipes), 3600);
|
await cache.set(cacheKey, JSON.stringify(recipes), 3600);
|
||||||
|
|||||||
Reference in New Issue
Block a user