Cloudflare Workers Sync Setup
Transform Estudo Organizado into a fullstack serverless application using Cloudflare Pages for hosting and Cloudflare Workers + KV for real-time synchronization across mobile, tablet, and PC.Architecture Overview
The Cloudflare sync implements a Primary/Secondary pattern for optimal performance:- Layer 0 (Core): IndexedDB Local (0ms latency)
- Layer 1 (Edge Network): Push/Pull via Cloudflare Workers (less than 150ms latency)
- Layer 2 (Backup): Google Drive intermittent backup (failsafe)
This setup takes approximately 5-10 minutes and provides ultra-fast synchronization at the edge with automatic conflict resolution based on timestamps.
Prerequisites
- GitHub account with your Estudo Organizado repository
- Cloudflare account (free tier works)
- Access to the
scripts/cloudflare-worker.jsfile in your project
Part A: Deploy to Cloudflare Pages
Host your application on Cloudflare’s global edge network.Access Cloudflare Dashboard
Go to the Cloudflare Dashboard and sign in or create your account.
Create Pages Application
- In the left menu, go to Workers & Pages
- Click the blue Create Application button
- Switch to the Pages tab
Connect to Git
- Click Connect to Git
- Authorize Cloudflare to access your GitHub account
- Select your
estudo-organizadorepository - Click Begin setup
Configure Build Settings
On the Set up builds and deployments screen:
- Project name:
estudo-organizado(or your preferred name) - Framework preset: None (vanilla HTML/JS)
- Build command: Leave blank
- Build output directory:
src
Part B: Create KV Namespace
Set up the ultra-fast key-value storage for your data.Your KV namespace is now created and ready to store synchronized data at the edge.
Part C: Create the Sync API Worker
Deploy the backend API that handles synchronization requests.Create Worker
- In the sidebar, go to Workers & Pages → Overview
- Click Create Application
- Click the Create Worker button
- Name it
estudo-sync-api - Click Deploy
Edit Worker Code
- On the success screen, click Edit code
- Delete all the “Hello World” boilerplate code
- Open
scripts/cloudflare-worker.jsfrom your project - Copy all the code and paste it into the Cloudflare editor
- Click Deploy (top right)
Part D: Configure Security and Bindings
Connect your Worker to the KV database and set up authentication.Access Settings
- From your Worker’s overview page, click the Settings tab
- In the sidebar, click Variables and Secrets
Create Auth Token
- Click the blue + Add button
- Change Type from Text to Secret
- Variable name:
AUTH_TOKEN(must be uppercase) - Value: Enter a strong password (e.g.,
SenhaMuitoForte123) - Click Add variable
Configure the Application
Now connect your deployed app to the sync API.Configure in App
- Open your deployed application
- Go to Settings (⚙️ icon)
- Find the Cloudflare Sync section
- Enter:
- API URL: Your Worker URL
- Auth Token: The password you created in Part D
- Toggle Enable Cloudflare Sync to ON
How It Works
Automatic Synchronization
The sync system automatically runs whenever you make changes:Conflict Resolution
The system uses timestamp-based conflict resolution:Devices with newer data always win. This prevents accidentally overwriting fresh changes with stale data.
Push and Pull Operations
Pull from Cloud:Troubleshooting
Sync status shows 'HTTP Error: 401'
Sync status shows 'HTTP Error: 401'
Your AUTH_TOKEN is incorrect or not configured properly.
- Check that you entered the exact same token in both:
- Worker environment variables
- App settings
- Verify the variable name is
AUTH_TOKEN(uppercase)
Sync status shows 'HTTP Error: 500'
Sync status shows 'HTTP Error: 500'
The KV binding may not be configured correctly.
- Check Worker Settings → Variables
- Verify KV Namespace Binding:
- Variable name:
ESTUDO_KV - Namespace:
ESTUDO_ORGANIZADO_KV
- Variable name:
- Redeploy the worker after making changes
Changes not syncing across devices
Changes not syncing across devices
- Ensure both devices have Cloudflare Sync enabled
- Check that both are using the same API URL and token
- Try manually clicking “Sync Now” on both devices
- Check browser console for error messages
Worker not deploying
Worker not deploying
- Make sure you copied the complete
cloudflare-worker.jscode - Check for syntax errors in the editor
- Try refreshing the page and redeploying
Benefits
Ultra-Low Latency
Edge-based sync with less than 150ms response time worldwide
Automatic Conflicts
Timestamp-based resolution prevents data loss
Always Available
Global CDN ensures your data is always accessible
Free Tier
Generous free limits for personal use
Next Steps
Google Drive Backup
Add an additional backup layer with Google Drive
Manual Backup
Learn about JSON export/import for local backups