Загрузка…
Загрузка…
CDN — это shared HTTP-кэш на edge PoP. Правильные Cache-Control и нормализация cache key решают и hit rate, и безопасность: статика кэшируется агрессивно, персональные ответы — никогда в shared cache.
# ✅ Static, content-hashed asset: cache hard at edge AND browser.
Cache-Control: public, max-age=31536000, s-maxage=31536000, immutable
# /static/app.7f3a9c.js
# ✅ Cacheable HTML with fast personalization window:
# browsers revalidate; the CDN holds 60s and serves stale up to a day while refreshing.
Cache-Control: public, max-age=0, s-maxage=60, stale-while-revalidate=86400
# ✅ Per-user / secret: never let a SHARED cache store it.
Cache-Control: private, no-store// Cache-key hygiene at a CDN edge worker: strip tracking params so
// /page?utm_source=x and /page are ONE cache entry (huge hit-rate win).
export default {
async fetch(request) {
const url = new URL(request.url);
for (const p of ['utm_source', 'utm_medium', 'fbclid', 'gclid']) {
url.searchParams.delete(p);
}
const key = new Request(url, request); // normalized cache key
const cache = caches.default;
let res = await cache.match(key);
if (!res) {
res = await fetch(request); // MISS → origin
if (res.ok) await cache.put(key, res.clone()); // populate edge
}
return res;
},
};s-maxage управляет TTL на CDN, max-age — в браузере. Content-hashed URL можно кэшировать «навсегда» (immutable). Tracking-параметры дробят cache key на миллионы записей — их стоит вырезать на edge. private, no-store обязателен для ответов с сессией.
CDN выигрывает от правильных директив и чистых cache keys: hashed static — long TTL, персональное — private/no-store, UTM — strip на edge.
