This website includes real-time copy tracking for install scripts with session-based rate limiting (1 copy per 10 minutes per session).
website/
├── index.html # Main website
├── api/
│ ├── copy-stats.ts # Cloudflare Worker API
│ └── README.md # API documentation
└── DEPLOYMENT.md # This file
-
Install Wrangler CLI:
npm install -g wrangler
-
Create KV Namespace:
wrangler kv:namespace create "COPY_STATS_KV" wrangler kv:namespace create "COPY_STATS_KV" --preview
-
Create
wrangler.tomlinwebsite/api/:name = "copy-stats-api" main = "copy-stats.ts" compatibility_date = "2024-01-01" [[kv_namespaces]] binding = "COPY_STATS_KV" id = "your-kv-namespace-id" preview_id = "your-preview-kv-namespace-id"
-
Deploy:
cd website/api wrangler deploy -
Update API URL in
index.html:const API_BASE_URL = 'https://copy-stats-api.your-subdomain.workers.dev';
- Create KV Namespace in Cloudflare Dashboard
- Place API file in
functions/api/copy-stats.ts - Bind KV in Pages settings → Functions → KV Namespace Bindings
- Update API URL to your Pages domain
- Create
api/copy-stats.js(convert TypeScript to JavaScript) - Use Vercel KV or Upstash Redis for storage
- Deploy to Vercel
In index.html, update the API base URL:
const API_BASE_URL = 'https://your-api-domain.com';-
Session Management:
- Each user gets a unique session ID stored in
sessionStorage - Session persists for the browser tab lifetime
- Each user gets a unique session ID stored in
-
Rate Limiting:
- One copy per session per 10 minutes
- Tracked server-side using session ID
- Prevents abuse while allowing legitimate users
-
Real-time Updates:
- Counts fetched on page load
- Polls API every 30 seconds for updates
- Updates displayed immediately when user copies
-
Platform Detection:
- Automatically detects Windows/Linux/Mac
- Tracks platform-specific counts
- Cross-platform methods (npm, bun) use detected platform
-
Test API locally (if using Wrangler):
wrangler dev
-
Test frontend:
- Open
index.htmlin browser - Open DevTools → Network tab
- Click copy buttons
- Verify API calls are made
- Open
-
Test rate limiting:
- Copy a script
- Immediately try to copy again
- Should see "Rate limited" message
- Wait 10 minutes and try again
-
Test real-time updates:
- Open site in two browsers
- Copy in one browser
- Count should update in both within 30 seconds
-
CORS: API allows all origins (
*). For production, restrict to your domain:'Access-Control-Allow-Origin': 'https://persistenceai.com'
-
Rate Limiting: Current implementation uses session-based limiting. Consider adding:
- IP-based rate limiting
- CAPTCHA for suspicious activity
- Maximum daily copies per IP
-
API Key: For production, consider adding API key authentication:
const API_KEY = request.headers.get('X-API-Key'); if (API_KEY !== env.API_KEY) { return new Response('Unauthorized', { status: 401 }); }
- Monitor KV operations
- Track API request volume
- Monitor error rates
Add logging to track:
- Copy events
- Rate limit hits
- API errors
- Check API endpoint is accessible
- Verify CORS headers
- Check browser console for errors
- Verify KV namespace is bound correctly
- Verify session ID is being generated
- Check KV namespace TTL settings
- Verify session storage is working (not in incognito mode)
- KV namespace data persists unless manually deleted
- Check KV namespace retention settings
- Verify no accidental namespace deletion