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

Nuxt is a powerful Vue framework that makes web development intuitive and performant. It’s an excellent choice for building headless storefronts with CoCart because of its server-side rendering capabilities, excellent developer experience, and robust ecosystem. This guide will walk you through setting up a Nuxt project configured to work with CoCart API. Instructions are provided for both Nuxt 3 (stable, widely supported) and Nuxt 4 (latest release with new features).
Which version should you use?
  • Nuxt 3: Recommended for production applications. Stable with extensive ecosystem support and long-term maintenance until January 2026.
  • Nuxt 4: Latest features and improvements. Good for new projects that want cutting-edge capabilities. Officially released in 2025.

Why Nuxt for Headless Commerce?

  • Hybrid rendering - Choose between SSR, SSG, or CSR per route
  • Auto imports - Components, composables, and utilities are automatically imported
  • File-based routing - Intuitive routing system with dynamic routes
  • Server routes - Built-in API routes with full-stack capabilities
  • Vue ecosystem - Access to the entire Vue.js ecosystem and components
  • SEO friendly - Built-in SEO features and meta tag management

Prerequisites

  • Node.js 16.10.0 or higher (18.0.0+ recommended)
  • A WordPress site with WooCommerce installed
  • CoCart plugin installed and activated
  • Basic knowledge of JavaScript and command line

Creating a New Nuxt Project

Create a new Nuxt 3 project using the official CLI:
When prompted, choose your preferred package manager (npm, yarn, or pnpm).Navigate to your project:
Install dependencies:

Installing Tailwind CSS

Install Tailwind CSS using the Nuxt module:
Add the module to your nuxt.config.ts:
Create a tailwind.config.js file (optional, for customization):

Project Structure

Your Nuxt project should have this structure:
Nuxt automatically imports components from the components/ directory and composables from the composables/ directory. No need for manual imports!

Environment Configuration

Create a .env file in your project root:
Variables prefixed with NUXT_PUBLIC_ are exposed to the client-side code. Keep sensitive data in server-only environment variables without the NUXT_PUBLIC_ prefix.
Add .env to your .gitignore:
Create a .env.example for your team:

Creating the CoCart Composable

Create a composable to interact with CoCart. Create composables/useCoCart.js:
This composable is automatically imported in all your components and pages. Just call const { getProducts } = useCoCart() to use it!

Creating a Default Layout

Create a default layout at layouts/default.vue:

Creating Your First Page

Create a home page at pages/index.vue:
The useAsyncData composable automatically handles server-side rendering and client-side hydration. Data fetched on the server is serialized and sent to the client.

Creating Server API Routes

Nuxt supports server API routes for server-side operations. Create server/api/cart/add.post.js:
Server routes are automatically prefixed with /api. This route will be accessible at /api/cart/add.

Managing Cart State

Create a cart composable for managing cart state at composables/useCartState.js:
Use the cart state in your components:

SEO and Meta Tags

Nuxt makes it easy to manage SEO with the useSeoMeta composable:

Running Your Project

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

Building for Production

Build your site for production:
Preview the production build:

Deployment Options

Nuxt can be deployed to various platforms:
Zero configuration deployment
Nuxt automatically detects Vercel and configures itself appropriately.
Easy deployment with built-in featuresCreate a netlify.toml file:
Connect your repository to Netlify and deploy.
Global edge network deployment
  1. Connect your Git repository to Cloudflare Pages
  2. Set build command: npm run build
  3. Set build output directory: .output/public
  4. Deploy
Nuxt automatically detects Cloudflare Pages and configures itself.
Deploy to any Node.js hostingAfter running npm run build, you can start the production server:
Or use PM2 for process management:
Generate a static siteFor fully static sites, you can use:
This creates a .output/public directory that can be deployed to any static hosting service like GitHub Pages, AWS S3, or Nginx.

Version-Specific Features

Nuxt 3 Specific Features

  • Stable ecosystem: All major modules and libraries are fully compatible
  • Long-term support: Maintenance until January 2026
  • Production-ready: Battle-tested in thousands of applications
  • Extensive documentation: Comprehensive guides and community resources

Troubleshooting

If you encounter CORS errors, you may need to configure WordPress to allow cross-origin requests. See CORS documentation.You can also add CORS headers in your Nuxt config:
  1. Verify your NUXT_PUBLIC_STORE_URL is correct in .env
  2. Ensure CoCart is installed and activated
  3. Check that WooCommerce is configured properly
  4. Test API endpoints directly in your browser or Postman
Debug tip: Add logging to your composable:
If you see hydration mismatch warnings:
  1. Ensure data fetching is done with useAsyncData or useFetch
  2. Check that your component structure matches between server and client
  3. Avoid using browser-only APIs during SSR
  4. Use <ClientOnly> for client-side only components:
Some Nuxt modules may not yet support Nuxt 4. Check the module’s documentation for compatibility.If a module isn’t compatible yet:
  • Check for updates or beta versions
  • Look for alternative modules
  • Consider staying on Nuxt 3 until the module is updated
  • Report the issue to the module maintainer

Next Steps

Now that your Nuxt project is set up with CoCart:
  1. Build product listing pages with dynamic routes
  2. Create a shopping cart component with real-time updates
  3. Implement checkout functionality
  4. Add user authentication with JWT
  5. Optimize images with Nuxt Image module
  6. Add PWA capabilities with @vite-pwa/nuxt

Resources