perf: drop redundant JSON.parse(JSON.stringify()) in recipe API

Every recipe list endpoint wrapped its result in
`JSON.parse(JSON.stringify(...))` before handing it to `json()`, which
then serialises again — a full extra stringify+parse cycle per response.
`lean()` already returns plain objects and ObjectIds/Dates serialise
correctly through `json()`'s single `JSON.stringify`, so the extra round
trip was pure waste.

Removed from the 9 output-side call sites (all_brief, category,
category/[cat], tag, tag/[tag], icon, icon/[icon], in_season/[month],
search, favorites/recipes, offline-db, translate/untranslated).
Kept the two deep-clone-before-mutation usages in items/[name] and
json-ld/[name] — those are load-bearing.

Shuffle stays server-side: moving it to the client would need a hero
preload + hydration rework that's bigger than a perf tweak.
This commit is contained in:
2026-04-23 15:00:37 +02:00
parent 3b4318206d
commit eb3604f9ea
14 changed files with 17 additions and 18 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ Order = impact. Font items + app.html preload intentionally skipped.
- [x] 1. Lucide subpath imports — convert `from '@lucide/svelte'` barrel imports to `@lucide/svelte/icons/<kebab-name>` so Vite tree-shakes per-icon (current 748 KB shared chunk)
- [x] 2. Chart.js dynamic import in `FitnessChart.svelte` (drop 244 KB from non-stats fitness routes)
- [ ] 3. Recipe `all_brief` endpoint — drop `JSON.parse(JSON.stringify(...))`, move shuffle client-side, enable caching
- [x] 3. Recipe API endpoints — drop `JSON.parse(JSON.stringify(...))` double-serialize (9 endpoints). Client-side shuffle / cache headers deferred (would require rethinking hero preload + hydration)
- [ ] 4. Favorites page — drop unnecessary `all_brief` fetch (verify consumer first)
- [ ] 5. Replace redundant `locals.auth()` with `locals.session` across recipe/calendar/fitness loaders
- [ ] 6. Stream fitness stats loader — return promises for slow panels
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "homepage",
"version": "1.46.14",
"version": "1.46.15",
"private": true,
"type": "module",
"scripts": {
@@ -65,10 +65,10 @@ export const GET: RequestHandler = async ({ params, locals }) => {
germanShortName: recipe.short_name,
translationStatus: t?.translationStatus
}});
return json(JSON.parse(JSON.stringify(englishRecipes)));
return json(englishRecipes);
}
return json(JSON.parse(JSON.stringify(recipes)));
return json(recipes);
} catch (e) {
throw error(500, 'Failed to fetch favorite recipes');
}
@@ -11,5 +11,5 @@ export const GET: RequestHandler = async ({ params }) => {
const dbRecipes = await Recipe.find(approvalFilter, projection).lean();
const recipes = dbRecipes.map(r => toBrief(r, params.recipeLang!));
return json(JSON.parse(JSON.stringify(rand_array(recipes))));
return json(rand_array(recipes));
};
@@ -10,5 +10,5 @@ export const GET: RequestHandler = async ({ params }) => {
const field = `${prefix}category`;
const categories = await Recipe.distinct(field, approvalFilter).lean();
return json(JSON.parse(JSON.stringify(categories)));
return json(categories);
};
@@ -14,5 +14,5 @@ export const GET: RequestHandler = async ({ params }) => {
).lean();
const recipes = rand_array(dbRecipes.map(r => toBrief(r, params.recipeLang!)));
return json(JSON.parse(JSON.stringify(recipes)));
return json(recipes);
};
@@ -5,8 +5,7 @@ import type {BriefRecipeType} from '$types/types';
export const GET: RequestHandler = async ({params}) => {
await dbConnect();
let icons = (await Recipe.distinct('icon').lean());
const icons = await Recipe.distinct('icon').lean();
icons = JSON.parse(JSON.stringify(icons));
return json(icons);
};
@@ -14,5 +14,5 @@ export const GET: RequestHandler = async ({ params }) => {
).lean();
const recipes = rand_array(dbRecipes.map(r => toBrief(r, params.recipeLang!)));
return json(JSON.parse(JSON.stringify(recipes)));
return json(recipes);
};
@@ -14,5 +14,5 @@ export const GET: RequestHandler = async ({ params }) => {
).lean();
const recipes = dbRecipes.map(r => toBrief(r, params.recipeLang!));
return json(JSON.parse(JSON.stringify(rand_array(recipes))));
return json(rand_array(recipes));
};
@@ -15,9 +15,9 @@ export const GET: RequestHandler = async ({ params }) => {
recipe.translations.en.tags.forEach((tag: string) => tagsSet.add(tag));
}
});
return json(JSON.parse(JSON.stringify(Array.from(tagsSet).sort())));
return json(Array.from(tagsSet).sort());
}
const tags = await Recipe.distinct('tags').lean();
return json(JSON.parse(JSON.stringify(tags)));
return json(tags);
};
@@ -14,5 +14,5 @@ export const GET: RequestHandler = async ({ params }) => {
).lean();
const recipes = dbRecipes.map(r => toBrief(r, params.recipeLang!));
return json(JSON.parse(JSON.stringify(rand_array(recipes))));
return json(rand_array(recipes));
};
@@ -72,8 +72,8 @@ export const GET: RequestHandler = async () => {
});
return json({
brief: JSON.parse(JSON.stringify(briefRecipes)),
full: JSON.parse(JSON.stringify(processedFullRecipes)),
brief: briefRecipes,
full: processedFullRecipes,
syncedAt: new Date().toISOString()
});
};
@@ -71,7 +71,7 @@ export const GET: RequestHandler = async ({ url, params, locals }) => {
});
}
return json(JSON.parse(JSON.stringify(recipes)));
return json(recipes);
} catch (e) {
return json({ error: 'Search failed' }, { status: 500 });
}
@@ -40,7 +40,7 @@ export const GET: RequestHandler = async ({ locals }) => {
translationStatus: recipe.translations?.en?.translationStatus || undefined
}));
return json(JSON.parse(JSON.stringify(result)));
return json(result);
} catch (e) {
console.error('Error fetching untranslated recipes:', e);
throw error(500, 'Fehler beim Laden der unübersetzten Rezepte');