Skip to content

fix: update marketplace stats fetch to include timestamp and adjust d…#359

Open
wolgwang1729 wants to merge 4 commits intoshopstr-eng:mainfrom
wolgwang1729:fix/homepage-stats-initial-load
Open

fix: update marketplace stats fetch to include timestamp and adjust d…#359
wolgwang1729 wants to merge 4 commits intoshopstr-eng:mainfrom
wolgwang1729:fix/homepage-stats-initial-load

Conversation

@wolgwang1729
Copy link
Copy Markdown
Contributor

@wolgwang1729 wolgwang1729 commented Apr 6, 2026

Description

This PR fixes the homepage stats first-load inconsistency in the Shopstr by the Numbers section (locally verified).

  • Updated stats fetching in pages/index.tsx so it re-runs when product data length changes, instead of only once on initial mount.
  • Added a cache-busting query param to the stats request to avoid stale cached responses returning outdated values.
  • Local impact:
    • Completely fixes the first-load issue where the first two stat cards showed 0 or placeholders until manual refresh.

Resolved or fixed issue

Fixes #358

Video

Screen.Recording.showing.the.fix.mp4

Affirmation

Copilot AI review requested due to automatic review settings April 6, 2026 15:17
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 6, 2026

@wolgwang1729 is attempting to deploy a commit to the shopstr-eng Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes inconsistent first-load homepage marketplace stats by making the stats request re-run when product event count changes and by avoiding stale cached responses.

Changes:

  • Adds a cache-busting timestamp query param to the marketplace stats API fetch.
  • Updates the stats-fetching useEffect dependency to re-run when productEvents.length changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

pages/index.tsx Outdated

useEffect(() => {
fetch("/api/db/marketplace-stats")
fetch(`/api/db/marketplace-stats?t=${Date.now()}`)
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

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

Using Date.now() as a query param forces a unique URL on every request, which can defeat CDN/browser caching entirely and can also lead to cache/key cardinality growth in intermediaries (each timestamp is a distinct cache key). Prefer controlling freshness via HTTP caching semantics (e.g., server Cache-Control: no-store / no-cache or client fetch options like cache: 'no-store') or a bounded version token that changes only when underlying data changes.

Suggested change
fetch(`/api/db/marketplace-stats?t=${Date.now()}`)
fetch("/api/db/marketplace-stats", { cache: "no-store" })

Copilot uses AI. Check for mistakes.
pages/index.tsx Outdated
})
.catch(() => {});
}, []);
}, [productEventContext.productEvents.length]);
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

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

Depending on productEventContext.productEvents.length directly often trips the React Hooks exhaustive-deps rule (because productEventContext / productEvents are referenced but not listed). Also, if the array content changes without a length change, the effect won’t re-run. Consider deriving a stable dependency (e.g., a memoized productEventsLength), or include the referenced object/array in deps and compute length inside the effect to satisfy linting and ensure intended refresh behavior.

Copilot uses AI. Check for mistakes.
@wolgwang1729 wolgwang1729 marked this pull request as draft April 6, 2026 18:19
@wolgwang1729
Copy link
Copy Markdown
Contributor Author

wolgwang1729 commented Apr 7, 2026

I was getting the following error, which is why I added safe JSON parsing

Error Type

Runtime SyntaxError

Error Message

Unexpected end of JSON input

at JSON.parse (<anonymous>:null:null)
at <unknown> (utils/nostr/fetch-service.ts:378:38)

Code Frame

376 | if (pubkeyProfilesToFetch.includes(event.pubkey)) {
377 | try {

378 | const content = JSON.parse(event.content);
| ^
379 | const profile = {
380 | pubkey: event.pubkey,
381 | created_at: event.created_at,

Next.js version: 16.1.7 (Turbopack)

@wolgwang1729 wolgwang1729 force-pushed the fix/homepage-stats-initial-load branch from 81a4a94 to 2cc8c51 Compare April 7, 2026 10:17
@wolgwang1729 wolgwang1729 force-pushed the fix/homepage-stats-initial-load branch from 2cc8c51 to c6505dd Compare April 7, 2026 10:28
@wolgwang1729 wolgwang1729 force-pushed the fix/homepage-stats-initial-load branch from c6505dd to 0984fc3 Compare April 7, 2026 10:34
content,
nip05Verified: false,
};
if (content.nip05) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @arnavkirti, I rebased and resolved the conflicts in the branch. I kept the fetch-service.tschanges from PR #294, but I’d appreciate it if you could verify that the latest merge didn’t break any of the profile-handling behavior or the DB caching changes from your PR.

@wolgwang1729 wolgwang1729 marked this pull request as ready for review April 7, 2026 11:06
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.

[BUG] Homepage stats show 0 locally and placeholders on production until refresh

2 participants