From 17626295964244c5582806bd0f413da2c799d5ad Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Fri, 4 Apr 2025 11:38:09 +0200 Subject: [PATCH] perf: run async operations in parallel in server load functions --- frontend/src/routes/+layout.server.ts | 28 +++++++++---------- .../routes/settings/account/+page.server.ts | 13 ++++++--- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/frontend/src/routes/+layout.server.ts b/frontend/src/routes/+layout.server.ts index c39ba63f..76143893 100644 --- a/frontend/src/routes/+layout.server.ts +++ b/frontend/src/routes/+layout.server.ts @@ -4,23 +4,21 @@ import UserService from '$lib/services/user-service'; import type { LayoutServerLoad } from './$types'; export const load: LayoutServerLoad = async ({ cookies }) => { - const userService = new UserService(cookies.get(ACCESS_TOKEN_COOKIE_NAME)); - const appConfigService = new AppConfigService(cookies.get(ACCESS_TOKEN_COOKIE_NAME)); + const accessToken = cookies.get(ACCESS_TOKEN_COOKIE_NAME); + const userService = new UserService(accessToken); + const appConfigService = new AppConfigService(accessToken); - const user = await userService - .getCurrent() - .then((user) => user) - .catch(() => null); + const userPromise = userService.getCurrent().catch(() => null); + + const appConfigPromise = appConfigService.list().catch((e) => { + console.error( + `Failed to get application configuration: ${e.response?.data.error || e.message}` + ); + return null; + }); + + const [user, appConfig] = await Promise.all([userPromise, appConfigPromise]); - const appConfig = await appConfigService - .list() - .then((config) => config) - .catch((e) => { - console.error( - `Failed to get application configuration: ${e.response?.data.error || e.message}` - ); - return null; - }); return { user, appConfig diff --git a/frontend/src/routes/settings/account/+page.server.ts b/frontend/src/routes/settings/account/+page.server.ts index b6e0465e..0fe07cff 100644 --- a/frontend/src/routes/settings/account/+page.server.ts +++ b/frontend/src/routes/settings/account/+page.server.ts @@ -4,10 +4,15 @@ import WebAuthnService from '$lib/services/webauthn-service'; import type { PageServerLoad } from './$types'; export const load: PageServerLoad = async ({ cookies }) => { - const webauthnService = new WebAuthnService(cookies.get(ACCESS_TOKEN_COOKIE_NAME)); - const userService = new UserService(cookies.get(ACCESS_TOKEN_COOKIE_NAME)); - const account = await userService.getCurrent(); - const passkeys = await webauthnService.listCredentials(); + const accessToken = cookies.get(ACCESS_TOKEN_COOKIE_NAME); + const webauthnService = new WebAuthnService(accessToken); + const userService = new UserService(accessToken); + + const [account, passkeys] = await Promise.all([ + userService.getCurrent(), + webauthnService.listCredentials() + ]); + return { account, passkeys