Static Content Hosting
Serve files that are the same for everyone, such as scripts, styles, images and prerendered pages, from object storage behind a CDN instead of from app servers.
Description
Much of what a website sends never changes per user: JavaScript bundles, CSS, fonts, images, even whole prerendered pages. Serving them from application servers wastes compute and adds latency for distant users.
Instead, put them in object storage and serve them through a CDN, which caches copies in data centres close to users worldwide. Dynamic requests still go to your APIs. Use content-hashed file names (app.3f9a1c.js) so files can be cached for a year and a deploy simply publishes new names.
System design
Static Content Hosting system design diagram
- User's browser (client).
- CDN edge (CDN): close to the user.
- Object storage (object storage): HTML, JS, CSS, images.
- API (service): dynamic data only.
- Step 1: User's browser → CDN edge: GET /app.3f9a1c.js.
- Step 2: CDN edge → Object storage: only on a cache miss.
- Step 3: CDN edge → User's browser: cached copy, nearby.
- Step 4: User's browser → API: fetch /api/orders.
- The browser requests a script with a content hash in its name.
- If this edge location doesn't have it yet, the CDN fetches it once from private object storage.
- Every later request in that region is served from the nearby edge cache, in milliseconds.
- Only dynamic, per-user data goes to the API.
- AWS
- S3 + CloudFront (Origin Access Control); AWS Amplify Hosting.
- Azure
- Blob Storage static websites + Azure Front Door; Static Web Apps.
- Google Cloud
- Cloud Storage + Cloud CDN behind a load balancer; Firebase Hosting.
When to use it
Use it when
- Websites and single-page apps with assets that are the same for every user.
- Prerendered or static sites: documentation, marketing pages, blogs.
- Users are spread across regions and page speed matters.
Avoid it when
- Every response is personalised or must be generated per request.
- Content changes every few seconds and must be instantly current everywhere. CDNs need invalidation or short TTLs.
Pitfalls
- Stale files after a deploy because they're cached without versioned names. Hash file names; keep HTML short-cached.
- A public bucket instead of private storage with CDN access. Lock the bucket down and let only the CDN read it.
- Forgetting security headers (CSP, HSTS) that the app server used to add. Configure them on the CDN.
- Client-side routes returning 404 for deep links. Configure a fallback, or prerender every route.
Alternatives
App server with a CDN in front
Choose it when pages are rendered per request but are cacheable for a short while.
Valet Key
Choose it when files are private per user; give each user a time-limited signed URL.
Cache-Aside
Choose it when you're caching data inside the application, not files at the edge.