From f138b9c0884a25daf9bee98df10f074ce5d8099a Mon Sep 17 00:00:00 2001 From: Stijnus Date: Thu, 23 Oct 2025 14:40:54 +0200 Subject: [PATCH] Refactor localStorage access for Supabase state Replaces direct localStorage usage with a safe 'storage' variable that checks for globalThis and method existence. This improves compatibility with environments where localStorage may not be available, such as server-side rendering. --- app/lib/stores/supabase.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/app/lib/stores/supabase.ts b/app/lib/stores/supabase.ts index fbee139..4ccfa5b 100644 --- a/app/lib/stores/supabase.ts +++ b/app/lib/stores/supabase.ts @@ -51,8 +51,15 @@ export interface SupabaseConnectionState { credentials?: SupabaseCredentials; } -const savedConnection = typeof localStorage !== 'undefined' ? localStorage.getItem('supabase_connection') : null; -const savedCredentials = typeof localStorage !== 'undefined' ? localStorage.getItem('supabaseCredentials') : null; +const storage = + typeof globalThis !== 'undefined' && + typeof globalThis.localStorage !== 'undefined' && + typeof globalThis.localStorage.getItem === 'function' + ? globalThis.localStorage + : null; + +const savedConnection = storage ? storage.getItem('supabase_connection') : null; +const savedCredentials = storage ? storage.getItem('supabaseCredentials') : null; const initialState: SupabaseConnectionState = savedConnection ? JSON.parse(savedConnection) @@ -75,14 +82,14 @@ if (savedCredentials && !initialState.credentials) { export const supabaseConnection = atom(initialState); -if (initialState.token && !initialState.stats) { - fetchSupabaseStats(initialState.token).catch(console.error); -} - export const isConnecting = atom(false); export const isFetchingStats = atom(false); export const isFetchingApiKeys = atom(false); +if (initialState.token && !initialState.stats) { + fetchSupabaseStats(initialState.token).catch(console.error); +} + export function updateSupabaseConnection(connection: Partial) { const currentState = supabaseConnection.get(); @@ -123,16 +130,16 @@ export function updateSupabaseConnection(connection: Partial