Skip to content

Commit 4540847

Browse files
committed
fix(appStorage): Fix that settings were not possible to save on vercel domains, use robust self-checking mechanism to ensure user data never lost when cookies storage enabled!
1 parent c360115 commit 4540847

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/react/appStorageProvider.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ const setCookieValue = (key: string, value: string): boolean => {
9191
}
9292

9393
document.cookie = cookie
94+
95+
// Verify the cookie was actually saved by reading it back
96+
const savedValue = getCookieValue(key)
97+
if (savedValue !== value) {
98+
console.warn(`Cookie verification failed for key '${key}'. Expected: ${value}, Got: ${savedValue}`)
99+
return false
100+
}
101+
94102
return true
95103
} catch (error) {
96104
console.error(`Failed to set cookie for key '${key}':`, error)
@@ -229,12 +237,19 @@ export const getRandomUsername = (appConfig: AppConfig) => {
229237

230238
export const appStorage = proxy({ ...defaultStorageData })
231239

240+
// Track if cookies failed in this session
241+
let cookiesFailedThisSession = false
242+
232243
// Check if cookie storage should be used (will be set by options)
233244
const shouldUseCookieStorage = () => {
234-
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
245+
// If cookies failed this session, don't try again
246+
if (cookiesFailedThisSession) {
247+
return false
248+
}
249+
235250
const isSecureCookiesAvailable = () => {
236251
// either https or localhost
237-
return window.location.protocol === 'https:' || (window.location.hostname === 'localhost' && !isSafari)
252+
return window.location.protocol === 'https:' || (window.location.hostname === 'localhost')
238253
}
239254
if (!isSecureCookiesAvailable()) {
240255
return false
@@ -345,8 +360,10 @@ const saveKey = (key: keyof StorageData) => {
345360
// Remove from localStorage if cookie save was successful
346361
markLocalStorageAsMigrated(key)
347362
} else {
348-
// Disabling for now so no confusing conflicts modal after page reload
349-
// useLocalStorage = true
363+
// Cookie save failed, disable cookies for this session and fallback to localStorage
364+
console.warn(`Cookie save failed for key '${key}', disabling cookies for this session`)
365+
cookiesFailedThisSession = true
366+
useLocalStorage = true
350367
}
351368
}
352369
}

0 commit comments

Comments
 (0)