fix: guard render-time window/matchMedia access for SSR (DecayCard, PixelCard, Masonry) - #1006
Merged
Merged
Conversation
DecayCard, PixelCard and Masonry read browser globals during render (in useRef/useState initializers), which throws "window is not defined" or "matchMedia is not defined" under SSR (e.g. Next.js). Guard each with a typeof check, matching the pattern used in DavidHDev#911. - DecayCard: guard window.innerWidth/innerHeight in the cursor/winsize useRef initializers (fallback 0) - PixelCard: guard window.matchMedia in the reducedMotion useRef initializer (fallback false) - Masonry: guard matchMedia in the useMedia `get` initializer consumed by useState (fallback defaultValue) Effect/handler code is unchanged. Updated across all 4 variants of each component per CONTRIBUTING. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Owner
|
nice, thanks! |
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.
Problem
DecayCard,PixelCardandMasonryaccess browser globals during render (inuseRef/useStateinitializers), so they throw under SSR (e.g. Next.js, or anyrenderToStringenvironment) before the component ever mounts:Same class of bug fixed for
TargetCursorin #911; I've reused the sametypeof windowguard style.Changes
Guarded only the render-time accesses — effect/handler code is untouched.
cursorandwinsizeuseRefinitializers readwindow.innerWidth/innerHeight. Guarded withtypeof window !== 'undefined' ? … : 0. The existing resize/mousemove effect overwrites these on mount, so client behavior is unchanged.reducedMotion = useRef(window.matchMedia(...).matches)runs at render. Guarded withtypeof window !== 'undefined' && …(→falseon the server, i.e. motion not reduced).useMedia'sget()is passed touseState(get), so it runs once during render and callsmatchMedia. Added an earlyif (typeof window === 'undefined') return defaultValue;.All three were updated across all 4 variants (JS-CSS, JS-TW, TS-CSS, TS-TW) per the contributing guide.
Notes / testing
prettier --check).windowdo so inside effects/handlers or already guard withtypeof window, so they're SSR-safe and left as-is.