perf: reuse locals.session from hook instead of re-awaiting locals.auth()
hooks.server.ts already awaits auth() once and stores the result on locals.session. In-scope loaders (recipe list + filter views, rosary, prayers, calendar — already done — and fitness stats) were awaiting locals.auth() a second time per request. Switched to the existing `locals.session ?? await locals.auth()` pattern so the hook's result is reused. Also pulls session out of Promise.all legs since it's now synchronous when the hook ran. Scope: loaders only — actions, /admin, /edit, /add intentionally skipped.
This commit is contained in:
@@ -8,7 +8,7 @@ import type { Session } from '@auth/sveltekit';
|
||||
type BriefRecipeWithFavorite = BriefRecipeType & { isFavorite: boolean };
|
||||
|
||||
export async function getUserFavorites(fetch: typeof globalThis.fetch, locals: App.Locals, recipeLang = 'rezepte'): Promise<string[]> {
|
||||
const session = await locals.auth();
|
||||
const session = locals.session ?? await locals.auth();
|
||||
|
||||
if (!session?.user?.nickname) {
|
||||
return [];
|
||||
@@ -47,10 +47,10 @@ export async function loadRecipesWithFavorites(
|
||||
recipeLoader: () => Promise<BriefRecipeType[]>,
|
||||
recipeLang = 'rezepte'
|
||||
): Promise<{ recipes: BriefRecipeWithFavorite[], session: Session | null }> {
|
||||
const [recipes, userFavorites, session] = await Promise.all([
|
||||
const session = locals.session ?? await locals.auth();
|
||||
const [recipes, userFavorites] = await Promise.all([
|
||||
recipeLoader(),
|
||||
getUserFavorites(fetch, locals, recipeLang),
|
||||
locals.auth()
|
||||
getUserFavorites(fetch, locals, recipeLang)
|
||||
]);
|
||||
|
||||
return {
|
||||
|
||||
@@ -36,7 +36,7 @@ export const load: PageServerLoad = async ({ params, url, locals, fetch }) => {
|
||||
|
||||
// Fetch angelus streak data for angelus/regina-caeli pages
|
||||
if (angelusSlugs.has(params.prayer)) {
|
||||
const session = await locals.auth();
|
||||
const session = locals.session ?? await locals.auth();
|
||||
if (session?.user?.nickname) {
|
||||
try {
|
||||
const res = await fetch('/api/glaube/angelus-streak');
|
||||
|
||||
@@ -43,7 +43,7 @@ function getMysteryForWeekday(date: Date, includeLuminous: boolean): string {
|
||||
}
|
||||
|
||||
export const load: PageServerLoad = async ({ url, fetch, locals, params }) => {
|
||||
const session = await locals.auth();
|
||||
const session = locals.session ?? await locals.auth();
|
||||
|
||||
// Read toggle/mystery state from URL search params (for no-JS progressive enhancement)
|
||||
const luminousParam = url.searchParams.get('luminous');
|
||||
|
||||
@@ -4,12 +4,11 @@ import { getUserFavorites, addFavoriteStatusToRecipes } from "$lib/server/favori
|
||||
export const load: PageServerLoad = async ({ fetch, locals, params }) => {
|
||||
const apiBase = `/api/${params.recipeLang}`;
|
||||
const currentMonth = new Date().getMonth() + 1;
|
||||
const session = locals.session ?? await locals.auth();
|
||||
|
||||
// Fetch all_brief, favorites, and session in parallel
|
||||
const [res_all_brief, userFavorites, session] = await Promise.all([
|
||||
const [res_all_brief, userFavorites] = await Promise.all([
|
||||
fetch(`${apiBase}/items/all_brief`).then(r => r.json()),
|
||||
getUserFavorites(fetch, locals, params.recipeLang),
|
||||
locals.auth()
|
||||
getUserFavorites(fetch, locals, params.recipeLang)
|
||||
]);
|
||||
|
||||
const all_brief = addFavoriteStatusToRecipes(res_all_brief, userFavorites);
|
||||
|
||||
@@ -19,8 +19,7 @@ export const load: PageServerLoad = async ({ fetch, params, locals, url }) => {
|
||||
const strippedName = stripHtmlTags(item.name);
|
||||
const strippedDescription = stripHtmlTags(item.description);
|
||||
|
||||
// Get session for user info
|
||||
const session = await locals.auth();
|
||||
const session = locals.session ?? await locals.auth();
|
||||
|
||||
return {
|
||||
item,
|
||||
|
||||
@@ -4,11 +4,11 @@ import { getUserFavorites, addFavoriteStatusToRecipes } from "$lib/server/favori
|
||||
export const load: PageServerLoad = async ({ fetch, locals, params }) => {
|
||||
const apiBase = `/api/${params.recipeLang}`;
|
||||
|
||||
const [res, allRes, userFavorites, session] = await Promise.all([
|
||||
const session = locals.session ?? await locals.auth();
|
||||
const [res, allRes, userFavorites] = await Promise.all([
|
||||
fetch(`${apiBase}/items/category/${params.category}`),
|
||||
fetch(`${apiBase}/items/all_brief`),
|
||||
getUserFavorites(fetch, locals, params.recipeLang),
|
||||
locals.auth()
|
||||
getUserFavorites(fetch, locals, params.recipeLang)
|
||||
]);
|
||||
|
||||
const [items, allRecipes] = await Promise.all([res.json(), allRes.json()]);
|
||||
|
||||
@@ -4,11 +4,11 @@ import { getUserFavorites, addFavoriteStatusToRecipes } from "$lib/server/favori
|
||||
export const load: PageServerLoad = async ({ fetch, locals, params }) => {
|
||||
const apiBase = `/api/${params.recipeLang}`;
|
||||
|
||||
const [item_season, icons, userFavorites, session] = await Promise.all([
|
||||
const session = locals.session ?? await locals.auth();
|
||||
const [item_season, icons, userFavorites] = await Promise.all([
|
||||
fetch(`${apiBase}/items/icon/` + params.icon).then(r => r.json()),
|
||||
fetch(`${apiBase}/items/icon`).then(r => r.json()),
|
||||
getUserFavorites(fetch, locals, params.recipeLang),
|
||||
locals.auth()
|
||||
getUserFavorites(fetch, locals, params.recipeLang)
|
||||
]);
|
||||
|
||||
return {
|
||||
|
||||
@@ -8,11 +8,8 @@ export const load: PageServerLoad = async ({ fetch, locals, params }) => {
|
||||
const res_season = await fetch(`${apiBase}/items/in_season/` + current_month);
|
||||
const item_season = await res_season.json();
|
||||
|
||||
// Get user favorites and session
|
||||
const [userFavorites, session] = await Promise.all([
|
||||
getUserFavorites(fetch, locals, params.recipeLang),
|
||||
locals.auth()
|
||||
]);
|
||||
const session = locals.session ?? await locals.auth();
|
||||
const userFavorites = await getUserFavorites(fetch, locals, params.recipeLang);
|
||||
|
||||
return {
|
||||
season: addFavoriteStatusToRecipes(item_season, userFavorites),
|
||||
|
||||
@@ -7,11 +7,8 @@ export const load: PageServerLoad = async ({ fetch, locals, params }) => {
|
||||
const res_season = await fetch(`${apiBase}/items/in_season/` + params.month);
|
||||
const item_season = await res_season.json();
|
||||
|
||||
// Get user favorites and session
|
||||
const [userFavorites, session] = await Promise.all([
|
||||
getUserFavorites(fetch, locals, params.recipeLang),
|
||||
locals.auth()
|
||||
]);
|
||||
const session = locals.session ?? await locals.auth();
|
||||
const userFavorites = await getUserFavorites(fetch, locals, params.recipeLang);
|
||||
|
||||
return {
|
||||
month: params.month,
|
||||
|
||||
@@ -4,11 +4,11 @@ import { getUserFavorites, addFavoriteStatusToRecipes } from "$lib/server/favori
|
||||
export const load: PageServerLoad = async ({ fetch, locals, params }) => {
|
||||
const apiBase = `/api/${params.recipeLang}`;
|
||||
|
||||
const [res_tag, allRes, userFavorites, session] = await Promise.all([
|
||||
const session = locals.session ?? await locals.auth();
|
||||
const [res_tag, allRes, userFavorites] = await Promise.all([
|
||||
fetch(`${apiBase}/items/tag/${params.tag}`),
|
||||
fetch(`${apiBase}/items/all_brief`),
|
||||
getUserFavorites(fetch, locals, params.recipeLang),
|
||||
locals.auth()
|
||||
getUserFavorites(fetch, locals, params.recipeLang)
|
||||
]);
|
||||
|
||||
const [items_tag, allRecipes] = await Promise.all([res_tag.json(), allRes.json()]);
|
||||
|
||||
@@ -4,7 +4,7 @@ import { ToTryRecipe } from '$models/ToTryRecipe';
|
||||
import { dbConnect } from '$utils/db';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals, params }) => {
|
||||
const session = await locals.auth();
|
||||
const session = locals.session ?? await locals.auth();
|
||||
|
||||
if (!session?.user) {
|
||||
const callbackUrl = encodeURIComponent(`/${params.recipeLang}/to-try`);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { PageServerLoad } from './$types';
|
||||
|
||||
export const load: PageServerLoad = async ({ fetch, locals }) => {
|
||||
const session = await locals.auth();
|
||||
const session = locals.session ?? await locals.auth();
|
||||
const [res, goalRes, heatmapRes, nutritionRes, latestRes, periodRes, sharedRes] = await Promise.all([
|
||||
fetch('/api/fitness/stats/overview'),
|
||||
fetch('/api/fitness/goal'),
|
||||
|
||||
Reference in New Issue
Block a user