CDN Optimization

Advanced CDN optimization techniques.

Cloudflare Setup

1. Add site to Cloudflare
Sign up, add your domain

2. Update nameservers
Point to Cloudflare nameservers

3. Enable proxy (orange cloud)
DNS records proxied through CF

4. Configure caching
Page Rules for custom cache TTL

Page Rules (Cloudflare)

Cache everything
URL: example.com/*
Setting: Cache Level = Cache Everything

Static assets long cache
URL: example.com/assets/*
Setting: Browser Cache TTL = 1 year
Setting: Edge Cache TTL = 1 month

Bypass cache for API
URL: example.com/api/*
Setting: Cache Level = Bypass

Cache Purging

Purge entire cache
Cloudflare Dashboard → Caching → Purge Everything

Purge specific URLs
Purge by URL or prefix

Purge via API (Cloudflare)
curl -X POST https://api.cloudflare.com/client/v4/zones/ZONE_ID/purge_cache \
  -H Authorization: Bearer TOKEN \
  -d {"purge_everything":true}

Preloading & Prefetching

DNS prefetch
 rel="dns-prefetch" href="//cdn.example.com">

Preconnect
 rel="preconnect" href="https://cdn.example.com">

Preload critical assets
 rel="preload" href="/app.css" as="style">
 rel="preload" href="/app.js" as="script">