Next.js 16 Commerce
A commerce demo for exploring Cache Components, Partial Prefetching, and static guarantees in Next.js.
Live demo →
This repository started as the demo for my talk, Composition, Caching, and Architecture in Modern Next.js. Since then, the current app has moved to the latest Next.js canary and follows the architecture from the Next.js App Architecture skill and Component Architecture for React Server Components.
Features
- Cache Components mix static, cached, and request-time content in the same route. Shared product data uses
'use cache', while cookie-backed cart and saved state use 'use cache: private'.
- Partial Prefetching prefetches the shared route shell. Product links use
prefetch={true} so cached product details are available before navigation, while personalized state waits for the visit.
ensureStatic guarantees static output at the stage a route needs it. The store layout guarantees its shell, product routes guarantee each prefetch, and the About page guarantees a full navigation.
- Server Functions update the cart, saved products, account details, and featured products on the server, then invalidate only the cache tags they change with
updateTag.
- React Compiler memoizes components and hooks automatically, so the code needs no manual
useMemo or useCallback.
- View Transitions animate personalized content as it streams in from Suspense.
- Async React keeps the interface responsive during server work with
Suspense, useOptimistic, useTransition, and use.
Original talk branches
starter is the intentionally unoptimized starting point used during the talk.
route-group separates static and personalized routes with route groups.
request-context encodes request context in the route instead.
legacy-talk-demo preserves the demo before the later cart and Next.js 16.4 work.
The main branch is the current version and continues to evolve with Next.js.
Getting started
The app runs on Postgres. Copy .env.sample to .env.local, set DATABASE_URL, then:
pnpm install
pnpm run prisma.push
pnpm run prisma.seed
pnpm run dev
Open http://localhost:3000 in your browser. You can inspect the database with pnpm run prisma.studio.
Run `main` locally with SQLite
Change the datasource provider in prisma/schema.prisma from postgresql to sqlite, then add this to .env.local:
DATABASE_URL="file:./dev.db"
Generate the SQLite client and create the local database:
pnpm exec prisma generate
pnpm run prisma.push
pnpm run prisma.seed
The schema and application code otherwise stay the same.
Testing
The end-to-end tests use @next/playwright with the instant() API to verify the static shell, product prefetches, and personalized state across navigations.
pnpm test:e2e
Static checks:
pnpm lint
pnpm exec tsc --noEmit
Stack
- Next.js canary: App Router, Cache Components, Partial Prefetching,
ensureStatic, and Server Functions
- React with React Compiler: Suspense, View Transitions,
useOptimistic, and use
- TypeScript and Tailwind CSS v4
- Prisma on PostgreSQL
- Ariakit for accessible dialogs and popovers
- Playwright with
@next/playwright for end-to-end tests
License
MIT
评论