perf: drop all_brief fetch from favorites page

Favorites page fetched both /favorites/recipes and /items/all_brief, then
stitched isFavorite flags onto allRecipes so Search could filter across
the full catalogue. But Search is invoked with favoritesOnly={true} and
hideFavoritesFilter (so it's pinned on), so it only ever returns matches
that are already in the favorites list — allRecipes was dead weight.

- drop allRes / allRecipes / favoriteIds / allRecipesWithFavorites
- Search now receives data.favorites directly
- filteredFavorites filters data.favorites by matchedRecipeIds
- use locals.session ?? locals.auth() to reuse the hook's auth lookup
This commit is contained in:
2026-04-23 15:03:39 +02:00
parent eb3604f9ea
commit dfeeeb5fdf
4 changed files with 10 additions and 29 deletions
@@ -3,7 +3,7 @@ import { redirect } from '@sveltejs/kit';
export const load: PageServerLoad = async ({ fetch, locals, params }) => {
const apiBase = `/api/${params.recipeLang}`;
const session = await locals.auth();
const session = locals.session ?? await locals.auth();
if (!session?.user?.nickname) {
const callbackUrl = encodeURIComponent(`/${params.recipeLang}/favorites`);
@@ -11,44 +11,25 @@ export const load: PageServerLoad = async ({ fetch, locals, params }) => {
}
try {
const [res, allRes] = await Promise.all([
fetch(`${apiBase}/favorites/recipes`),
fetch(`${apiBase}/items/all_brief`)
]);
const res = await fetch(`${apiBase}/favorites/recipes`);
if (!res.ok) {
return {
favorites: [],
allRecipes: [],
error: 'Failed to load favorites'
};
}
const [favorites, allRecipes] = await Promise.all([res.json(), allRes.json()]);
// Mark all favorites with isFavorite flag for filter compatibility
const favoritesWithFlag = favorites.map((recipe: any) => ({
...recipe,
isFavorite: true
}));
// Get favorite IDs for marking in allRecipes
const favoriteIds = new Set(favoritesWithFlag.map((r: any) => r._id));
const allRecipesWithFavorites = allRecipes.map((recipe: any) => ({
...recipe,
isFavorite: favoriteIds.has(recipe._id)
}));
const favorites = await res.json();
return {
favorites: favoritesWithFlag,
allRecipes: allRecipesWithFavorites,
favorites: favorites.map((recipe: any) => ({ ...recipe, isFavorite: true })),
session
};
} catch (e) {
return {
favorites: [],
allRecipes: [],
error: 'Failed to load favorites'
};
}
};
};
@@ -33,12 +33,12 @@
function handleSearchResults(ids: Set<string>, categories: Set<string>) {
matchedRecipeIds = ids;
hasActiveSearch = ids.size < (data.allRecipes?.length || data.favorites.length);
hasActiveSearch = ids.size < data.favorites.length;
}
const filteredFavorites = $derived.by(() => {
if (!hasActiveSearch) return data.favorites;
return data.allRecipes.filter((r: any) => matchedRecipeIds.has(r._id));
return data.favorites.filter((r: any) => matchedRecipeIds.has(r._id));
});
</script>
@@ -93,7 +93,7 @@
<p class="to-try-link"><a href="/{data.recipeLang}/to-try">{labels.toTry} &rarr;</a></p>
<Search favoritesOnly={true} lang={data.lang} recipes={data.allRecipes || data.favorites} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
<Search favoritesOnly={true} lang={data.lang} recipes={data.favorites} isLoggedIn={!!data.session?.user} onSearchResults={handleSearchResults}></Search>
{#if data.error}
<p class="empty-state">{labels.errorLoading} {data.error}</p>