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
- Nuxt 3
- Nuxt 4
- 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
- Nuxt 3
- Nuxt 4
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:nuxt.config.ts
:
- Nuxt 3
- Nuxt 4
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..env
to your .gitignore
:
.env.example
for your team:
Creating the CoCart Composable
Create a composable to interact with CoCart. Createcomposables/useCoCart.js
:
- Nuxt 3
- Nuxt 4
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 atlayouts/default.vue
:
Creating Your First Page
Create a home page atpages/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. Createserver/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 atcomposables/useCartState.js
:
SEO and Meta Tags
Nuxt makes it easy to manage SEO with theuseSeoMeta
composable:
Running Your Project
Start the development server:http://localhost:3000
to see your store.
Building for Production
Build your site for production:Deployment Options
Nuxt can be deployed to various platforms:Vercel
Vercel
Zero configuration deploymentNuxt automatically detects Vercel and configures itself appropriately.
Netlify
Netlify
Easy deployment with built-in featuresCreate a Connect your repository to Netlify and deploy.
netlify.toml
file:Cloudflare Pages
Cloudflare Pages
Global edge network deployment
- Connect your Git repository to Cloudflare Pages
- Set build command:
npm run build
- Set build output directory:
.output/public
- Deploy
Node.js Server
Node.js Server
Deploy to any Node.js hostingAfter running Or use PM2 for process management:
npm run build
, you can start the production server:Static Hosting
Static Hosting
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
- Nuxt 4
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
Recommended Modules for Nuxt 3
Troubleshooting
CORS Errors
CORS Errors
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:
API Connection Issues
API Connection Issues
- Verify your
NUXT_PUBLIC_STORE_URL
is correct in.env
- Ensure CoCart is installed and activated
- Check that WooCommerce is configured properly
- Test API endpoints directly in your browser or Postman
Hydration Mismatch Errors
Hydration Mismatch Errors
If you see hydration mismatch warnings:
- Ensure data fetching is done with
useAsyncData
oruseFetch
- Check that your component structure matches between server and client
- Avoid using browser-only APIs during SSR
- Use
<ClientOnly>
for client-side only components:
Module Compatibility (Nuxt 4)
Module Compatibility (Nuxt 4)
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:- Build product listing pages with dynamic routes
- Create a shopping cart component with real-time updates
- Implement checkout functionality
- Add user authentication with JWT
- Optimize images with Nuxt Image module
- Add PWA capabilities with
@vite-pwa/nuxt