Deploying to Cloudflare Workers
This comprehensive guide walks you through deploying your Hydrogen storefront to Cloudflare Workers, providing a fast, globally distributed hosting solution for your e-commerce site.Quick Start
For experienced users, here’s the condensed deployment process:Prerequisites
Before you begin, ensure you have the following:- Node.js: Version 20.0.0 or higher
- Cloudflare Account: Sign up at cloudflare.com if you haven’t already
- Wrangler CLI: Install globally with
npm install -g wrangler - Shopify Store: Active Shopify store with Storefront API access
- Weaverse Project: Configured Weaverse project for your storefront
Required Dependencies
Install the Cloudflare Vite plugin, which is essential for building your application for the Cloudflare Workers runtime:All dependencies should be installed before proceeding with the deployment steps.
Configuration Overview
Your project includes optimized configurations for Cloudflare Workers deployment. Here’s what you need to know about the key configuration files.These configurations are pre-optimized for Cloudflare Workers. You typically won’t need to modify them unless you have specific requirements.
React Router Configuration
Thereact-router.config.ts file is configured for server-side rendering (SSR) deployment:
ssr: true- Enables server-side rendering for optimal performanceunstable_viteEnvironmentApi: true- Required for Vite’s environment API integration
Vite Configuration
Thevite.config.ts file includes Cloudflare-specific plugins and build optimizations:
@cloudflare/vite-pluginwith SSR environment configurationassetsInlineLimit: 0ensures Content-Security-Policy compliance- Oxygen deployment plugin is commented out (not needed for Cloudflare)
Environment Setup
1. Authenticate with Cloudflare
Begin by authenticating your Wrangler CLI with your Cloudflare account:2. Configure Environment Variables
Environment variables are managed throughwrangler.json. Update the following variables with your actual production values:
Setting
NODE_ENV to "production" is crucial to prevent Hydrogen from adding virtual routes during the build process, which can cause deployment issues.3. Update Project Configuration
Verify yourwrangler.json has the correct project name and settings:
name field with your desired project name, which will become part of your deployment URL.
Available Scripts
Your project includes several npm scripts specifically designed for Cloudflare Workers deployment:cf-typegen- Generates TypeScript types for Cloudflare Workers and React Routercheck- Performs comprehensive validation: TypeScript checking, React Router build, and dry-run deploymentdeploy- Deploys your application to Cloudflare Workerscf-dev- Runs the application locally in the Cloudflare Workers environment for testing
Deployment Steps
Follow these steps to deploy your storefront to Cloudflare Workers:1. Install Dependencies
Install all project dependencies:2. Build the Application
Build your application for production deployment:- Generates GraphQL types from your queries
- Builds the application bundle optimized for production
- Prepares assets for the Cloudflare Workers runtime environment
3. Test Locally (Optional but Recommended)
Before deploying to production, test your application locally in the Cloudflare Workers environment:4. Run Pre-deployment Validation
Execute comprehensive pre-deployment checks to catch potential issues:- TypeScript type checking to ensure code quality
- React Router build validation
- Cloudflare Workers dry-run deployment to identify configuration problems
5. Deploy to Production
Deploy your application to Cloudflare Workers:Your storefront is now live on Cloudflare Workers! The deployment URL will be displayed in your terminal.
Post-Deployment Verification
After successful deployment, Wrangler will display your application’s live URL (typicallyhttps://your-project-name.your-subdomain.workers.dev).
Monitor Deployment Status
Monitor your deployment in real-time and check for any runtime errors:Keep the log tail running during your initial testing to catch any runtime errors immediately.
Configure Custom Domain (Optional)
To use your own domain instead of the default Workers subdomain:- Access your Cloudflare Dashboard
- Navigate to Workers & Pages
- Select your deployed worker
- Go to the Settings tab
- Find the Domains & Routes section
- Add your custom domain(s) and configure routing rules
Troubleshooting
Common Issues and Solutions
-
Build Failures
- Verify Node.js version 20.0.0 or higher is installed
- Ensure all dependencies are installed:
npm install - Check for TypeScript errors:
npm run typecheck
-
Environment Variables
- Double-check all required variables are set in
wrangler.json - Verify variable names match exactly (case-sensitive)
- Ensure sensitive values are properly escaped
- Double-check all required variables are set in
-
Session Secret
- Generate a secure, random string for
SESSION_SECRET - Use at least 32 characters for security
- Never reuse secrets across environments
- Generate a secure, random string for
-
Shopify API Configuration
- Verify your Storefront API token has correct permissions
- Confirm your store domain and Shop ID are accurate
- Check that Customer Account API is properly configured
Performance Optimization
- Observability: Enable in
wrangler.jsonfor production monitoring - Source Maps: Upload source maps for better error tracking
- Caching: Leverage Cloudflare’s caching features for static assets
- Analytics: Monitor performance with Cloudflare’s analytics dashboard
Deployment Rollback
If you need to rollback to a previous deployment:If you encounter persistent issues not covered in this guide, don’t hesitate to reach out to our community or support team for assistance.
Additional Resources
Official Documentation
- Cloudflare Workers Documentation - Comprehensive guide to Cloudflare Workers
- Wrangler CLI Reference - Complete command reference
- Shopify Hydrogen Deployment Guide - Hydrogen-specific deployment information
Weaverse Resources
- Weaverse Documentation - Learn more about Weaverse platform features
- Weaverse Community - Get help from the community
Support
If you encounter issues not covered in this guide:- Join our Slack Community for real-time support
- Contact Weaverse Support for enterprise assistance