fix: Next.js hydration mismatches, focus ref logic & performance#731
Open
kamilguszpit wants to merge 2 commits intoemilkowalski:mainfrom
Open
fix: Next.js hydration mismatches, focus ref logic & performance#731kamilguszpit wants to merge 2 commits intoemilkowalski:mainfrom
kamilguszpit wants to merge 2 commits intoemilkowalski:mainfrom
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR fixes three critical issues for Next.js (App Router) compatibility: SSR hydration mismatches, focus management logic errors, and performance optimization. The changes ensure server-side rendering works correctly by avoiding window and document access during initial render, fix a focus ref bug by converting from a single ref to an array of refs for multiple toast positions, and optimize performance by removing unnecessary effect re-subscriptions.
- Fixed SSR hydration issues by initializing state with safe defaults before accessing browser APIs
- Converted single
listReftolistRefsarray to properly handle multiple toast positions - Optimized subscription effect by removing unnecessary dependency
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/hooks.tsx | Initializes useIsDocumentHidden state with false instead of document.hidden to prevent SSR hydration mismatch |
| src/index.tsx | Simplifies actualTheme initialization for SSR, converts listRef to listRefs array, updates focus/escape key handlers, and removes toasts dependency from subscription effect |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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
This PR addresses three critical issues identified during integration with Next.js (App Router):
windowanddocumentusage during initial render inuseIsDocumentHiddenandactualTheme. This eliminates console errors likeText content does not match server-rendered HTMLin Next.js applications.listRefwas being overwritten in the render loop. Converted it tolistRefs(array) to correctly handle multiple toaster positions. Now, the hotkey correctly focuses the first available list instead of only the last one.Toastersubscription effect by removing[toasts]from the dependency array, preventing unnecessary re-subscriptions on every state update.Changes
src/hooks.tsx: Initialized state withfalseinstead ofdocument.hidden.src/index.tsx:listRefsarray pattern.handleKeyDownto find and focus the first valid list (.find(el => el)).useEffectdependencies.