Skip to main content
This tutorial was written by Claude Code (an AI) and has not yet been reviewed. Follow along with caution. If the tutorial was helpful or a specific part was not clear/correct, please provide feedback at the bottom of the page. Thank you.

Introduction

Next.js is a powerful React framework that enables you to build fast, production-ready web applications. It’s an excellent choice for headless storefronts with CoCart because of its server-side rendering, API routes, excellent performance, and built-in optimizations. This guide will walk you through setting up a Next.js project configured to work with CoCart API.

Why Next.js for Headless Commerce?

  • Hybrid rendering - Use SSR, SSG, or ISR based on your needs
  • Built-in API routes - Create backend endpoints without a separate server
  • Optimized performance - Automatic code splitting and image optimization
  • SEO friendly - Server-side rendering for better search engine visibility
  • Great DX - Fast refresh, TypeScript support, and comprehensive documentation
  • Vercel deployment - Zero-config deployment with built-in CI/CD

Prerequisites

  • Node.js 18.17 or higher
  • A WordPress site with WooCommerce installed
  • CoCart plugin installed and activated
  • Basic knowledge of React and JavaScript

Creating a New Next.js Project

Create a new Next.js project using the official CLI:
When prompted, choose the following options:
  • Would you like to use TypeScript? → Yes (recommended) or No
  • Would you like to use ESLint? → Yes (recommended)
  • Would you like to use Tailwind CSS? → Yes (recommended)
  • Would you like your code inside a src/ directory? → Yes (recommended)
  • Would you like to use App Router? → Yes (recommended)
  • Would you like to use Turbopack for next dev? → Yes (optional, for faster dev)
  • Would you like to customize the import alias? → No (unless you have preference)
Navigate to your project:

Project Structure

Your Next.js project will have this structure:
Create the necessary folders:

Environment Configuration

Create a .env.local file in your project root:
Variables prefixed with NEXT_PUBLIC_ are exposed to the browser. Server-only variables should not have this prefix.
Add .env.local to your .gitignore (it should already be there by default):
Create a .env.example for your team:

Creating the CoCart API Client

Create a centralized API client to interact with CoCart. Create src/lib/cocart.ts (or .js if not using TypeScript):
We are currently building out this client, so for now just make standard fetch requests to the CoCart API endpoints as needed.

Creating API Routes

Next.js API routes allow you to create backend endpoints. Create an API route for cart operations. Create src/app/api/cart/add/route.ts:

Updating the Root Layout

Update your root layout at src/app/layout.tsx:

Testing Your Setup

Update the homepage at src/app/page.tsx:

Running Your Project

Start the development server:
Visit http://localhost:3000 to see your store.

Building for Production

Build your site for production:
Start the production server locally:

Caching Strategies

Next.js offers several caching strategies for optimal performance:

Static Generation (SSG)

For pages that can be pre-rendered at build time:

Incremental Static Regeneration (ISR)

For pages that need periodic updates:

Server-Side Rendering (SSR)

For dynamic pages that need fresh data:

Deployment Options

Next.js sites can be deployed to various platforms:
  • Vercel - Zero configuration deployment (recommended)
  • Netlify - Easy deployment with built-in features
  • AWS Amplify - Full-stack deployment
  • Docker - Containerized deployment
  • Your own server - Node.js server required

Deploying to Vercel

  1. Push your code to GitHub, GitLab, or Bitbucket
  2. Import your project on Vercel
  3. Add your environment variables
  4. Deploy!

Next Steps

Now that your Next.js project is set up with CoCart:
  1. Add shopping cart functionality with React Context or Zustand
  2. Implement checkout flow
  3. Add user authentication with JWT
  4. Optimize images with Next.js Image component
  5. Add loading states and error boundaries

Troubleshooting

CORS Errors

If you encounter CORS errors, you may need to configure WordPress to allow cross-origin requests. See CORS documentation.

API Connection Issues

  1. Verify your NEXT_PUBLIC_STORE_URL is correct in .env.local
  2. Ensure CoCart is installed and activated
  3. Check that WooCommerce is configured properly
  4. Test API endpoints directly in your browser or Postman

Hydration Errors

If you see hydration mismatches:
  • Ensure you’re not using browser-only APIs during SSR
  • Use 'use client' directive for client-only components
  • Check that your data is consistent between server and client renders

Resources