Getting 404 errors on all endpoints when deployed to Cloudflare Pages.
Cloudflare Pages Functions requires specific file patterns and routing configuration.
Created /public/_routes.json to explicitly route /api/* to Functions:
{
"version": 1,
"include": ["/api/*"],
"exclude": []
}Cloudflare Pages supports these patterns:
[...path].js- Catch-all (recommended)[[route]].js- Double bracket catch-all (may have issues)
Created /functions/api/[...path].js as the primary handler.
In Cloudflare Dashboard:
Build command: (leave empty)
Build output directory: public
Root directory: /
Your deployed structure should be:
/
├── functions/
│ └── api/
│ ├── [[route]].js
│ ├── [...path].js ← New catch-all
│ └── handler.cjs (dev only, not deployed)
├── public/
│ ├── _routes.json ← New routing config
│ ├── index.html
│ ├── large.html
│ └── assets/
├── wrangler.toml
└── dev-server.js (dev only)
git add .
git commit -m "Fix Cloudflare Pages routing for Functions"
git pushCloudflare automatically redeploys on push.
# Install wrangler (requires Node 20+)
npm install -g wrangler
# Deploy
wrangler pages deploy public --project-name=load-testing-sut- Go to Cloudflare Dashboard → Pages
- Click on your project
- Go to "Deployments"
- Click "Upload assets"
- Upload the entire project
Wait 1-2 minutes after deployment, then:
# Replace with your actual URL
export SITE_URL="https://your-project.pages.dev"
# Quick test
curl $SITE_URL/api/test
# Full validation
./validate.sh $SITE_URLAll tests should pass:
Testing: Basic API call ... ✓ PASS (Status: 200)
Testing: 500 Error simulation ... ✓ PASS (Status: 500)
Testing: 404 Error simulation ... ✓ PASS (Status: 404)
Testing: 401 Auth error ... ✓ PASS (Status: 401)
Testing: 503 DB error ... ✓ PASS (Status: 503)
Testing: Artificial delay (2s) ... ✓ PASS (Took 2s)
Testing: Large payload (100KB) ... ✓ PASS (Size: 100107 bytes)
Testing: Response headers ... ✓ PASS
Testing: Homepage (/) ... ✓ PASS
Testing: Large page (/large.html) ... ✓ PASS
Check Functions are deployed:
- Cloudflare Dashboard → Pages → Your Project
- Click "Functions" tab
- Verify you see
/api/[...path]or/api/[[route]]
Check Build Logs:
- Go to "Deployments"
- Click latest deployment
- View "Build log"
- Look for: "✨ Compiled Worker successfully"
Check _routes.json deployed:
curl https://your-site.pages.dev/_routes.jsonShould return the routing config (not 404).
Verify compatibility date:
In wrangler.toml, ensure:
compatibility_date = "2024-01-01"The /functions directory MUST be at the project root, not inside /public.
Correct structure:
/your-project/
├── functions/ ← Here, at root
└── public/
Wrong:
/your-project/
└── public/
└── functions/ ← Not here!
Check that /public contains:
index.htmllarge.htmlassets/large-image.jpg
The Cache-Control: no-store header is set in the API handler. Verify with:
curl -I https://your-site.pages.dev/api/test- Functions directory must be at root - Not in public/
- Use
_routes.jsonfor explicit routing - Prevents routing conflicts - Catch-all patterns -
[...path].jsis more reliable than[[route]].js - Build output - Must be
public(notdistor other) - Compatibility date - Must be 2021 or later for modern APIs
- Check Cloudflare Pages docs: https://developers.cloudflare.com/pages/functions/
- View deployment logs in Cloudflare Dashboard
- Test locally first:
node dev-server.jsthen./validate.sh
After applying these fixes and redeploying, your site should work correctly! ✅