Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

bookernath
Copy link
Contributor

@bookernath bookernath commented Sep 6, 2025

What/Why?


This pull request implements a prerendering staleness detection system for BigCommerce checkout pages
to improve user experience during navigation from prerendered pages.

Problem: When users navigate to checkout pages that were prerendered using the Speculation Rules API,
the prerendered data may become stale if the user modified their cart in another tab. This results
in users seeing outdated cart contents, totals, or checkout state.

Solution: Implements a comprehensive staleness detection system that:

  1. Version-based Detection: Uses the version field from the /api/storefront/checkouts/[id] API
    response to detect when checkout data has changed
  2. Automatic Refresh: Listens for the prerenderingchange event and automatically refreshes checkout
    data when stale
  3. Global API: Exposes a window.checkoutRefreshAPI for external systems to trigger manual refreshes
  4. Seamless UX: Only refreshes data when actually stale, preventing unnecessary API calls and UI jank

Technical Implementation:

  • PrerenderingStalenessDetector class captures initial checkout snapshots and compares versions
  • createPrerenderingChangeHandler provides automatic refresh on prerendering activation
  • createCheckoutRefreshAPI exposes manual refresh capabilities on the window object
  • TypeScript module augmentation extends the Checkout interface to include the version: number field
  • Integration with CheckoutPage.tsx for event handling and API setup

Additional Fixes:

  • Resolved webpack MANIFEST_JSON is not defined race condition in development mode
  • Updated all checkout mock objects to include the version property for type consistency

Rollout/Rollback


Rollout: This change can be safely rolled out immediately as it:

  • Only adds new functionality without modifying existing checkout behavior
  • Uses feature detection for prerenderingchange event support
  • Gracefully handles errors with try/catch blocks and console warnings
  • Has no external dependencies or database changes

Measuring Success:

  • Monitor console warnings for "Failed to refresh checkout data" to track any API issues
  • Track user-reported issues with stale checkout data
  • Monitor checkout conversion rates to ensure no negative impact

Rollback: Simple code revert if needed. No database migrations or persistent state changes. The
feature is entirely client-side and self-contained.

Testing


Unit Tests: All 18 tests passing for the staleness detection system:
npx nx run core:test --testPathPattern=prerenderingStalenessDetector
✓ PrerenderingStalenessDetector (9 tests)
✓ createPrerenderingChangeHandler (3 tests)
✓ createCheckoutRefreshAPI (6 tests)

Integration Testing:

  • Verified webpack build completes without MANIFEST_JSON errors
  • Confirmed TypeScript compilation passes with extended Checkout interface
  • Tested wallet-button-integration and other packages compile successfully

Manual Testing Approach:

  1. Enable Speculation Rules API in browser dev tools
  2. Navigate to product page, add items to cart
  3. Use "Prerender" in dev tools to prerender checkout page
  4. In original tab, modify cart (add/remove items)
  5. Navigate to prerendered checkout page
  6. Verify checkout data automatically refreshes to show current cart state
  7. Test window.checkoutRefreshAPI.refreshCheckout() manually in console

Browser Compatibility: Feature uses progressive enhancement - falls back gracefully in browsers
without Speculation Rules API support.

Performance Impact: Minimal - only adds version comparison logic and event listener. No impact on
users without prerendering.

@bookernath bookernath force-pushed the prerendering-trueup branch 2 times, most recently from 207f7c3 to 2e38ff5 Compare September 6, 2025 01:59
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