Repository navigation
feat(core): SSR ProductGallery and fix CLS on screen-size detection (VTE-9) - #3349
Conversation
…TE-9) Two related changes that reduce layout shift on PLP first paint: - ProductGallery: replace React.lazy() with a static import of ProductGalleryPage. The lazy chunk was loaded on the client after hydration, so the gallery initially rendered the Suspense fallback and then swapped in the real content, producing a measurable CLS. Inlining the gallery into the parent bundle removes the swap. - useScreenResize: use an isomorphic useLayoutEffect (useLayoutEffect in the browser, useEffect during SSR). The previous useEffect ran after the first paint, so mobile-only branches first rendered with isMobile=undefined and then shifted once the effect updated the state. useLayoutEffect runs synchronously before paint, eliminating the shift. Co-authored-by: Cursor <[email protected]>
WalkthroughProductGallery replaces lazy-loading of ChangesProductGallery static import migration
useScreenResize SSR compatibility
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. |
|
|
||
| // useLayoutEffect fires synchronously before the browser paints, eliminating | ||
| // the layout shift caused by useEffect firing after the first paint. | ||
| // Falls back to useEffect on the server (SSR) where window is not available. |
There was a problem hiding this comment.
Doesn't make sense as the server doesn't runs any useEffect neither useLayoutEffect.
| const [isDesktop, setIsDesktop] = useState(undefined) | ||
|
|
||
| useEffect(() => { | ||
| useIsomorphicLayoutEffect(() => { |
There was a problem hiding this comment.
to test if keeping useLayoutEffect resolves it. no need of isomorphic logic
There was a problem hiding this comment.
TODO: Change and test over again!
| const [isDesktop, setIsDesktop] = useState(undefined) | ||
|
|
||
| useEffect(() => { | ||
| useIsomorphicLayoutEffect(() => { |
There was a problem hiding this comment.
TODO: Change and test over again!
Summary
Two related changes that reduce layout shift on PLP first paint.
React.lazy(() => import('./ProductGalleryPage'))with a static import ofProductGalleryPage. The lazy chunk was loaded on the client after hydration, so the gallery first rendered the Suspense fallback and then swapped in the real content, producing a measurable CLS. Inlining the gallery into the parent bundle removes the swap.useEffectto an isomorphicuseLayoutEffect(useLayoutEffectin the browser,useEffectduring SSR). The previoususeEffectran after the first paint, so mobile-only branches first rendered withisMobile=undefinedand then shifted once the effect updated the state.useLayoutEffectruns synchronously before paint, eliminating the shift.This is the second slice of #3345.
Notes for reviewers
useIsomorphicLayoutEffectdefinition with anegated conditionwarning and aPrefer globalThis.window over windowwarning. Both are intentional — the negated form is the canonical pattern from the React docs, andwindow(notglobalThis.window) is the convention used throughout the rest of the file. Happy to revisit if reviewers prefer otherwise.Test plan
useScreenResizeconsumers continue to receive correctisMobile/isTablet/isDesktopafter window resize.pnpm test --filter=@faststore/corepasses.Made with Cursor
Summary by CodeRabbit