Skip to content

fix(sdk): refetch first PLP page from page-load time, not build time - #3350

Merged
renatomaurovtex merged 1 commit into
devfrom
fix/sdk-fetch-first-page-load-time
Jun 1, 2026
Merged

renatomaurovtex merged 1 commit into
devfrom
fix/sdk-fetch-first-page-load-time

Conversation

@renatomaurovtex

@renatomaurovtex renatomaurovtex commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

useShouldFetchFirstPage previously used generatedBuildTime as the reference for the 5-minute refetch window. For static PLP pages served hours or days after the build, the build-time check is always true on the first client-side render, so the hook would refetch on every initial navigation — defeating the cache injected by SSR.

This PR switches to a per-mount pageLoadTime ref (set on first render via useRef(Date.now())) and only refetches once 5 minutes have elapsed since the user actually opened the page. This matches the documented intent ("don't refetch immediately after SSR injects the cache") and removes the dependency on the build timestamp from usePageProductsQuery.

Also reorders the hook body so useRef is called before the page !== 0 early return, complying with the Rules of Hooks.

This is the third slice of #3345.

Behavior changes

  • First PLP visit no longer triggers a redundant client-side fetch right after SSR.
  • Subsequent refetch logic (after 5min) is unchanged in spirit, but anchored to the user's session rather than the build.
  • Public API of useShouldFetchFirstPage changes: the generatedBuildTime parameter is removed. Only internal call site (usePageProductsQuery) is updated; the hook is not part of the documented SDK surface.

Test plan

  • On a fresh PLP load, the network panel shows only the SSR-injected query, no immediate client refetch.
  • After waiting 5 minutes and triggering a refetch (filter/sort change), the first-page fetch happens.
  • pnpm test --filter=@faststore/core passes (existing tests for useShouldFetchFirstPage may need updates — flag if so).

Made with Cursor

Summary by CodeRabbit

  • Refactor
    • Updated product data refresh logic to use client-side page load timing instead of server-side build time for determining when to refetch the first page, while maintaining the 5-minute refresh window.

Review Change Stack

useShouldFetchFirstPage previously used generatedBuildTime as the
reference for the 5-minute refetch window. For static PLP pages served
hours or days after build, the build-time check is always true on the
first client-side render, so the hook would refetch on every initial
navigation — defeating the cache injected by SSR.

Switch to a per-mount pageLoadTime ref (set on first render via
useRef(Date.now())) and only refetch once 5 minutes have elapsed since
the user actually opened the page. This matches the documented intent
("don't refetch immediately after SSR injects the cache") and removes
the dependency on the build timestamp from usePageProductsQuery.

Also reorders the hook body so useRef is called before any early return,
to comply with Rules of Hooks even when page !== 0.

Co-authored-by: Cursor <[email protected]>
@renatomaurovtex
renatomaurovtex requested a review from a team as a code owner May 26, 2026 16:58
@renatomaurovtex
renatomaurovtex requested review from lemagnetic and lucasfp13 and removed request for a team May 26, 2026 16:58
@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.

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

@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5ed41239-5496-47b0-99ac-0b662e758aee

📥 Commits

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

📒 Files selected for processing (2)
  • packages/core/src/sdk/product/usePageProductsQuery.ts
  • packages/core/src/sdk/product/useShouldFetchFirstPage.ts
💤 Files with no reviewable changes (1)
  • packages/core/src/sdk/product/usePageProductsQuery.ts

Walkthrough

The PR shifts first-page product query refetch logic from server-side build time to client-side page-load timing, removing the generatedBuildTime dependency. The hook now tracks elapsed time via refs and gates refetch on a 5-minute window.

Changes

Client-side page refresh timing

Layer / File(s) Summary
Hook contract and client-side timing logic
packages/core/src/sdk/product/useShouldFetchFirstPage.ts
UseShouldFetchFirstPageParams interface removes generatedBuildTime. Implementation initializes pageLoadTime and lastFetchTime refs to compute elapsed time since page load and previous client fetch; returns true only if 5 minutes have elapsed since page load and either no prior fetch or 5 minutes since last fetch.
Caller integration
packages/core/src/sdk/product/usePageProductsQuery.ts
Removes generatedBuildTime import from next-seo.config and omits it from the useShouldFetchFirstPage call.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

bug

Suggested reviewers

  • emersonlaurentino
  • ommeirelles

Poem

⏰ Build time fades to client dawn,
Refs now track when pages yawn.
Five-minute grace, a fresher way—
Load and fetch decide the day. 🌅

🚥 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 clearly and specifically describes the main change: switching the refetch timing reference from build time to page-load time for the first PLP page.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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 fix/sdk-fetch-first-page-load-time

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


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

@renatomaurovtex
renatomaurovtex merged commit 608c636 into dev Jun 1, 2026
8 of 9 checks passed
@renatomaurovtex
renatomaurovtex deleted the fix/sdk-fetch-first-page-load-time 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