Cloudflare Worker Edge Acceleration Engine
Architected custom TypeScript Cloudflare Workers in front of Laravel origin servers to offload over 70% of incoming traffic, enforce intelligent edge caching via Edge KV and Cache API, scrub abusive bots, and deliver sub-30ms global Time-To-First-Byte.
The Production Challenge
Origin Laravel servers and databases were burning expensive CPU cycles repeatedly rendering identical public pages, API lookups, and defending against automated scraper floods from distributed regions.
The Engineered Solution
Built TypeScript Cloudflare Worker scripts that inspect request paths, cookies, and authorization headers at the edge—serving cached HTML/JSON from Cloudflare PoPs and Edge KV in under 30ms while forwarding only authenticated or mutating requests to Laravel.
Most Laravel performance guides stop at OPcache and Redis—yet the fastest HTTP request is the one that never reaches your origin server at all. By combining cookie-free public Laravel middleware (`Cache-Control: public, s-maxage=86400, stale-while-revalidate=600`) with custom TypeScript Cloudflare Workers, anonymous traffic is absorbed globally at 300+ edge locations.
Crucially, edge caching must never leak personalized or authenticated state. The Worker enforces strict bypass rules whenever session cookies, authorization headers, or Livewire update endpoints (`/livewire/*`) are detected, while cache-purge hooks invalidate Edge KV keys immediately whenever content updates.
Origin Bottlenecks & Uncached Bot Traffic
- Every anonymous GET request booted PHP-FPM, Laravel, and MySQL even for semi-static pages.
- Laravel Set-Cookie session headers accidentally forced CDN caches to bypass HTML caching.
- Global visitors outside the origin region experienced 250–600ms+ TTFB.
- Automated vulnerability scanners and scrapers consumed origin worker pools.
TypeScript Edge Worker & Cookie-Free Origin Responses
- Middleware strips session cookies and emits s-maxage + stale-while-revalidate on public routes.
- TypeScript Workers normalize cache keys, scrub bad bots, and serve edge hits in <30ms.
- Authenticated sessions or POST mutations transparently bypass edge cache directly to origin.
- Origin compute load dropped by 70%+, extending server headroom dramatically.
Edge Topology — Anycast Caching & Origin Shielding
TypeScript Workers filter bots and serve cached public responses globally while passing authenticated and Livewire traffic straight to Laravel.
flowchart LR
Visitor["Global Visitor / Bot"] --> Worker["Cloudflare TypeScript Worker"]
Worker -->|"Bad Bot / Rate Limit"| Block["403 / 429 Edge Reject"]
Worker -->|"Authenticated / POST / Livewire"| Origin["Laravel Origin Server"]
Worker -->|"Public GET"| Cache{"Edge Cache / KV Hit?"}
Cache -->|"Hit (<30ms)"| Visitor
Cache -->|"Miss"| Origin
Origin -->|"Cookie-Free s-maxage=86400"| Cache
Cache Decision Sequence — Safe Public HTML + Livewire
How public pages achieve edge caching without breaking interactive Livewire POST roundtrips.
sequenceDiagram
participant B as Browser
participant W as Cloudflare Worker
participant L as Laravel Origin
B->>W: GET / (Anonymous)
W->>W: Strip tracking query params & check Cache
alt Edge Hit
W-->>B: HTTP 200 (cf-cache-status: HIT, <30ms)
else Edge Miss
W->>L: Forward GET /
L-->>W: HTML + Cache-Control: public, s-maxage=86400 (No Set-Cookie)
W->>W: Store in Edge Cache
W-->>B: HTTP 200
end
B->>W: POST /livewire/update (Interactive action)
W->>L: Bypass cache -> Origin Laravel
L-->>B: Livewire JSON diff
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const url = new URL(request.url);
// Never cache mutations, Livewire XHR updates, or authenticated requests
const hasAuthCookie = (request.headers.get('Cookie') || '').includes('boswell_session=');
if (request.method !== 'GET' || url.pathname.startsWith('/livewire') || hasAuthCookie) {
return fetch(request);
}
// Normalize cache key by stripping utm_* marketing parameters
const cacheUrl = new URL(url.toString());
for (const key of [...cacheUrl.searchParams.keys()]) {
if (key.startsWith('utm_') || key === 'fbclid' || key === 'gclid') {
cacheUrl.searchParams.delete(key);
}
}
const cacheKey = new Request(cacheUrl.toString(), { method: 'GET' });
const cache = caches.default;
const cached = await cache.match(cacheKey);
if (cached) {
return cached;
}
const response = await fetch(request, { cf: { cacheEverything: true } });
if (response.ok && !response.headers.has('Set-Cookie')) {
ctx.waitUntil(cache.put(cacheKey, response.clone()));
}
return response;
},
};
Architectural Highlights
- TypeScript Cloudflare Worker scripts offloading over 70% of static and cacheable dynamic requests from Laravel origins.
- Sub-30ms global Time-To-First-Byte across European and international traffic via Cloudflare Anycast PoPs.
- Query-parameter normalization preventing marketing UTM tags from fragmenting edge cache hit ratios.
- Cookie-free Laravel middleware emitting s-maxage=86400 and stale-while-revalidate=600 on public routes.
- Edge-level bot scrubbing and rate limiting blocking malicious scrapers before PHP-FPM workers are touched.
Cookie-Free Public Middleware Paired with Edge Workers
Default Laravel web middleware attaches session cookies that instruct CDNs not to cache HTML. Stripping cookies on public landing routes unlocked full-page edge caching while preserving Livewire POST interactivity.
Query-Param Normalization at the Edge
Ad clicks and social shares append `utm_*` and `fbclid` parameters. Stripping non-functional parameters from the edge cache key keeps cache hit ratios near 95%+.
Designing or rescuing a system like this?
I work directly with engineering teams and startup founders on high-concurrency Laravel, MySQL performance engineering, payment infrastructure, and edge caching.