Skip to content
← All posts

A Cached Product Page with a Live Cart in Next.js

A product page does not have to choose between “all static” and “all dynamic.” Keep public product content reusable, read the current cart through an authenticated request-time path, and give that cart its own loading state.

The important boundary is data ownership, not how close two elements sit in the layout.

Split the page by correctness requirements

For this example, assume the product title and description are public. The cart belongs to a session. Inventory and checkout prices have their own authoritative rules and are not decided by a cached description.

The Cache Components model supports reusable content alongside streamed request-time work.

The checkout branch matters. Fast product delivery must not turn a cached display value into permission to charge or reserve inventory.

Keep the cart's wait in the cart component

Here is the page structure. The imported functions are application-owned: getPublicProduct returns only public fields and applies your cache policy; readAuthorizedCart validates the session before returning cart data.

import { Suspense } from 'react';
import { getPublicProduct } from '@/lib/catalog';
import { readAuthorizedCart } from '@/lib/cart';

async function CartSummary() {
  const cart = await readAuthorizedCart();
  return <p>{cart.itemCount} items in your cart</p>;
}

async function ProductDetails({ id }: { id: string }) {
  const product = await getPublicProduct(id);
  return (
    <section>
      <h1>{product.title}</h1>
      <p>{product.description}</p>
    </section>
  );
}

export function ProductView({ id }: { id: string }) {
  return (
    <main>
      <ProductDetails id={id} />
      <Suspense fallback={<p>Loading your cart...</p>}>
        <CartSummary />
      </Suspense>
    </main>
  );
}

This is a component excerpt, not a complete route or authentication implementation. Your route must resolve and validate its product parameter. Enable Cache Components and make the public reader's lifetime explicit in the data module.

Do not cache the response as though everyone shares a cart

The public result can be shared because its contract is public. The cart cannot be made public merely by wrapping it in a reusable parent.

Read session information through your authentication layer at request time. The authentication guide for Cache Components explains this separation. A cache key that happens to include a user identifier does not replace an authorization check.

Test two accounts with visibly different carts. Interleave requests and navigation. Then log out and request the page again. Also confirm that anonymous visitors never receive private data in the initial HTML or navigation payload.

A loading state should not lie

Use “Loading your cart...” rather than “Your cart is empty” while the read is pending. The latter is a factual claim that may be wrong.

For product availability, distinguish “checking availability” from “out of stock.” Loading states are part of the correctness contract, particularly when a visitor can act before all dynamic content arrives.

Keep critical purchase controls disabled or appropriately guarded until the required current data is available. The server must still enforce those rules on submission.

Verify the design after a content update

Warm the product page, edit the public description, and trigger its invalidation. Confirm that the public content refreshes while carts remain separate.

Then test a slow cart service. The product details should remain useful without hiding the cart delay. Finally, test a failed cart read and verify the recovery behavior.

This pattern is valuable because it assigns different jobs to different parts of the page. It is not a promise that the whole request becomes free. The cart still runs, checkout still validates, and the delivery layer still does work. What changes is that a visitor-specific cart no longer justifies repeating unrelated public content work.

About the author