The hosting platform is your performance foundation. Getting the CDN and caching strategy right yields bigger gains than any framework-level optimization. Here are the levers I tune first.
1. Cache header strategy
Every resource type should have an explicit cache policy. The CDN and browser need to know what to cache, for how long, and whether revalidation is required:
// Fingerprinted assets — never change
Cache-Control: public, max-age=31536000, immutable
// HTML pages — short TTL + revalidate
Cache-Control: public, max-age=300, s-maxage=600, stale-while-revalidate=86400
// API responses — no caching
Cache-Control: private, no-cache, no-store
// Images — long TTL, immutable if versioned
Cache-Control: public, max-age=31536000, immutable
// Fonts — cross-origin, long cache
Cache-Control: public, max-age=31536000, immutable
Access-Control-Allow-Origin: *
The golden rule: fingerprinted assets (main.a1b2c3.js) get immutable and a 1-year TTL. HTML pages get a short TTL with stale-while-revalidate. API responses get no caching. This single rule, applied correctly, achieves 95%+ cache hit ratios.
2. Pre-compression at build time
Compress text assets (HTML, JS, CSS, JSON, SVG) during the build, not at request time. The hosting platform generates both .gz and .br versions of every compressible asset. When the CDN receives a request, it serves the pre-compressed file directly — no CPU spent on compression, no latency added.
Brotli at quality level 6 achieves 20-25% smaller files than gzip at the same compression speed. For JavaScript bundles, this is the difference between a 200 KB download and a 150 KB download — a meaningful improvement on slow 3G connections.
3. Service worker for instant repeat visits
A service worker precaches the app shell (HTML, CSS, JS) so that repeat visits load instantly, even on flaky networks. The CDN handles the first visit; the service worker handles every visit after that:
const CACHE_NAME = "app-shell-v2";
const PRECACHE_URLS = [
"/",
"/_next/static/chunks/main-abc.js",
"/_next/static/css/global.css",
"/fonts/inter-var.woff2",
];
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) =>
cache.addAll(PRECACHE_URLS),
),
);
self.skipWaiting();
});
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keys) =>
Promise.all(
keys
.filter((k) => k !== CACHE_NAME)
.map((k) => caches.delete(k)),
),
),
);
});
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
if (url.pathname.match(/\.(js|css|png|woff2)$/)) {
event.respondWith(cacheFirst(event.request));
} else {
event.respondWith(networkFirst(event.request));
}
});
async function cacheFirst(request: Request): Promise<Response> {
const cached = await caches.match(request);
return cached ?? fetchAndCache(request);
}
async function networkFirst(request: Request): Promise<Response> {
try {
return await fetchAndCache(request);
} catch {
const cached = await caches.match(request);
if (cached) return cached;
throw new Error("Offline and no cache");
}
}
async function fetchAndCache(request: Request): Promise<Response> {
const response = await fetch(request);
if (response.ok) {
const cache = await caches.open(CACHE_NAME);
cache.put(request, response.clone());
}
return response;
}
4. Image optimization pipeline
Images account for 50-70% of page weight on most sites. The hosting platform should automatically optimize images during the build: convert PNG to WebP/AVIF, resize to multiple breakpoints, strip metadata, and apply lossy compression. Next.js's next/image component does this at the component level, but the platform can also do it globally via edge workers.
Tradeoff table
| Strategy | Pros | Cons |
|---|
| SSG + CDN | Cheapest, fastest, simplest. 99%+ cache hit ratio. | No dynamic content per request. Rebuild on every change. |
| SSR + CDN + ISR | Dynamic per-request, cached for repeat visitors. | Server cost, cold starts, more complex infra. |
| Edge rendering | No cold starts, compute at user's location, global by default. | Limited runtime (V8 isolates), smaller ecosystem. |
| Pre-compression (build time) | Zero CPU at request time, best compression ratios. | Larger artifact storage, dual files (.js + .js.gz). |
| Service worker precache | Instant repeat visits, offline support. | Caching complexity, version management, storage limits. |