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!

This commit is contained in:
Vitaly Turovsky 2025-07-18 07:53:47 +03:00
commit 45408476a5

View file

@ -91,6 +91,14 @@ const setCookieValue = (key: string, value: string): boolean => {
}
document.cookie = cookie
// Verify the cookie was actually saved by reading it back
const savedValue = getCookieValue(key)
if (savedValue !== value) {
console.warn(`Cookie verification failed for key '${key}'. Expected: ${value}, Got: ${savedValue}`)
return false
}
return true
} catch (error) {
console.error(`Failed to set cookie for key '${key}':`, error)
@ -229,12 +237,19 @@ export const getRandomUsername = (appConfig: AppConfig) => {
export const appStorage = proxy({ ...defaultStorageData })
// Track if cookies failed in this session
let cookiesFailedThisSession = false
// Check if cookie storage should be used (will be set by options)
const shouldUseCookieStorage = () => {
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
// If cookies failed this session, don't try again
if (cookiesFailedThisSession) {
return false
}
const isSecureCookiesAvailable = () => {
// either https or localhost
return window.location.protocol === 'https:' || (window.location.hostname === 'localhost' && !isSafari)
return window.location.protocol === 'https:' || (window.location.hostname === 'localhost')
}
if (!isSecureCookiesAvailable()) {
return false
@ -345,8 +360,10 @@ const saveKey = (key: keyof StorageData) => {
// Remove from localStorage if cookie save was successful
markLocalStorageAsMigrated(key)
} else {
// Disabling for now so no confusing conflicts modal after page reload
// useLocalStorage = true
// Cookie save failed, disable cookies for this session and fallback to localStorage
console.warn(`Cookie save failed for key '${key}', disabling cookies for this session`)
cookiesFailedThisSession = true
useLocalStorage = true
}
}
}