fix(ui): guard against non-functional localStorage in Node/SSR environment (fixes #5379)#6226
Open
asheesh-devops wants to merge 1 commit intokeephq:mainfrom
Open
Conversation
When Node.js is started with --localstorage-file without a valid path, the global localStorage object exists but getItem/setItem are not functions. The existing typeof localStorage check passes, causing TypeError: localStorage.getItem is not a function on the /signin page. Added typeof localStorage.getItem/setItem !== 'function' checks to handle this edge case in the useLocalStorage hook. Closes keephq#5379
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
TypeError: localStorage.getItem is not a functionthat crashes the/signinpage with a 500 error on first run ofdocker compose -f docker-compose.dev.yml up.Root Cause
When Node.js is started with the
--localstorage-fileflag without a valid path (as happens in the Docker dev setup), the globallocalStorageobject exists butgetItem/setItemare not proper functions. The existing guard inuseLocalStoragehook only checkstypeof localStorage === "undefined"which passes, leading to:The Fix
Add additional type checks for
getItem/setItembeing actual functions:Changes
keep-ui/utils/hooks/useLocalStorage.ts— enhanced SSR guards ingetSnapshot()andsetLocalStorageValue()Testing
useLocalStoragehook used by 26+ components across the appuseSyncExternalStorethird argument (server snapshot) returnsJSON.stringify(initialValue)so the hook gracefully degrades during SSRlocalStoragecalls in the signin page flow — all go through this hookFixes #5379