Skip to content

perf(plp): prioritize first product image as LCP (VTE-14) - #3348

Merged
renatomaurovtex merged 2 commits into
devfrom
perf/vte-14-plp-lcp-image
Jun 1, 2026
Merged

renatomaurovtex merged 2 commits into
devfrom
perf/vte-14-plp-lcp-image

Conversation

@renatomaurovtex

@renatomaurovtex renatomaurovtex commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Improves PLP LCP by making the browser fetch and prioritize the first product card image earlier.

  • ProductListingPage: render <link rel="preload" as="image" fetchPriority="high"> in <Head> for the first product image. The preload URL is generated by faststoreLoader with width=320 so it exactly matches the <img> srcset selection at Lighthouse mobile (412px viewport, DPR≈2, sizes="30vw" → 247px → next srcset step = 320). This avoids a double fetch.
  • ProductGrid: set fetchPriority: 'high' on the first card image, and switch eager loading from idx === 0 to idx < 4 so the first visible row is not blocked by lazy loading.
  • ProductCard: forward fetchPriority from imgProps to the underlying Image component.

This is the first slice of #3345.

Context

PageSpeed Insights identifies the first product card image as the LCP element on PLP even when a Hero section is present — the hero renders at a smaller visual area than the first product card on mobile.

Test plan

  • Lighthouse mobile PLP run shows LCP element = first product image and the preload reused (no request not used warning in DevTools).
  • PLP renders correctly when server.search.products.edges[0] is missing (no preload link rendered).
  • Other PLPs and PDP unaffected.
  • pnpm test --filter=@faststore/core passes.

Made with Cursor

Summary by CodeRabbit

  • Performance Improvements
    • Optimized image loading on product listing pages with intelligent priority handling to improve page load speed.
    • First product images are now prioritized for faster initial rendering on all devices.
    • Product cards now support configurable image loading priority settings for enhanced performance control.

Review Change Stack

PageSpeed Insights consistently identifies the first product card image
as the LCP element on PLP, even when a Hero section is present. Make the
browser fetch it earlier and prioritize its decoding/painting.

- ProductListingPage: render <link rel="preload" as="image"
  fetchPriority="high"> in <Head> for the first product image, using
  faststoreLoader with width=320 so the URL exactly matches the <img>
  srcset selection at Lighthouse mobile (412px viewport, DPR=2, 30vw).
- ProductGrid: set fetchPriority="high" on the first card image, and
  switch eager loading from idx === 0 to idx < 4 so the first row above
  the fold is not blocked by lazy loading.
- ProductCard: forward fetchPriority from imgProps to the underlying
  Image component.

Co-authored-by: Cursor <[email protected]>
@codesandbox-ci

codesandbox-ci Bot commented May 26, 2026 •

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.

Re-applies the changes that were lost when the original commit ended up
empty due to a husky pre-commit hook interaction.

- ProductListingPage: render <link rel="preload" as="image"
  fetchPriority="high"> in <Head> for the first product image, using
  faststoreLoader with width=320 so the URL exactly matches the <img>
  srcset selection at Lighthouse mobile (412px viewport, DPR=2, 30vw).
- ProductGrid: set fetchPriority="high" on the first card image, and
  switch eager loading from idx === 0 to idx < 4 so the first row above
  the fold is not blocked by lazy loading.
- ProductCard: forward fetchPriority from imgProps to the underlying
  Image component.

Co-authored-by: Cursor <[email protected]>
@renatomaurovtex
renatomaurovtex requested a review from a team as a code owner May 26, 2026 18:32
@renatomaurovtex
renatomaurovtex requested review from eduardoformiga and ommeirelles and removed request for a team May 26, 2026 18:32
@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

This PR optimizes image loading performance across product listing pages by introducing conditional fetchPriority hints and eager-loading rules. ProductCard enables priority control, ProductGrid applies strategy by viewport and page position, and ProductListingPage pre-announces the LCP candidate via next/head.

Changes

Image Loading Priority Optimization

Layer / File(s) Summary
ProductCard fetchPriority prop forwarding
packages/core/src/components/product/ProductCard/ProductCard.tsx
ProductCard now forwards imgProps?.fetchPriority to the Image component, enabling callers to control loading priority.
ProductGrid mobile and desktop strategies
packages/core/src/components/product/ProductGrid/ProductGrid.tsx
Mobile ViewportObserver grids set fetchPriority="high" for the first item. Desktop grids eagerly load the first four items and apply high priority only to the first item on the first page.
ProductListingPage LCP preload injection
packages/core/src/components/templates/ProductListingPage/ProductListingPage.tsx
Computes and preloads the first product image URL via faststoreLoader, then injects a <link rel="preload" as="image" fetchPriority="high"> tag into the document head when rendering.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • eduardoformiga

Poem

🖼️ First images leap ahead with priority high,
Eager loaders stand ready as users scroll by.
ProductCard whispers fetchPriority through,
While grids and pages orchestrate loading anew.
⚡ LCP smiles—the first frame arrives on time!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly summarizes the main change: prioritizing the first product image as LCP on the product listing page, which aligns perfectly with the changeset modifications across ProductListingPage, ProductGrid, and ProductCard.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

✏️ 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-14-plp-lcp-image

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

@pkg-pr-new

pkg-pr-new Bot commented May 26, 2026

Copy link
Copy Markdown

Open in StackBlitz

@faststore/api

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/cli

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/components

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/core

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/diagnostics

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/lighthouse

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/sdk

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

@faststore/ui

yarn add https://pkg.pr.new/vtex/faststore/@faststore/[email protected]

commit: 777da96

@sonar-workflows

Copy link
Copy Markdown

@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: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/src/components/product/ProductGrid/ProductGrid.tsx`:
- Around line 192-194: The ProductGrid component currently treats firstPage as
optional which can disable the LCP fetchPriority optimization when callers
accidentally pass undefined; update ProductGrid so firstPage is always
defined—either make the firstPage prop required in the ProductGrid props
interface (remove optional marker) or give it a safe default (default firstPage
= 0 in the ProductGrid function signature) and then use that defined value in
the fetchPriority check (the comparison using firstPage inside ProductGrid where
loading/fetchPriority are set). Also audit call sites (ProductGalleryPage,
ProductGallery, useSearch usage) to ensure they still pass or rely on the
enforced/ defaulted firstPage.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d604c919-abbb-466e-a327-34141a189155

📥 Commits

Reviewing files that changed from the base of the PR and between 2edbcfd and 777da96.

📒 Files selected for processing (3)
  • packages/core/src/components/product/ProductCard/ProductCard.tsx
  • packages/core/src/components/product/ProductGrid/ProductGrid.tsx
  • packages/core/src/components/templates/ProductListingPage/ProductListingPage.tsx

Comment on lines +192 to +194
loading: idx < 4 ? 'eager' : 'lazy',
fetchPriority:
idx === 0 && page === firstPage ? 'high' : undefined,

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.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for ProductGrid usage to verify firstPage is provided
rg -nP --type=tsx --type=ts -C3 '<ProductGrid\b' -g '!ProductGrid.tsx'

Repository: vtex/faststore

Length of output: 86


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# 1) Inspect ProductGrid.tsx around the optional `firstPage` typing and the usage near lines 192-194
FILE='packages/core/src/components/product/ProductGrid/ProductGrid.tsx'
echo "=== Context: $FILE (lines 1-80) ==="
sed -n '1,120p' "$FILE" | cat -n

echo "=== Context: $FILE (lines 160-240) ==="
sed -n '160,240p' "$FILE" | cat -n

# 2) Find ProductGrid usages excluding its own file
echo "=== Searching for ProductGrid call sites ==="
rg -n --glob '!**/ProductGrid.tsx' --glob '**/*.{ts,tsx,js,jsx}' -C3 'ProductGrid\b' packages/core/src packages/components/src 2>/dev/null || true
rg -n --glob '!**/ProductGrid.tsx' --glob '**/*.{ts,tsx,js,jsx}' -C3 'ProductGrid\b' packages 2>/dev/null || true

Repository: vtex/faststore

Length of output: 23663


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the known call site around where ProductGrid is rendered
FILE='packages/core/src/components/ui/ProductGallery/ProductGalleryPage.tsx'
echo "=== Context: $FILE (ProductGrid usage) ==="
rg -n '<ProductGrid\b' "$FILE" -n
sed -n '35,110p' "$FILE" | cat -n

# Search for any ProductGrid JSX usage and whether firstPage is passed
echo "=== Search: <ProductGrid ... firstPage=... ==="
rg -n '<ProductGrid\b[^>]*\bfirstPage=' packages/core/src packages/components/src packages 2>/dev/null || true

echo "=== Search: <ProductGrid\b' call sites (excluding export/type noise) ==="
rg -n '<ProductGrid\b' packages/core/src packages/components/src packages -S --glob '!**/*.d.ts' 2>/dev/null || true

echo "=== Search: firstPage usage anywhere ==="
rg -n '\bfirstPage\b' packages/core/src packages/components/src packages 2>/dev/null || true

Repository: vtex/faststore

Length of output: 3349


🏁 Script executed:

#!/bin/bash
set -euo pipefail

FILE='packages/core/src/components/ui/ProductGallery/ProductGallery.tsx'
echo "=== Context: $FILE (around firstPage={pages[0]}) ==="
sed -n '280,360p' "$FILE" | cat -n

echo "=== Search: how `pages` is computed ==="
rg -n '\bconst pages\b|pages\s*=' "$FILE"

# show the definition of `pages` plus nearby lines
rg -n '\bpages\b' "$FILE" -n | head -n 50

Repository: vtex/faststore

Length of output: 3973


🏁 Script executed:

#!/bin/bash
set -euo pipefail

FILE='packages/core/src/components/ui/ProductGallery/ProductGallery.tsx'
echo "=== Search for `pages` definition in $FILE ==="
rg -n --no-heading '\bconst\s+pages\b|\bpages\s*=' "$FILE"

# Show the most relevant nearby blocks for the `pages` definition
# (First match only, to keep output small)
LINE=$(rg -n --no-heading '\bconst\s+pages\b|\bpages\s*=' "$FILE" | head -n 1 | cut -d: -f1)
echo "=== Showing around line $LINE ==="
sed -n "$((LINE-20)),$((LINE+40))p" "$FILE" | cat -n

echo "=== Find where `pages[0]` is used ==="
rg -n 'firstPage=\{pages\[0\]\}' "$FILE" -n

Repository: vtex/faststore

Length of output: 220


🏁 Script executed:

#!/bin/bash
set -euo pipefail

FILE='packages/core/src/components/ui/ProductGallery/ProductGallery.tsx'

echo "=== First 60 matches of `pages` in $FILE ==="
rg -n '\bpages\b' "$FILE" | head -n 60

echo
echo "=== Around the line that uses firstPage={pages[0]} ==="
FIRSTPAGE_LINE=$(rg -n 'firstPage=\{pages\[0\]\}' "$FILE" | head -n 1 | cut -d: -f1 || true)
echo "firstPage line: ${FIRSTPAGE_LINE:-<not found>}"
if [ -n "${FIRSTPAGE_LINE:-}" ]; then
  sed -n "$((FIRSTPAGE_LINE-40)),$((FIRSTPAGE_LINE+40))p" "$FILE" | cat -n
fi

echo
echo "=== Around the line(s) with pages.map((page) => ...) ==="
MAP_LINE=$(rg -n 'pages\.map\(\(page\)\s*=>' "$FILE" | head -n 1 | cut -d: -f1 || true)
echo "map line: ${MAP_LINE:-<not found>}"
if [ -n "${MAP_LINE:-}" ]; then
  sed -n "$((MAP_LINE-40)),$((MAP_LINE+60))p" "$FILE" | cat -n
fi

Repository: vtex/faststore

Length of output: 8855


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect where `pages` comes from in ProductGallery.tsx
FILE='packages/core/src/components/ui/ProductGallery/ProductGallery.tsx'
echo "=== Context around `const { pages, ... } = useSearch()` in $FILE ==="
sed -n '110,170p' "$FILE" | cat -n

# Find definition/import of useSearch in this file
echo
echo "=== Import/definition of useSearch in $FILE ==="
rg -n 'useSearch' "$FILE"

# Resolve where useSearch is implemented and inspect its return type
# (Search for `function useSearch` or `export const useSearch` across core)
echo
echo "=== Finding useSearch implementation ==="
rg -n --type=ts --type=tsx 'useSearch\s*=\s*|function\s+useSearch|export\s+function\s+useSearch' packages/core/src packages -S --glob '!**/*.d.ts' 2>/dev/null || true

Repository: vtex/faststore

Length of output: 2940


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# 1) Find where useSearch is implemented in this repo (it's imported from `@faststore/sdk`)
echo "=== Searching for useSearch implementation ==="
rg -n --type=ts --type=tsx 'useSearch\s*=\s*|function\s+useSearch|export\s+function\s+useSearch' packages 2>/dev/null || true

echo
echo "=== Searching for 'pages' return shape from useSearch ==="
rg -n --type=ts --type=tsx '\bpages\b' packages -S 2>/dev/null | head -n 50

Repository: vtex/faststore

Length of output: 164


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Quick inventory of packages
echo "=== packages/ ==="
ls -1 packages | head -n 200

# Find the workspace package that corresponds to `@faststore/sdk`
echo
echo "=== Search package.json for `@faststore/sdk` ==="
rg -n '"name"\s*:\s*"`@faststore/sdk`"' -S packages 2>/dev/null || true

# Try locating useSearch type/definition inside the repo (if `@faststore/sdk` is vendored here)
echo
echo "=== Search for 'useSearch' in packages (excluding dist/build) ==="
rg -n --type=ts --type=tsx --type=dts --type=js --type=mjs --type=cjs 'useSearch' packages -S 2>/dev/null || true

# Also search for the literal import source '`@faststore/sdk`'
echo
echo "=== Search for import { useSearch } from '`@faststore/sdk`' ==="
rg -n --type=ts --type=tsx "from '`@faststore/sdk`'" packages -S 2>/dev/null || true

Repository: vtex/faststore

Length of output: 374


🏁 Script executed:

#!/bin/bash
set -euo pipefail

SDK_DIR='packages/sdk'

echo "=== Search for useSearch implementation in $SDK_DIR ==="
rg -n --type=ts --type=tsx --type=d.ts --type=cts --type=mts 'useSearch' "$SDK_DIR" -S

echo
echo "=== Search for 'pages' usage in $SDK_DIR ==="
rg -n --type=ts --type=tsx 'pages' "$SDK_DIR" -S | head -n 80

echo
echo "=== Narrow: search for where pages is returned from useSearch ==="
rg -n --type=ts --type=tsx 'return\s+\{[^}]*pages|pages\s*:' "$SDK_DIR" -S || true

Repository: vtex/faststore

Length of output: 164


🏁 Script executed:

#!/bin/bash
set -euo pipefail

SDK_DIR='packages/sdk'

echo "=== Search for useSearch implementation (ts/tsx only) in $SDK_DIR ==="
rg -n --glob '*.{ts,tsx}' 'useSearch' "$SDK_DIR" -S || true

echo
echo "=== Search for `pages` mentions in $SDK_DIR ==="
rg -n --glob '*.{ts,tsx}' '\bpages\b' "$SDK_DIR" -S | head -n 120 || true

echo
echo "=== Narrow: search for pages in useSearch-related code ==="
rg -n --glob '*.{ts,tsx}' -S 'useSearch.*pages|pages.*useSearch|function\s+useSearch|export\s+function\s+useSearch|const\s+useSearch' "$SDK_DIR" || true

Repository: vtex/faststore

Length of output: 6124


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect useSearch + the global search state that provides `pages`
SDK_FILE_STATE='packages/sdk/src/search/globalState/useSearchState.ts'
SDK_FILE_USESEARCH='packages/sdk/src/search/useSearch.ts'
SDK_FILE_PAGINATION='packages/sdk/src/search/usePagination.ts'

echo "=== useSearch.ts ==="
sed -n '1,120p' "$SDK_FILE_USESEARCH" | cat -n

echo
echo "=== useSearchState.ts (pages type + default init) ==="
# show around the interface/type and initial values
sed -n '1,220p' "$SDK_FILE_STATE" | cat -n

echo
echo "=== usePagination.ts (assumptions about pages non-empty) ==="
sed -n '1,120p' "$SDK_FILE_PAGINATION" | cat -n

Repository: vtex/faststore

Length of output: 6751


🏁 Script executed:

#!/bin/bash
set -euo pipefail

rg -n --glob '!**/ProductGrid.tsx' '<ProductGrid\b' packages -S --glob '*.{ts,tsx,js,jsx}' 2>/dev/null || true

Repository: vtex/faststore

Length of output: 150


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect tests that assert default pages initialization in sdk search Provider
rg -n --glob '*.{ts,tsx}' 'pages\.toEqual\(\[0\]|\bpages\b.*toEqual\(' packages/sdk/test/search/Provider.test.tsx
echo
rg -n --glob '*.{ts,tsx}' 'resetInfiniteScroll|start\(|initialize\(|pages:' packages/sdk/test/search/Provider.test.tsx | head -n 80

# Show the sections around where pages are asserted
rg -n --glob '*.{ts,tsx}' 'expect\(result\.current\.pages\)' packages/sdk/test/search/Provider.test.tsx

Repository: vtex/faststore

Length of output: 213


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect Provider.test.tsx around the assertions
FILE='packages/sdk/test/search/Provider.test.tsx'
echo "=== Context around pages expectations in $FILE ==="
sed -n '360,460p' "$FILE" | cat -n

echo
echo "=== Context around where pages are initialized/modified in $FILE ==="
# show early initialization section too
sed -n '1,120p' "$FILE" | cat -n

echo
echo "=== Inspect packages/sdk/src/search/Provider.tsx for pages initialization ==="
PROV='packages/sdk/src/search/Provider.tsx'
sed -n '1,120p' "$PROV" | cat -n
sed -n '120,260p' "$PROV" | cat -n

Repository: vtex/faststore

Length of output: 9737


ProductGrid firstPage handling: callers pass it; keep it robust

All current ProductGrid call sites pass a firstPage value: ProductGalleryPage requires firstPage: number and forwards it to ProductGrid, and ProductGallery supplies it as pages[0] from useSearch. In @faststore/sdk, pages is a number[] initialized to [0] when session storage is missing, and pagination updates always keep it non-empty—so the page === firstPage LCP fetchPriority="high" optimization shouldn’t silently disable in normal flows.
Edge case: if session storage contains an empty pages array ([]), pages[0] becomes undefined and the optimization drops; consider making firstPage required in ProductGrid props or defaulting firstPage to 0.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/core/src/components/product/ProductGrid/ProductGrid.tsx` around
lines 192 - 194, The ProductGrid component currently treats firstPage as
optional which can disable the LCP fetchPriority optimization when callers
accidentally pass undefined; update ProductGrid so firstPage is always
defined—either make the firstPage prop required in the ProductGrid props
interface (remove optional marker) or give it a safe default (default firstPage
= 0 in the ProductGrid function signature) and then use that defined value in
the fetchPriority check (the comparison using firstPage inside ProductGrid where
loading/fetchPriority are set). Also audit call sites (ProductGalleryPage,
ProductGallery, useSearch usage) to ensure they still pass or rely on the
enforced/ defaulted firstPage.

@renatomaurovtex
renatomaurovtex merged commit 8fd5dea into dev Jun 1, 2026
8 of 10 checks passed
@renatomaurovtex
renatomaurovtex deleted the perf/vte-14-plp-lcp-image branch June 1, 2026 19:18
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.

2 participants