A comprehensive meal planning application built as a single Cloudflare Worker that combines:
- Frontend UI: Beautiful, responsive meal planning interface
- Backend API: Meal processing and Cloudflare Workers AI integration
- KV Storage: Basic caching for ingredients
- Vectorize: Intelligence caching for ingredients
- Single Deployment: Everything deployed as one worker
- Weekly Meal Planning: Plan breakfast, lunch, and dinner for each day
- AI-Powered Ingredients: Uses Cloudflare Workers AI to generate shopping lists
- Smart Caching: KV-based caching with 1-hour TTL for performance
- Responsive Design: Works perfectly on desktop and mobile
- Week Navigation: Easy navigation between weeks
- Copy to Clipboard: One-click copying of shopping lists
The application is structured as a single Cloudflare Worker with clear separation of concerns:
src/
├── worker.ts # Main worker entry point
├── api/
│ └── meal-planner.ts # API handlers and business logic
└── ui/
└── app.ts # Frontend UI and styling
- Node.js 18+
- Cloudflare account with Workers AI enabled
-
Install dependencies:
npm install
-
Configure environment:
- Ensure Workers AI is enabled in your Cloudflare account
- No additional API keys needed - Workers AI is included with Cloudflare Workers
-
Local development:
npm run dev
-
Build and deploy:
npm run deploy
In your Cloudflare dashboard:
- Ensure Workers AI is enabled for your account
- No additional configuration needed - the AI binding is automatically available
npm run deploy
The worker will be available at:
https://meal-planner.your-subdomain.workers.dev
The wrangler.toml
file is configured for:
- Worker name:
meal-planner
- KV namespace binding:
MEAL_PLANNER_KV
- D1 database binding:
MEAL_PLANNER_DB
- AI binding:
AI
(for Workers AI) - AI Gateway binding:
AI_GATEWAY
(for monitoring and caching) - Main entry point:
src/worker.ts
Make sure your KV namespace is created and the ID is correctly set in wrangler.toml
.
-
Create D1 Database:
wrangler d1 create meal-planner-db
-
Update wrangler.toml: Replace
your-d1-database-id-here
with the actual database ID from step 1. -
Apply Schema:
wrangler d1 execute meal-planner-db --file=./schema.sql
-
Create AI Gateway:
- Go to Cloudflare Dashboard → AI → AI Gateway
- Create a new gateway with ID:
meal-planner-gateway
- Configure rate limits and caching as needed
-
Gateway Features:
- Request Monitoring: Track AI usage and performance
- Caching: Automatic caching of AI responses (1-2 hours)
- Rate Limiting: Control API usage and costs
- Analytics: Detailed insights into AI request patterns
- Navigate to your deployed worker URL
- Plan meals: Click on any meal slot and type your meal
- Save meal plan: Click "💾 Save Meal Plan" to store in D1 database
- Navigate weeks: Use the Previous/Next week buttons (saved plans auto-load)
- Generate shopping list: Click "🧾 Compute Ingredients"
- Copy ingredients: Use the copy button to get your shopping list
GET /
- Serves the meal planner UIPOST /api/compute-ingredients
- Generates shopping list from mealsPOST /api/save-meal-plan
- Saves meal plan to D1 databaseGET /api/get-meal-plan
- Retrieves saved meal plan from D1 databaseGET /api/health
- Health check endpoint
- CORS enabled for cross-origin requests
- Workers AI runs securely within Cloudflare's infrastructure
- Input validation on all API endpoints
- KV Caching: 1-hour TTL for repeated meal plans
- AI Gateway Caching: 1-2 hour TTL for AI responses
- Single Worker: No network latency between frontend and backend
- Optimized Bundle: Minimal JavaScript for fast loading
- CDN Distribution: Global edge deployment via Cloudflare
- AI Request Monitoring: Real-time performance tracking
- KV namespace not found: Check your KV namespace ID in
wrangler.toml
- Claude API errors: Verify your API key is set correctly
- Build errors: Ensure TypeScript is installed and configured
Use npm run dev
to test locally with Wrangler's development server.
This project is open source and available under the MIT License.