The edge
CDNs and edge caching
Serving static files from the point nearest the user, and knowing what must never be cached.
Understand the concept
A CDN (Content Delivery Network) consists of servers spread around the world. When a user requests a file, the nearest edge responds; if it lacks the file it fetches it from the origin and stores it for later requests. Shorter distance means less latency and less load on the origin.
The Cache-Control header decides how long things are kept. Next.js build output for JavaScript and CSS includes a content hash in the file name; when content changes, the name changes. Those files can therefore be cached for a year as ‘immutable’. HTML may vary per user and must stay fresh, so it is usually cached briefly or not at all.
The most dangerous mistake is putting a personal response into a shared cache. ‘private’ means only the browser may store it; ‘no-store’ means nobody may. Caching a page that depends on a session cookie as ‘public’ can show one user’s data to others.
Correct cache headers decide both speed and data privacy.
Sending a session-dependent response with Cache-Control: public.
One topic. Three levels of depth.
/_next/static/chunks/main-3f9a1c.js
Cache-Control: public, max-age=31536000, immutableA file whose name changes with its content can be cached for a year.
This page connects to no real server or database. Code samples are illustrative for .NET 8, Next.js and SQL Server; timings are example values in the simulation, not measurements. Try samples only in your own development environment; never share real connection strings, tokens or passwords.
Try it yourself first.
You fixed a CSS file but users keep seeing the old look. It is served as ‘/styles.css’ with max-age=31536000. What is the problem and the fix?
Show the solution and explanation
Ad değişmediği için tarayıcı ve CDN bir yıl boyunca eski dosyayı kullanır.
Çözüm: dosya adına içerik özeti eklemek (styles.3f9a1c.css)
veya bu adı kısa max-age ile sunmak.Long-lived caching is only safe for files whose name changes when their content does.