Denis Kirevby Denis Kirev

Understanding Content Delivery Networks and Asset Hashing

Deep dive into CDN architecture, asset hashing strategies, and performance optimization techniques.

4 min read
NETWORKPERFORMANCEGUIDE
Image

Hash vs. CDN vs. Different Types of Storage in Frontend Development

Introduction

When building modern web applications, performance and efficiency are key concerns. The way we store and serve assets impacts speed, caching, security, and scalability. This article explores three core approaches in frontend development:

  • Hash-based storage – Ensures cache-busting and content integrity.
  • CDN (Content Delivery Network) – Optimizes asset delivery worldwide.
  • Different types of storage – Local Storage, Session Storage, IndexedDB, and Cache API.
  • Hashing API Requests – Optimizing API response caching and validation.

Understanding these methods helps in making informed architectural decisions for frontend performance and scalability.


1. Hash-based Storage: Managing Assets with Cache-Busting

What It Does

Hashing is commonly used in frontend build systems to ensure that assets are uniquely identified by their content. Whenever a file changes, its hash changes too, preventing outdated caching.

Example: Webpack & Vite Hashing

Modern bundlers like Webpack and Vite generate hashed filenames:

main.8f3c29.js  →  main.2a4b67.js (after update)

This prevents browsers from serving outdated files from cache.

Pros & Cons

Cache Busting – Users always get the latest files. ✅ Improves Security – Content integrity is maintained. ⚠️ More Requests – Users must download new files after updates.

Use Cases

  • Versioned assets in SPAs and static sites.
  • Content fingerprinting to avoid caching issues.

2. CDN (Content Delivery Network): Accelerating Asset Delivery

What It Does

A CDN distributes assets across multiple global servers. Users receive files from the closest server, reducing latency and improving load times.

Example: Using a CDN for Static Assets

<script src="https://cdn.example.com/app.js"></script>

Pros & Cons

Faster Load Times – Assets are served from edge locations. ✅ Reduces Server Load – Offloads traffic from the origin server. ⚠️ Potential Costs – Premium CDNs can be expensive. ⚠️ Cache Invalidation – Requires proper cache control strategies.

Use Cases

  • Hosting static assets (JS, CSS, images, fonts).
  • Delivering media content (videos, images, JSON APIs).

3. Storage Solutions in Frontend: Where to Store Data?

Local Storage vs. Session Storage vs. IndexedDB vs. Cache API

| Storage Type | Capacity | Persistence | Use Case | | ------------------- | ------------ | -------------------------- | -------------------------------------------------- | | Local Storage | ~5-10MB | Persistent (until cleared) | Saving user preferences (e.g., dark mode) | | Session Storage | ~5MB | Clears on tab close | Temporary session-based data (e.g., form progress) | | IndexedDB | ~50MB+ | Persistent | Storing structured data, offline capabilities | | Cache API | Varies | Persistent | Caching API responses, PWA optimization |

Example: Using Cache API for Offline Storage

async function cacheResponse() {
  const cache = await caches.open('my-cache')
  await cache.add('/api/data')
}

Pros & Cons

Improves Performance – Reduces redundant network requests. ✅ Enables Offline Functionality – PWAs can work without the internet. ⚠️ Storage Limits – Browsers enforce different quotas. ⚠️ Security Risks – Stored data can be accessed by scripts.

Use Cases

  • Local Storage: Theme preferences, authentication tokens.
  • IndexedDB: Offline databases for complex web apps.
  • Cache API: Service worker caching for PWAs.

4. Hashing API Requests: Optimizing Caching and Performance

What It Does

Hashing API requests helps ensure that responses are only re-fetched when data changes, reducing redundant network requests and improving performance.

Example: Hashing API Response Data for Efficient Caching

async function fetchWithHash(url) {
  const response = await fetch(url)
  const data = await response.json()
  const hash = btoa(JSON.stringify(data)) // Generate a base64 hash

  const cachedHash = localStorage.getItem(`${url}-hash`)
  if (cachedHash === hash) {
    console.log('Using cached data')
    return JSON.parse(localStorage.getItem(`${url}-data`))
  }

  localStorage.setItem(`${url}-hash`, hash)
  localStorage.setItem(`${url}-data`, JSON.stringify(data))
  return data
}

Pros & Cons

Reduces Network Load – Fetches only when data changes. ✅ Enhances Performance – Improves perceived speed for users. ⚠️ Storage Management Required – Cached data must be periodically cleaned.

Use Cases

  • Caching frequently accessed API responses.
  • Reducing redundant network requests for static or rarely changing data.

Conclusion: Choosing the Right Approach

Each storage and asset delivery method has its unique advantages. The best choice depends on the use case, performance needs, and caching strategy.

When to Use What?

| Scenario | Recommended Approach | | --------------------------- | -------------------------- | | Preventing outdated assets | Hash-based file naming | | Improving global load speed | CDN for assets | | Storing user settings | Local Storage | | Handling large offline data | IndexedDB | | Caching API responses | Cache API with hashing |

Understanding these methods ensures your frontend applications are fast, scalable, and user-friendly. 🚀

Last updated: February 6, 2025