🛡️ Intelligent TikTok bot detection with server-side content rendering (no redirects)
A sophisticated Vercel Edge middleware that uses reverse proxy to serve different content to TikTok bots (Bytespider, TikTokSpider) and legitimate human users—including those browsing via TikTok's in-app browser (WebView)—while keeping the same URL visible in the browser.
🇲🇦 دليل النشر بالدارجة المغربية متوفر في DEPLOYMENT_AR.md Arabic/Moroccan Darija deployment guide available in DEPLOYMENT_AR.md
- ⚡ Ultra-Fast Detection: Powered by Vercel Edge Functions (<50ms latency)
- 🎯 Precise TikTok Bot Detection: Targets only Bytespider and TikTokSpider crawlers
- 👥 Human-Friendly: Preserves access for TikTok WebView users (trill, musical_ly, BytedanceWebview)
- 🔄 Server-Side Rendering (SSR): Reverse proxy serves different content without URL changes
- 🎭 Stealth Mode: Same URL shows different content to bots vs humans
- 🌍 Global Distribution: Runs at 100+ edge locations worldwide
- 📱 Mobile-First: Optimized for TikTok in-app browser and mobile users
- 🚀 Serverless: Zero infrastructure management, auto-scaling
- 🧪 Comprehensive Testing: Includes test suite to validate bot detection
- 📊 Debug Headers: X-Bot-Detection, X-Proxy-Target, X-Render-Mode headers
┌─────────────────┐
│ Incoming Request│
│ (same URL for │
│ bots & humans) │
└────────┬────────┘
│
▼
[User-Agent Check]
(Edge Middleware)
│
┌────┴────┐
│ │
▼ ▼
TikTok TikTok Bot?
WebView? (Bytespider/
(Human) TikTokSpider)
│ │
│ YES │ YES
▼ ▼
┌─────────┐ ┌─────────┐
│ Fetch │ │ Fetch │
│ REAL │ │ FAKE │
│ Content │ │ Content │
└────┬────┘ └────┬────┘
│ │
└─────┬─────┘
▼
┌──────────────┐
│ Serve Content│
│ (SSR - same │
│ URL in │
│ browser) │
└──────────────┘
Key Logic:
- ✅ TikTok WebView (trill, musical_ly, BytedanceWebview) → HUMAN → Serve Real Content
- ✅ Bytespider or TikTokSpider → BOT → Serve Fake Content
- ✅ All other traffic → HUMAN → Serve Real Content
- ✅ URL never changes - server-side rendering only
- Framework: Next.js 14 (TypeScript)
- Runtime: Vercel Edge Runtime
- Configuration: Static environment variables (single-user setup)
- Styling: Tailwind CSS
- Deployment: Vercel Platform
- Node.js 18+ installed
- A Vercel account (free tier works)
- Git installed
git clone <your-repo-url>
cd vercel-bot-proxynpm installCreate a .env.local file in the project root:
# Static Proxy Configuration
REAL_URL=https://your-real-website.com
BOT_URL=https://your-decoy-website.comReplace the URLs with your actual website URLs.
npm run devOpen http://localhost:3000 in your browser.
- Homepage:
http://localhost:3000 - Admin Dashboard:
http://localhost:3000/admin
npm install -g vercelvercel login# Deploy to preview
vercel
# Deploy to production
vercel --prodSet your environment variables in Vercel:
- Go to your Vercel Dashboard: https://vercel.com/dashboard
- Select your project
- Go to Settings → Environment Variables
- Add the following variables:
REAL_URL: Your real website URL (e.g.,https://your-main-site.com)BOT_URL: Your decoy website URL (e.g.,https://decoy-site.com)
- Make sure to set them for Production, Preview, and Development environments
After adding environment variables, trigger a redeployment:
vercel --prodgit init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin <your-github-repo-url>
git push -u origin main- Go to Vercel Dashboard
- Click Import Project
- Select your GitHub repository
- Vercel will auto-detect Next.js and configure build settings
- Click Deploy
Follow the same environment variable setup steps as Method 1 (Step 4).
The following environment variables are required for the static configuration:
REAL_URL=https://your-real-website.com
BOT_URL=https://your-decoy-website.comNote: These must be set in your Vercel project settings or .env.local file for local development.
-
Navigate to the Admin Dashboard:
https://your-app.vercel.app/admin -
View your static configuration:
- Real Website URL: Where legitimate users will go
- Bot Redirect URL: Where bots will be redirected
-
Copy your deployment URL (e.g.,
https://your-app.vercel.app) -
Important: Share your deployment URL instead of your real website URL
-
The middleware will automatically redirect:
- Legitimate users → Real Website URL
- Bots → Bot Redirect URL
The middleware uses a precise, priority-based detection system:
CRITICAL: Users browsing via TikTok's in-app browser are real humans.
TikTok WebView Identifiers:
trill- TikTok WebView identifiermusical_ly- TikTok WebView identifierBytedanceWebview- TikTok WebView identifierJsSdk/1.0orJsSdk/2.0- TikTok WebView SDK
These users are NEVER redirected to fake content.
User-Agent patterns treated as bots:
Bytespider- AI data scraper for LLM trainingTikTokSpider- Link preview fetcher
These bots are redirected to the fake URL with HTTP 301.
Any traffic that doesn't match the above patterns is treated as legitimate and redirected to the real URL.
if (TikTok WebView detected) {
→ HUMAN → Fetch and serve Real Content (SSR)
} else if (Bytespider or TikTokSpider detected) {
→ BOT → Fetch and serve Fake Content (SSR)
} else {
→ HUMAN → Fetch and serve Real Content (SSR)
}Note: This implementation focuses specifically on TikTok bot detection. Generic bot detection and ChatGPT/AI bot detection have been removed to prevent false positives.
SSR Behavior: The middleware acts as a reverse proxy, fetching content from the target URL and serving it directly. The URL in the browser never changes - bots and humans see the same URL but receive different content.
The proxy preserves:
- ✅ Query parameters (e.g.,
?utm_source=tiktok&id=123) - ✅ Path segments (e.g.,
/products/item-5) - ✅ All original request context
Example:
Deployment URL: https://your-app.vercel.app/products?id=5
↓
Real URL: https://your-site.com/products?id=5
- TIKTOK_BOT_DETECTION.md - Comprehensive guide to TikTok bot detection logic, testing, and troubleshooting
- ARCHITECTURE.md - Detailed system architecture and data flow diagrams
- test-bot-detection.sh - Automated test suite for validating bot detection
Run the comprehensive test suite:
# Test local development
./test-bot-detection.sh http://localhost:3000
# Test production
./test-bot-detection.sh https://your-app.vercel.appTest TikTok Bot (should serve FAKE content via SSR):
curl -I -A "Mozilla/5.0 (compatible; Bytespider; spider-feedback@bytedance.com)" https://your-app.vercel.app
# Expected: HTTP 200, X-Bot-Detection: bot, X-Proxy-Target: fake-urlTest TikTok WebView (should serve REAL content via SSR):
curl -I -A "Mozilla/5.0 (Linux; Android 8.1.0) AppleWebKit/537.36 trill_200005 JsSdk/1.0" https://your-app.vercel.app
# Expected: HTTP 200, X-Bot-Detection: human, X-Proxy-Target: real-urlTest Regular Browser (should serve REAL content via SSR):
curl -I -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/120.0.0.0" https://your-app.vercel.app
# Expected: HTTP 200, X-Bot-Detection: human, X-Proxy-Target: real-urlView actual content (not just headers):
# Bot sees fake content
curl -A "Mozilla/5.0 (compatible; Bytespider)" https://your-app.vercel.app | head -20
# Human sees real content
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0" https://your-app.vercel.app | head -20See TIKTOK_BOT_DETECTION.md for comprehensive testing documentation.
This project uses static configuration via environment variables for a simple, single-user setup. No database is required - all configuration is stored in environment variables.
vercel-bot-proxy/
├── middleware.ts # Edge middleware (bot detection & routing)
├── lib/
│ ├── botDetection.ts # Bot detection utilities
│ └── supabase.ts # (Legacy - not used in static config)
├── pages/
│ ├── _app.tsx # Next.js app wrapper
│ ├── index.tsx # Homepage
│ ├── admin.tsx # Admin dashboard (view static config)
│ └── api/
│ ├── config.ts # (Legacy - not used in static config)
│ └── config/
│ └── [id].ts # (Legacy - not used in static config)
├── styles/
│ └── globals.css # Global styles
├── package.json
├── tsconfig.json
├── next.config.js
├── tailwind.config.js
└── vercel.json # Vercel deployment config
- Edge Middleware Latency: <50ms
- Global Distribution: 100+ edge locations
- Scalability: Handles millions of requests
- Cold Start: ~100ms (Edge Functions are always warm)
- Static Configuration: Environment-based, no dynamic configuration
- No Database: No data storage or user input processing
- No Data Logging: Zero data storage beyond environment variables
- HTTPS Only: All traffic encrypted
Edit middleware.ts to customize bot detection logic:
// Add custom bot patterns
const customBotPatterns = [
'my-custom-bot',
'another-crawler',
];To protect the admin dashboard, you can:
-
Use Vercel Password Protection (easiest):
- Go to Project Settings → Deployment Protection
- Enable Password Protection
-
Implement Custom Auth (advanced):
- Add authentication middleware
- Use NextAuth.js or similar
Symptom: When you visit your deployed app, you see an error page saying "Proxy service not configured".
Cause: Environment variables REAL_URL and BOT_URL are not set in Vercel.
Solution:
- Go to Vercel Dashboard
- Select your project
- Navigate to Settings → Environment Variables
- Add both variables:
REAL_URL= Your real website URLBOT_URL= Your decoy website URL
- Enable for Production, Preview, and Development
- Redeploy your application
Detailed instructions:
- English: See section "Deployment to Vercel" above
- Arabic/Darija: See DEPLOYMENT_AR.md for full guide in Moroccan Arabic
Solution: Make sure you've set the environment variables in Vercel and redeployed.
# For local development, create .env.local with:
REAL_URL=https://your-real-website.com
BOT_URL=https://your-decoy-website.com
# For production, set in Vercel Dashboard and redeploy:
vercel --prodSolution: Ensure middleware.ts is in the root directory and check the middleware configuration.
Solution:
- Check bot User-Agent in your server logs
- Add pattern to
middleware.ts(detectTikTokBot function) - Test locally with custom User-Agent:
curl -H "User-Agent: TikTok Bot" https://your-app.vercel.app/- Analytics dashboard (bot traffic metrics)
- Multiple bot type support (Instagram, Facebook, etc.)
- Machine learning-based detection
- Rate limiting
- Admin authentication
- Webhook notifications
- Custom redirect rules (A/B testing)
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - feel free to use this project for any purpose.
For issues and questions:
- Create an issue in this repository
- Check the ARCHITECTURE.md for technical details
Built with ❤️ using Vercel Edge Functions