Course map
Stage 1: App Router Foundations
Stage 2: The Rendering Model
Stage 3: Data, Caching and Mutations
Stage 4: Full-Stack Next.js
Stage 5: Production and Specialization

Project

QuickCart

Start after Part 16

Stage 4

Goal: A small, real, full-stack e-commerce slice proving Stages 3–4 work together, not just each part on its own — a cached product catalog, a cart that mutates through Server Actions, an admin edit that revalidates the public page it changed, checkout gated behind a real session cookie, a JSON order-status endpoint any curl can hit, and every bit of cart/order/product data surviving an actual server restart. Time: ~10 h · Uses: Parts 9–16 (Server Components, caching, revalidation, Server Actions, Route Handlers, middleware/proxy, the auth pattern, SQLite) — the first project to combine every stage since Stage 2's Server/Client boundary alone.

Project brief

QuickCart: a five-item grocery catalog (Alphonso Mangoes, Basmati Rice, Toor Dal, Amul Butter, Darjeeling Tea), a cart identified by its own cookie (no login needed to browse or add to cart — only to check out), a checkout flow gated behind Part 15's iron-session pattern, an admin screen that edits a product's price/stock and revalidates the public catalog in the same running process, and a read-only JSON order-status endpoint. Every row — products, cart lines, orders — lives in a real SQLite file (better-sqlite3, Part 16), reusing Part 15's app/account/ auth universe directly rather than rebuilding a second login system.

A real run against this project (the full transcript is in tests_manual.md):

$ curl -s http://localhost:PORT/api/quickcart/orders/8d599808-66a8-4440-a3fe-cbfacf80055f
{"id":"8d599808-66a8-4440-a3fe-cbfacf80055f","status":"placed","totalCents":15900,"createdAt":"2026-12-01T12:51:51.315Z","items":[{"name":"Toor Dal (1kg)","quantity":1}]}
$ curl -s -D - -o /dev/null http://localhost:PORT/quickcart/checkout   # no session cookie
HTTP/1.1 307 Temporary Redirect
location: /account/login

Files

File What it is
README.md This brief, the implementation map, and the ticked acceptance checklist
tests_manual.md Every curl/build-output check run for real, with the exact captured output
examples/ Numbered snapshot copies of the reference implementation (the live code is added to the shared harness — see "Implementation location" below)

How to run

This project adds routes to the course's one shared harness, under harness/app/quickcart/ (plus harness/app/api/quickcart/ for the Route Handler and two small extensions to harness/app/account/'s universe: proxy.ts's matcher, and importing its requireUser). No separate install, no separate package.json — better-sqlite3 and iron-session were already installed in Parts 15–16.

cd ../../harness
npm run dev          # then open http://localhost:3000/quickcart

Demo accounts (seeded in Part 15's app/account/lib/users.ts, reused as-is):

A fresh SESSION_PASSWORD must exist in harness/.env.local (Part 15's setup note — openssl rand -base64 32); QuickCart's checkout/admin screens reuse that exact session, not a second one.

Implementation location

harness/app/quickcart/
├── page.tsx                   Product list — Server Component, cached (Part 10)
├── [id]/page.tsx              Product detail — Server Component, cached per-product
├── cart/page.tsx              Cart — dynamic (reads the cart cookie), never cached
├── checkout/page.tsx          Checkout — gated by requireUser() called IN the page (Part 15)
├── orders/[id]/page.tsx       Order confirmation — gated the same way, owner-or-admin only
├── admin/page.tsx             Admin product list — gated by requireUser() + role check
├── admin/[id]/page.tsx        Admin edit form
├── admin/actions.ts           updateProductAction — Part 11's revalidation, for real
├── actions.ts                 addToCartAction / removeFromCartAction / placeOrderAction
└── lib/
    ├── db.ts                  better-sqlite3 connection, WAL, migration + idempotent seed
    ├── products.ts            Plain DAL: listProducts/getProduct/updateProduct
    ├── cached-products.ts     "use cache" wrappers, cacheTag per-list and per-product
    ├── cart-session.ts        The cart's own cookie (separate from Part 15's session cookie)
    ├── cart.ts                Cart DAL (an UPSERT via ON CONFLICT, a join to products)
    └── orders.ts               Order DAL — placeOrder() is a real db.transaction (Part 16)
harness/app/api/quickcart/orders/[id]/route.ts   Read-only JSON order-status (Part 13)
harness/proxy.ts                                 Extended (Part 14/15) to cover /quickcart/checkout and /quickcart/admin

QuickCart imports requireUser directly from ../account/lib/auth — Part 15's own DAL, not a copy of it. This is deliberate: the whole point of a capstone is proving the earlier parts' real code composes, not that the pattern can be reimplemented a second time.

Features and which Parts they use

Feature What it proves Part
Product list + detail, cached A Server Component awaiting a "use cache"-wrapped read with a simulated 600ms cost; next build's route table shows the list fully static (○) Part 9 / Part 10
Admin edit → public page updates, same request updateProductAction calls updateTag on both the shared list tag and the product's own tag; the public catalog shows the new price with no rebuild Part 11
Add/remove cart, place order Three Server Actions, zero client-side fetch — addToCartAction sets a cart cookie and redirects; placeOrderAction re-verifies the session inside the action (Part 12's rule) and runs a real db.transaction Part 12 / Part 16
GET /api/quickcart/orders/[id] Plain Request/Response, a 404 on an unknown id, no auth (an order id is a random UUID, not a sequential number — flagged explicitly in tests_manual.md, not silently assumed safe) Part 13
Checkout/admin gated by proxy.ts + the DAL proxy.ts's matcher now covers /quickcart/checkout and /quickcart/admin, running the exact same optimistic iron-session check Part 15 built for /account/dashboard Part 14
Checkout's real protection checkout/page.tsx calls requireUser() itself, not just a shared layout — Part 15 section 4's reproduced finding, applied for real this time Part 15
Every row survives a restart Products, cart lines, and orders are all real SQLite rows (better-sqlite3, WAL mode) — confirmed by stopping the server process and starting a new one Part 16

How it was built — notable decisions, not a step-by-step

This project builds on four already-finished parts rather than introducing new APIs, so the interesting decisions were about composition:

  • The cart cookie is separate from the Part 15 session cookie. A visitor can add items to a cart before ever logging in (a real storefront's own UX) — only checkout/page.tsx and its Server Action require requireUser(). cart-session.ts's getOrCreateCartId() can only run inside a Server Action (where cookies().set(...) is legal); the cart page's own Server Component render uses the read-only getCartId() instead and never tries to create one.
  • Two cache tags per product, not one. quickcart-products (the shared list) and quickcart-product-${id} (that one product's detail page) are both set on cached-products.ts's two functions, and admin/actions.ts calls updateTag on both after a write — proven in tests_manual.md to invalidate exactly the right pages, nothing more, nothing less.
  • A real concurrency bug, found and fixed during the build, not hypothesized. The first version of db.ts seeded products with a SELECT COUNT(*); if 0, INSERT check. next build runs page-data collection across many parallel workers, each importing db.ts independently — more than one worker read 0 before any had committed a row, and the build failed on a UNIQUE constraint failed: products.id error. Fixed with INSERT OR IGNORE, which is safe no matter how many workers race it. See tests_manual.md for the real error and the fix.
  • A second real bug, found by replaying a Server Action directly. placeOrderAction originally had no empty-cart guard — checkout/page.tsx already hides the "Place order" button once the cart is empty, but Part 12's own rule ("every Server Action is a public POST endpoint") means that's just UI. Replaying the exact same curl POST a second time hit placeOrder() with nothing left in the cart and crashed with a raw 500. Fixed with an explicit getCartLines(cartId).length === 0 check that redirects to the cart instead. Both the crash and the fix are captured in tests_manual.md.
  • The order-status Route Handler is deliberately unauthenticated, matching Part 13's PNR-status pattern exactly — an order id is a randomUUID(), not a guessable sequential number, which is the actual reason this is an acceptable "read-only tracking link" design, not an oversight. A production system wanting a stronger guarantee would add a per-order access token to the URL; this project states that trade-off rather than hiding it.
  • Two different "not authenticated" responses, side by side, on purpose. /quickcart/checkout and /quickcart/admin are in proxy.ts's matcher, so an unauthenticated visitor gets a real top-level 307 before any React rendering happens. /quickcart/orders/[id] is not in that matcher — its only protection is requireUser() inside the page itself, which (Part 15 section 5's finding) always returns 200 with the redirect embedded in the stream. Both are real, verified, and intentionally different — see tests_manual.md for both responses captured back to back.

Checklist — acceptance sketch, finalized and ticked from observed behavior

  • Product listing and detail pages are Server Components (Part 9) with caching applied to at least one expensive read (Part 10) — getCachedProductList/getCachedProduct, both "use cache" + cacheTag, both carrying a simulated 600ms cost; next build's route table shows /quickcart fully static (○).
  • Adding/removing a cart item goes through a Server Action (Part 12), not a client-side fetch — addToCartAction/removeFromCartAction, both "use server", both invoked from a plain <form action={...}> with no JavaScript fetch anywhere in this project's code.
  • An admin-style edit to a product revalidates the public page that shows it (Part 11) — updateProductAction → updateTag on both tags; a real price change (₹899.00 → ₹799.00) showed up on both the list and detail pages on the same running server, no rebuild (tests_manual.md, section 5).
  • Checkout is gated behind the Part 15 auth pattern — an unauthenticated visitor is redirected, not shown a broken page — proxy.ts's optimistic check returns a real 307 to /account/login; checkout/page.tsx's own requireUser() call is the authoritative check underneath it.
  • A Route Handler (Part 13) exposes a read-only, JSON order-status endpoint, testable with curl — GET /api/quickcart/orders/[id], plain Request/Response, a real 404 for an unknown id, verified directly with curl (tests_manual.md, section 6).
  • Cart and order data persist to SQLite (Part 16) and survive a server restart — an order placed on one server process, a product price edited by an admin on that same process, both still present after the process was fully stopped and a new one started on a different port (tests_manual.md, section 7).

Next

🎉 Stage 4 done! Go back to the roadmap and start Stage 5: Production and Specialization.

Search the guide