Skip to content

How to fix the data inconsistency between the shopping cart and checkout pages after updating next.js 16? #1504

@liaoyio

Description

@liaoyio

Update Next.js 16

Summary

  • Previous Version: 15.3.0-canary.13
  • Upgraded To: 16.0.5
  • Package Manager: pnpm
  • Status: ⚠️ Partially successful upgrade

my code 👉🏻: liaoyio@12a2d60

Issues Requiring Manual Fixes

✅ Fixed: Experimental Flags Merged

File: next.config.ts

Issue: Next.js 16 merged experimental.ppr and experimental.useCache into experimental.cacheComponents

Before:

experimental: {
  ppr: true,
  inlineCss: true,
  useCache: true
}

After:

experimental: {
  cacheComponents: true,
  inlineCss: true
}

✅ Fixed: Turbopack Flag Removed

File: package.json

Issue: Turbopack is now default in Next.js 16, the --turbopack flag is no longer needed

Before:

"dev": "next dev --turbopack"

After:

"dev": "next dev"

✅ Fixed: revalidateTag API Update

Files:

  • lib/shopify/index.ts

Issue: Next.js 16 recommends adding a profile parameter to revalidateTag() to achieve stale-while-revalidate behavior

Before:

revalidateTag(TAGS.collections);
revalidateTag(TAGS.products);

After:

revalidateTag(TAGS.collections, 'max');
revalidateTag(TAGS.products, 'max');

✅ Fixed: updateTag Usage in Server Actions

File: components/cart/actions.ts

Issue: Next.js 16 introduced the updateTag() API, specifically designed for Server Actions that require immediate data updates

Explanation:

Feature Next.js 15 revalidateTag() Next.js 16 revalidateTag() Next.js 16 updateTag()
Cache Invalidation ✅ Immediate ✅ Immediate ✅ Immediate
Route Refresh ✅ Auto-triggered ❌ Not triggered ✅ Auto-triggered
Page Re-render ✅ Yes ❌ No ✅ Yes
Data Update Timing Immediate Delayed (SWR) Immediate
Use Case Server Actions Route Handlers / Webhooks Server Actions
  • updateTag() immediately invalidates cache and triggers route refresh
  • revalidateTag(tag, 'max') uses stale-while-revalidate semantics and does not immediately trigger route refresh
  • In cart operations, using updateTag() ensures users see updates immediately (e.g., cart modal display, cart count in header component)

Current Implementation:

// addItem removeItem and updateItemQuantity - use updateTag for immediate updates
updateTag(TAGS.cart);

✅ Fixed: await params Hydration Error

  • When page components directly await params and include components using useSearchParams(), uncached data triggers errors;
  • Extract these async operations into separate components and wrap them with Suspense to avoid hydration errors and support streaming rendering;
  • Page components can immediately return Suspense boundaries without blocking rendering;

Current structure:

ProductPage (synchronous) 
  └─ Suspense
      └─ ProductPageContent (async, await params)
          └─ ProductProvider (uses useSearchParams)
  • This solves both the uncached data access issue with params and the Suspense requirement for useSearchParams().

Modified files: app/product/[handle]/page.tsx:52,133

✅ Fixed: Missing Suspense in Cache Components Mode

Issue: Hydration errors caused by missing Suspense in Cache Components mode.
Fix: Wrap components with Suspense

Total Updates: 2 locations

  • 1 location in components/layout/navbar/index.tsx:56,58
  • 2 locations in components/carousel.tsx:17,40

✅ Fixed: Cache Components Mode Build Error

Issue: In Next.js 16's Cache Components mode, the /[page] route is pre-rendered at build time, but CartProvider accessed an uncached cartPromise, causing blocking and build-time errors.

Fix

  1. Add cache directive to getCart()
  • Add 'use cache: private' to getCart() in lib/shopify/index.ts
  • Each user has independent cache, avoiding build-time blocking
  1. Add <Suspense> boundary in root layout
  • Wrap CartProvider's children with <Suspense> in app/layout.tsx
  • Ensure uncached data is accessed within <Suspense> boundary

✅ Fixed: Cannot access Date.now() Hydration Error

Updated File components/layout/footer.tsx:11,16

After Change:

'use cache';
const menu = await getMenu('next-js-frontend-footer-menu');
const skeleton = 'w-full h-6 animate-pulse rounded-sm bg-neutral-200 dark:bg-neutral-700';
const currentYear = new Date().getFullYear();
const copyrightDate = 2023 + (currentYear > 2023 ? `-${currentYear}` : '');
const copyrightName = COMPANY_NAME || SITE_NAME || '';

❌ Unfixed: Cart Quantity Update Issues with Rapid Consecutive Clicks

After upgrading from Next.js 15.3.0-canary.13 to 16.0.5, the following issues could not be resolved:

Issue 1: Cart quantity update operations behave abnormally when navigating to checkout page after rapid consecutive clicks.

  1. User rapidly clicks the "+" button 3 times
  2. 3 Server Actions start almost simultaneously
  3. Each Action calls getCart(), potentially reading the same old data (e.g., quantity = 5)
  4. Client-side payload is based on optimistic update: item.quantity + 1 = 6
  5. All 3 requests attempt to set quantity to 6, instead of the expected 8
  6. Final result: Quantity only increases by 1, instead of 3
Nov-30-2025.18-53-42.mp4

Issue 2: Navigation to homepage after rapid consecutive cart quantity updates requires waiting for all server actions to complete before normal navigation can occur.

  1. User rapidly clicks the "+" button 3 times
  2. Closes the cart modal
  3. Immediately clicks the homepage logo to return to homepage
  4. Final result: Page is blocked and cannot directly return to homepage; navigation only works after all Server Actions complete
Nov-30-2025.18-55-34.mp4

This might be a business scenario issue, but my expected functionality is as follows:

  1. When cart operations are clicked rapidly, if the checkout button is clicked, it should wait for all requests to complete before navigating to ensure checkout page reliability! Like this 👉🏻

  2. When cart operations are clicked rapidly, if navigating to homepage or other pages, it should discard the existing request queue and navigate immediately; alternatively, is it possible to achieve non-blocking page navigation while cart data still updates normally?

Verified Functionality:

  • ✅ Development server starts normally
  • ✅ Page routing works normally
  • ✅ Cart operation functionality works normally
  • ❌ Navigation to checkout page after cart operations behaves abnormally
  • ✅ Build succeeds, no compilation errors
  • ✅ No linter errors

my code 👉🏻: liaoyio@12a2d60

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions