Skip to content

feat(core): SSR ProductGallery and fix CLS on screen-size detection (VTE-9) - #3349

Merged
renatomaurovtex merged 1 commit into
devfrom
perf/vte-9-plp-cls-ssr-gallery
Jun 1, 2026
Merged

renatomaurovtex merged 1 commit into
devfrom
perf/vte-9-plp-cls-ssr-gallery

Conversation

@renatomaurovtex

@renatomaurovtex renatomaurovtex commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two related changes that reduce layout shift on PLP first paint.

  • ProductGallery: replace React.lazy(() => import('./ProductGalleryPage')) with a static import of ProductGalleryPage. 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.
  • useScreenResize: switch from useEffect to 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.

This is the second slice of #3345.

Notes for reviewers

  • SonarQube flagged useIsomorphicLayoutEffect definition with a negated condition warning and a Prefer globalThis.window over window warning. Both are intentional — the negated form is the canonical pattern from the React docs, and window (not globalThis.window) is the convention used throughout the rest of the file. Happy to revisit if reviewers prefer otherwise.

Test plan

  • PLP first paint CWV: CLS does not regress (Lighthouse mobile).
  • PLP renders the full gallery on first paint (no Suspense fallback flash).
  • useScreenResize consumers continue to receive correct isMobile/isTablet/isDesktop after window resize.
  • pnpm test --filter=@faststore/core passes.

Made with Cursor

Summary by CodeRabbit

  • Refactor
    • Improved component loading and rendering architecture for better performance
    • Enhanced server-side rendering compatibility for layout operations

Review Change Stack

…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]>
@renatomaurovtex
renatomaurovtex requested a review from a team as a code owner May 26, 2026 16:57
@renatomaurovtex
renatomaurovtex requested review from gabpaladino and renatamottam and removed request for a team May 26, 2026 16:57
@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

ProductGallery replaces lazy-loading of ProductGalleryPage with static import while maintaining Suspense boundaries. useScreenResize adds isomorphic effect handling to support both browser and SSR environments, selecting useLayoutEffect or useEffect based on window availability.

Changes

ProductGallery static import migration

Layer / File(s) Summary
ProductGallery import and loading strategy
packages/core/src/components/ui/ProductGallery/ProductGallery.tsx
React imports remove lazy, and ProductGalleryPage shifts from React.lazy() to direct static import. Suspense wrapper with GalleryPageSkeleton fallback remains unchanged, simplifying component resolution without losing streaming fallback behavior.

useScreenResize SSR compatibility

Layer / File(s) Summary
useScreenResize isomorphic layout effect
packages/core/src/sdk/ui/useScreenResize.ts
React imports gain useLayoutEffect. A useIsomorphicLayoutEffect constant selects the appropriate hook based on window availability, and the resize listener effect switches from useEffect to the isomorphic variant, enabling correct timing in both browser and SSR contexts.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

📦 Static imports reign where lazy once roamed,
✨ Suspense still guards the gallery's home,
🪟 Layout effects now dance through both worlds,
SSR-safe, responsive banners unfurled.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title accurately summarizes the main changes: SSR optimizations for ProductGallery and CLS fixes via screen-size detection, matching the PR's core objectives of reducing layout shift.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch perf/vte-9-plp-cls-ssr-gallery

Comment @coderabbitai help to get the list of available commands and usage tips.

@codesandbox-ci

Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't make sense as the server doesn't runs any useEffect neither useLayoutEffect.

const [isDesktop, setIsDesktop] = useState(undefined)

useEffect(() => {
useIsomorphicLayoutEffect(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to test if keeping useLayoutEffect resolves it. no need of isomorphic logic

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Change and test over again!

const [isDesktop, setIsDesktop] = useState(undefined)

useEffect(() => {
useIsomorphicLayoutEffect(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Change and test over again!

@hellofanny hellofanny changed the title perf(plp): SSR ProductGallery and fix CLS on screen-size detection (VTE-9) perf(core): SSR ProductGallery and fix CLS on screen-size detection (VTE-9) Jun 1, 2026
@renatomaurovtex
renatomaurovtex merged commit e70a23b into dev Jun 1, 2026
6 of 10 checks passed
@renatomaurovtex
renatomaurovtex deleted the perf/vte-9-plp-cls-ssr-gallery branch June 1, 2026 19:28
@hellofanny hellofanny changed the title perf(core): SSR ProductGallery and fix CLS on screen-size detection (VTE-9) feat(core): SSR ProductGallery and fix CLS on screen-size detection (VTE-9) Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants