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.tsand+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:- 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)
Installing Tailwind CSS
Install Tailwind CSS using the SvelteKit integration:- Install Tailwind CSS and its dependencies
- Create
tailwind.config.jsandpostcss.config.jsfiles - Add necessary imports to your app
Project Structure
Your SvelteKit project should have this structure: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..env to your .gitignore:
.env.example for your team:
Creating the CoCart API Client
Create a centralized API client to interact with CoCart. Createsrc/lib/cocart.ts:
CoCart API Client Code
CoCart API Client Code
Creating a Root Layout
Create a root layout atsrc/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:
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:
API Route Example
API Route Example
Managing Cart State with Stores
SvelteKit works seamlessly with Svelte stores for client-side state management. Createsrc/lib/stores/cart.ts:
Cart Store Implementation
Cart Store Implementation
Running Your Project
Start the development server:http://localhost:5173 to see your store.
Building for Production
Build your site for production:Deployment Options
SvelteKit supports multiple adapters for different deployment platforms:Vercel
Vercel
Install the Vercel adapter:Update
svelte.config.js:Netlify
Netlify
Install the Netlify adapter:Update
svelte.config.js:Cloudflare Pages
Cloudflare Pages
Install the Cloudflare adapter:Update
svelte.config.js:Node.js Server
Node.js Server
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
Usesrc/hooks.server.ts for global request handling like authentication:
Troubleshooting
CORS Errors
CORS Errors
If you encounter CORS errors, you may need to configure WordPress to allow cross-origin requests. See the CORS documentation.
API Connection Issues
API Connection Issues
- Verify your
PUBLIC_STORE_URLis correct in.env - Ensure CoCart is installed and activated
- Check that WooCommerce is configured properly
- Test API endpoints directly in your browser or Postman
Build Errors
Build Errors
If you encounter build errors:
- Clear the
.svelte-kitdirectory:rm -rf .svelte-kit - Reinstall dependencies:
npm install - Try building again:
npm run build
Next Steps
Now that your SvelteKit project is set up with CoCart:- Build product listing pages with dynamic routes
- Create a shopping cart page with real-time updates
- Implement checkout functionality
- Add user authentication with JWT
- Optimize for performance with preloading and caching