How adrianhajdin/ecommerce_sanity_stripe works
Project Type and Purpose
• This is a modern full-stack e-commerce web application built on Next.js (React) with server-side rendering/static generation, React Context for state, Stripe for payments, and Sanity as a headless CMS.
• The “sanity_ecommerce” folder is the separate Sanity project that provides content (products, banners) via its APIs.
High-Level File Structure Analysis
• Top-level files: .env (API keys), next.config.js, package.json, build/lint config.
• “pages/”: Next.js page routes (UI and serverless backend endpoints under pages/api).
• “components/”: React presentation components (Navbar, Cart, Product, etc.).
• “context/StateContext.js”: React Context managing cart state.
• “lib/”: helper modules for Sanity client, Stripe integration, utilities.
• “public/”: static assets served at root.
• “styles/”: global and module CSS.
• “sanity_ecommerce/”: standalone Sanity studio code with schemas, config, and built static admin UI.
Key Architecture Insights from README
• Uses Next.js data-fetching methods (getStaticProps, getServerSideProps) to retrieve product/banners from Sanity.
• Implements serverless API routes for initiating Stripe checkout sessions.
• Leverages React Context API for cart management on the client.
• Sanity handles content management (no custom database).
Main Components
a. Frontend (Next.js Application)
– UI Components (components/…)
– State Management (React Context)
– Data-fetching layer (getStaticProps / getServerSideProps)
b. Serverless Backend (Next.js API)
– /api/stripe.js (creates Stripe checkout sessions)
– /api/hello.js (demo endpoint)
c. Headless CMS (Sanity)
– Sanity Studio (admin UI in sanity_ecommerce/dist)
– Sanity Content API (product/banner data)
d. External Service (Stripe)
– Payments, checkout, shipping rates
e. Static Assets & CDN
– Public folder assets, built pages, and CMS static (Sanity studio build)
Relationships and Data Flows
• Browser → Next.js frontend: navigates pages, React renders UI.
• Next.js frontend ↔ Sanity Content API: fetch product lists/details via client.js in lib.
• Next.js frontend ↔ React Context: add/remove items in cart.
• When user checks out: Next.js API route (/api/stripe) → Stripe API to create session → returns session URL → front end redirects user.
• Sanity Studio (admin) → Sanity Content API: editors update product content, which Next.js fetches on next build or request.
Architectural Patterns and Principles
• Jamstack / hybrid: static generation + serverless functions.
• Headless CMS integration: decoupled content management.
• Single Page App UX with server-rendered pages for performance.
• Client-side state via Context to avoid prop drilling.
• Secure secret handling in .env and API routes.
Final Recommendations
• Be exhaustive: break down each folder’s role.
• Build/deploy flow: Next.js app → Vercel/Cdn, Sanity Studio → netlify/docker, Stripe always external.
• Bearer token/env usage confined to serverless functions.