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