Skip to content

PLP performance: narrow fields, derive hasMore, drop dead memo - #123

Merged
damianlegawiec merged 2 commits into
mainfrom
fix/plp-optimization
Apr 10, 2026
Merged

PLP performance: narrow fields, derive hasMore, drop dead memo#123
damianlegawiec merged 2 commits into
mainfrom
fix/plp-optimization

Conversation

@damianlegawiec

@damianlegawiec damianlegawiec commented Apr 10, 2026

Copy link
Copy Markdown
Member

Three Vercel React best-practices fixes to the product listing:

  1. Pass a fields list to the listing fetches (PRODUCT_CARD_FIELDS) so Spree returns only the columns ProductCard reads. Shrinks the cached entry, the RSC→client serialization, and the streaming HTML size. Applied to both ProductListing (category/search PLPs) and FeaturedProducts (homepage carousel). Same pattern SearchBar already uses for quick-search suggestions.

  2. Derive hasMore in InfiniteProductList from currentPage < knownPages rather than mirroring it in its own useState. Introduce a separate hasError flag so the "nothing left to load" and "fetch failed" signals no longer share state, per the "don't mirror derivable values into state" rule.

  3. Drop the manual useMemo wrapping the product grid JSX. reactCompiler is enabled in next.config, so the compiler handles this automatically — the manual memo was redundant.

Summary by CodeRabbit

  • Bug Fixes

    • Improved error handling in product list pagination to better distinguish between connection failures and exhausted inventory.
  • Refactor

    • Optimized product data retrieval to reduce payload size and improve loading performance by fetching only essential product fields.

Three Vercel React best-practices fixes to the product listing:

1. Pass a fields list to the listing fetches (`PRODUCT_CARD_FIELDS`) so
   Spree returns only the columns ProductCard reads. Shrinks the
   cached entry, the RSC→client serialization, and the streaming HTML
   size. Applied to both ProductListing (category/search PLPs) and
   FeaturedProducts (homepage carousel). Same pattern SearchBar
   already uses for quick-search suggestions.

2. Derive `hasMore` in InfiniteProductList from `currentPage <
   knownPages` rather than mirroring it in its own useState. Introduce
   a separate `hasError` flag so the "nothing left to load" and
   "fetch failed" signals no longer share state, per the
   "don't mirror derivable values into state" rule.

3. Drop the manual useMemo wrapping the product grid JSX.
   reactCompiler is enabled in next.config, so the compiler handles
   this automatically — the manual memo was redundant.
@vercel

vercel Bot commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
storefront Ready Ready Preview, Comment Apr 10, 2026 8:33pm

Request Review

@coderabbitai

coderabbitai Bot commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@damianlegawiec has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 0 minutes and 31 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 0 minutes and 31 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d5e3f881-4e86-42f1-a17b-de9ca714258a

📥 Commits

Reviewing files that changed from the base of the PR and between e963667 and ed4dde7.

📒 Files selected for processing (3)
  • src/components/products/InfiniteProductList.tsx
  • src/components/products/ProductListing.tsx
  • src/lib/data/cached.ts

Walkthrough

This PR optimizes product data fetching by introducing a PRODUCT_CARD_FIELDS constant defining minimal required fields for product cards and applying it across product listing components. Additionally, InfiniteProductList refactors state management to distinguish between exhausted lists and errors, and removes unnecessary memoization.

Changes

Cohort / File(s) Summary
Product Field Optimization
src/lib/data/cached.ts, src/components/products/FeaturedProducts.tsx, src/components/products/ProductListing.tsx
Introduces PRODUCT_CARD_FIELDS constant with minimal product fields (id, name, slug, thumbnail_url, purchasable, default_variant_id, price, original_price) and passes it to product fetching calls to constrain API payload size.
InfiniteProductList State Refactoring
src/components/products/InfiniteProductList.tsx
Replaces hasMore state with derived logic using knownPages; adds explicit hasError flag to distinguish failed loads from exhausted lists; updates loadNextPage guard conditions; removes useMemo wrapper around grid rendering.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 A field list trimmed so neat and fine,
ProductCards fetch just what's divine,
No extra bloat clutters the stream,
While errors now shine bright and gleam—
State logic flows, memos take flight,
Optimization hops through the night! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes all three main changes: narrowing fields for performance, deriving hasMore state, and removing unnecessary memoization.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/plp-optimization

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 2

🧹 Nitpick comments (1)
src/components/products/ProductListing.tsx (1)

100-105: Prevent baseParams from overriding the narrowed fields set.

Line 104 can override Line 102 when baseParams.fields is present, which weakens the “always narrow to card fields” optimization.

Suggested diff
   const listParams: ProductListParams = {
     limit: PAGE_SIZE,
-    fields: PRODUCT_CARD_FIELDS,
     ...queryParams,
     ...baseParams,
+    fields: PRODUCT_CARD_FIELDS,
   };
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/components/products/ProductListing.tsx` around lines 100 - 105,
listParams currently spreads baseParams after setting fields, so
baseParams.fields can override the enforced PRODUCT_CARD_FIELDS; update the
construction of listParams (ProductListParams) so PRODUCT_CARD_FIELDS is applied
last (or explicitly assign fields = PRODUCT_CARD_FIELDS after spreading
queryParams and baseParams) ensuring PAGE_SIZE and other params remain, but
fields cannot be overridden by baseParams or queryParams.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/components/products/InfiniteProductList.tsx`:
- Around line 66-67: The current hasMore definition (hasMore = !hasError &&
currentPage < knownPages) causes the UI to think there are no more products on
fetch failure; change the logic so hasMore only reflects pagination (e.g.,
hasMore = currentPage < knownPages) and keep error state separate, then update
the noMoreProducts rendering to only show when there is truly no more data
(e.g., !hasError && !isLoading && !hasMore). Locate the hasMore variable and the
noMoreProducts render in the InfiniteProductList component and adjust them so
errors do not trigger the "no more products" message.

In `@src/lib/data/cached.ts`:
- Around line 23-32: PRODUCT_CARD_FIELDS currently omits product.categories
which prevents GTM mapping from populating GA4 item_category; update the
PRODUCT_CARD_FIELDS array in cached.ts (the exported constant
PRODUCT_CARD_FIELDS) to include "categories" so product objects returned by
cached data include categories and analytics/gtm.ts can read item_category as
before.

---

Nitpick comments:
In `@src/components/products/ProductListing.tsx`:
- Around line 100-105: listParams currently spreads baseParams after setting
fields, so baseParams.fields can override the enforced PRODUCT_CARD_FIELDS;
update the construction of listParams (ProductListParams) so PRODUCT_CARD_FIELDS
is applied last (or explicitly assign fields = PRODUCT_CARD_FIELDS after
spreading queryParams and baseParams) ensuring PAGE_SIZE and other params
remain, but fields cannot be overridden by baseParams or queryParams.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 77dca992-1fb7-4e2a-991d-03f28482a33c

📥 Commits

Reviewing files that changed from the base of the PR and between 5f10d91 and e963667.

📒 Files selected for processing (4)
  • src/components/products/FeaturedProducts.tsx
  • src/components/products/InfiniteProductList.tsx
  • src/components/products/ProductListing.tsx
  • src/lib/data/cached.ts

Comment thread src/components/products/InfiniteProductList.tsx Outdated
Comment thread src/lib/data/cached.ts
- InfiniteProductList: decouple hasMore from hasError so fetch
  failures no longer show "no more products to load". hasMore is now
  pure pagination state (currentPage < knownPages); the render gate
  for the exhausted message checks !hasError && !hasMore.
- PRODUCT_CARD_FIELDS: include `categories` so mapProductToGA4Item
  can continue to populate GA4 item_category on view_item_list and
  select_item events. Without it, analytics silently lost category
  attribution for every listing interaction.
- ProductListing: move `fields: PRODUCT_CARD_FIELDS` to the end of
  the listParams spread so baseParams / queryParams can't
  accidentally override the narrowed set.
@damianlegawiec
damianlegawiec merged commit 1652a4b into main Apr 10, 2026
6 checks passed
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.

1 participant