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

SvelteKit is a modern web framework for building high-performance applications with Svelte. It’s an excellent choice for headless storefronts with CoCart because of its speed, developer experience, and flexible rendering options. This guide will walk you through setting up a SvelteKit project configured to work with CoCart API.

Why SvelteKit for Headless Commerce?

  • Fast and lightweight - Minimal JavaScript bundle with reactive components
  • Flexible rendering - Choose between SSR, SSG, or CSR per route
  • Built-in routing - File-based routing with layouts and nested routes
  • Server-side capabilities - API routes and server-only code with +server.ts and +page.server.ts
  • Great DX - Hot module replacement, TypeScript support, and excellent tooling
  • Progressive enhancement - Works without JavaScript, enhanced when available

Prerequisites

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

Creating a New SvelteKit Project

Create a new SvelteKit project using the official CLI:
When prompted, choose the following options:
  • Which Svelte app template? → Skeleton project
  • Add type checking with TypeScript? → Yes, using TypeScript syntax (recommended)
  • Select additional options
    • Add ESLint for code linting (recommended)
    • Add Prettier for code formatting (recommended)
Navigate to your project:
Install dependencies:

Installing Tailwind CSS

Install Tailwind CSS using the SvelteKit integration:
This will:
  • Install Tailwind CSS and its dependencies
  • Create tailwind.config.js and postcss.config.js files
  • Add necessary imports to your app

Project Structure

Your SvelteKit project should have this structure:
SvelteKit distinguishes between client and server code. Code in src/lib/server is only bundled for the server and never sent to the client.

Environment Configuration

Create a .env file in your project root:
Variables prefixed with PUBLIC_ are exposed to the client-side code. Keep sensitive data in server-only environment variables without the PUBLIC_ prefix.
Add .env to your .gitignore:
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:

Creating a Root Layout

Create a root layout at src/routes/+layout.svelte:

Loading Data with Server-Side Rendering

SvelteKit uses +page.server.ts files to load data on the server before rendering. Create src/routes/+page.server.ts:
Then create the page at src/routes/+page.svelte:
SvelteKit automatically generates TypeScript types for your route data. The PageData type is auto-generated based on your load function’s return value.

Creating API Routes

SvelteKit supports API routes using +server.ts files. Create src/routes/api/cart/add/+server.ts:

Managing Cart State with Stores

SvelteKit works seamlessly with Svelte stores for client-side state management. Create src/lib/stores/cart.ts:
Use the store in your components:

Running Your Project

Start the development server:
Visit http://localhost:5173 to see your store.
Use npm run dev -- --open to automatically open the browser.

Building for Production

Build your site for production:
Preview the production build:

Deployment Options

SvelteKit supports multiple adapters for different deployment platforms:
Install the Vercel adapter:
Update svelte.config.js:
Install the Netlify adapter:
Update svelte.config.js:
Install the Cloudflare adapter:
Update svelte.config.js:
Install the Node adapter:
Update svelte.config.js:

Advanced Configuration

Form Actions

SvelteKit’s form actions provide a way to handle form submissions with progressive enhancement:

Hooks for Global Request Handling

Use src/hooks.server.ts for global request handling like authentication:

Troubleshooting

If you encounter CORS errors, you may need to configure WordPress to allow cross-origin requests. See the CORS documentation.
  1. Verify your 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
If you encounter build errors:
  1. Clear the .svelte-kit directory: rm -rf .svelte-kit
  2. Reinstall dependencies: npm install
  3. Try building again: npm run build

Next Steps

Now that your SvelteKit project is set up with CoCart:
  1. Build product listing pages with dynamic routes
  2. Create a shopping cart page with real-time updates
  3. Implement checkout functionality
  4. Add user authentication with JWT
  5. Optimize for performance with preloading and caching

Resources