fix(ssr): prevent hydration mismatches from client-only storage reads#392
Closed
Valyrian-Code wants to merge 1 commit into
Closed
fix(ssr): prevent hydration mismatches from client-only storage reads#392Valyrian-Code wants to merge 1 commit into
Valyrian-Code wants to merge 1 commit into
Conversation
Header called isAuth() and getAllGroups() directly in JSX render. Footer initialized useState with getSessionStorage(). During SSR these return null/false; on the client they return real values, causing React hydration mismatches. Defer all storage reads to useEffect so server and client render the same initial output. Also guard getLocalStorage and getSessionStorage against stored 'undefined' strings, which caused JSON.parse to throw when a key was set with an undefined value. Signed-off-by: RAJVEER42 <irajveer.bishnoi2310@gmail.com>
|
This pull request has conflicts, please rebase to resolve those before we can evaluate the pull request. |
Author
|
Closing this PR — the issues it addressed are already fixed in the current
Rebasing would have reverted unrelated refactoring (cva variants, color tokens) on |
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.
Description
Fixes SSR hydration mismatches caused by browser storage being accessed during render. The server rendered fallback values (
null/false), while the client immediately rendered values from storage, resulting in hydration warnings.Changes
Header
isAuth()andgetAllGroups()were previously called directly during render.authenticatedandgroupsstate variables, initialized tofalseandnull.useEffect, ensuring consistent server and client renders.Footer
useStatewas initialized withgetSessionStorage("fossologyVersion").useEffect, preserving the existing cache behavior while avoiding hydration mismatches.storageHelper
getLocalStorageandgetSessionStoragecould throw when attempting to parse"undefined"viaJSON.parse.nullandundefinedvalues before parsing.How to Test
Start the development environment:
Open:
Open DevTools → Console.
Verify that no hydration mismatch warnings or errors appear during page load.
Closes #243.