From 8914cc2b0bd438c494ddc77da25d2946bac600bf Mon Sep 17 00:00:00 2001 From: isra el Date: Mon, 4 Aug 2025 08:32:46 +0300 Subject: [PATCH] chore(web): add session caching to improve performance --- web/lib/httpBrowserClient.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/web/lib/httpBrowserClient.ts b/web/lib/httpBrowserClient.ts index 381fea6..7dfce1c 100644 --- a/web/lib/httpBrowserClient.ts +++ b/web/lib/httpBrowserClient.ts @@ -5,8 +5,29 @@ const httpBrowserClient = axios.create({ baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || '', }) +// Cache for session data to reduce API calls +let sessionCache: any = null +let cacheTimestamp = 0 +const CACHE_DURATION = 2 * 60 * 1000 // 2 minutes + +const getCachedSession = async () => { + const now = Date.now() + + // Return cached session if it's still valid + if (sessionCache && (now - cacheTimestamp) < CACHE_DURATION) { + return sessionCache + } + + // Fetch fresh session and update cache + const session = await getSession() + sessionCache = session + cacheTimestamp = now + + return session +} + httpBrowserClient.interceptors.request.use(async (config) => { - const session: any = await getSession() + const session = await getCachedSession() if (session?.user?.accessToken) { config.headers.Authorization = `Bearer ${session.user.accessToken}`