Skip to main content

Managing Firebase Hosting's Global CDN

Firebase Hosting automatically serves your content through a global Content Delivery Network (CDN) with over 200 edge locations worldwide. Understanding how to optimize and manage this CDN is crucial for delivering fast, reliable experiences to your users.

How Firebase CDN Works

Global Distribution

When you deploy to Firebase Hosting, your content is automatically:

  1. Uploaded to origin servers in Google's data centers
  2. Distributed to edge locations worldwide on first request
  3. Cached intelligently based on your headers configuration
  4. Served from the nearest location to each user

Automatic Features

Firebase CDN provides these features automatically:

  • Brotli compression for text files (HTML, CSS, JS)
  • HTTP/2 and HTTP/3 support for multiplexing
  • SSL/TLS termination at edge locations
  • DDoS protection built into the infrastructure
  • Anycast routing for optimal path selection

Cache Management

Understanding Cache Behavior

Firebase Hosting uses a multi-tier caching system:

  1. Browser cache - Controlled by your Cache-Control headers
  2. CDN edge cache - Stores content at edge locations
  3. Origin cache - Maintains content at Google data centers

Setting Cache Headers

Configure caching in your firebase.json:

{
"hosting": {
"headers": [
{
"source": "**/*.@(jpg|jpeg|gif|png|webp|svg)",
"headers": [{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}]
},
{
"source": "**/*.@(js|css)",
"headers": [{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}]
},
{
"source": "**/*.html",
"headers": [{
"key": "Cache-Control",
"value": "public, max-age=3600, s-maxage=7200"
}]
}
]
}
}

Cache Control Directives

DirectiveDescriptionUse Case
max-ageBrowser cache duration (seconds)All assets
s-maxageCDN cache duration (seconds)Dynamic content
immutableContent never changesVersioned assets
no-cacheAlways revalidateCritical HTML
no-storeNever cacheSensitive data
stale-while-revalidateServe stale while updatingFrequently updated content

Cache Invalidation

Firebase Hosting automatically invalidates CDN cache on new deployments for:

  • HTML files
  • Service worker files
  • Files without cache headers

For immediate invalidation of all cached content:

# Deploy with cache purge
firebase deploy --only hosting

# The CDN cache is automatically cleared for changed files

Performance Optimization

Asset Optimization Strategies

1. Version Your Assets

Use content hashing in filenames:

// webpack.config.js
output: {
filename: '[name].[contenthash].js',
chunkFilename: '[name].[contenthash].chunk.js'
}

2. Optimize Images

  • Use modern formats (WebP, AVIF)
  • Implement responsive images
  • Lazy load below-the-fold images
<picture>
<source srcset="hero.webp" type="image/webp">
<source srcset="hero.jpg" type="image/jpeg">
<img src="hero.jpg" alt="Hero" loading="lazy">
</picture>

3. Code Splitting

Split your JavaScript bundles:

// Dynamic imports for code splitting
const AdminPanel = lazy(() => import('./AdminPanel'));

CDN Performance Headers

Optimize delivery with additional headers:

{
"hosting": {
"headers": [
{
"source": "**/*",
"headers": [
{
"key": "X-Content-Type-Options",
"value": "nosniff"
},
{
"key": "Vary",
"value": "Accept-Encoding"
}
]
},
{
"source": "**/*.@(js|css|html)",
"headers": [
{
"key": "Link",
"value": "</css/critical.css>; rel=preload; as=style"
}
]
}
]
}
}

Geographic Considerations

Edge Location Coverage

Firebase CDN has edge locations in:

  • North America: 50+ locations
  • Europe: 40+ locations
  • Asia Pacific: 60+ locations
  • South America: 20+ locations
  • Middle East & Africa: 15+ locations

Region-Specific Optimization

For region-specific content, use Firebase's i18n features:

{
"hosting": {
"i18n": {
"root": "/localized"
},
"headers": [
{
"source": "/localized/**",
"headers": [{
"key": "Vary",
"value": "Accept-Language"
}]
}
]
}
}

Monitoring CDN Performance

Key Metrics to Track

  1. Cache Hit Ratio - Percentage of requests served from cache
  2. Origin Bandwidth - Data transferred from origin servers
  3. Response Time by Region - Latency for different geographic areas
  4. Error Rates - 4xx and 5xx responses

Using Firebase Console

Monitor CDN performance in the Firebase Console:

  1. Go to HostingUsage
  2. View bandwidth consumption by region
  3. Check cache effectiveness
  4. Analyze request patterns

Setting Up Alerts

Configure monitoring alerts for:

  • Bandwidth spikes
  • High error rates
  • Cache hit ratio drops
  • Regional performance issues

Advanced CDN Features

Custom CDN Behavior

While Firebase manages most CDN behavior automatically, you can influence it through:

  1. Cache-Control headers - Fine-tune caching duration
  2. Vary headers - Control cache key generation
  3. Custom headers - Add debugging information
{
"headers": [{
"source": "**/*",
"headers": [{
"key": "X-Cache-Status",
"value": "STATIC"
}]
}]
}

Edge Computing Integration

Firebase Hosting integrates with edge computing through:

  • Cloud Functions - Serverless compute
  • Cloud Run - Containerized applications
  • Service Workers - Client-side edge logic

Best Practices

Do's ✅

  1. Use immutable assets with versioned filenames
  2. Set appropriate cache durations based on content type
  3. Monitor cache hit ratios regularly
  4. Implement resource hints (preconnect, prefetch)
  5. Compress images before uploading
  6. Use CDN-friendly URL structures

Don'ts ❌

  1. Don't cache sensitive content (use no-store)
  2. Don't use short cache durations for static assets
  3. Don't ignore Vary headers for dynamic content
  4. Don't deploy unoptimized images
  5. Don't forget about mobile users in different regions

Troubleshooting CDN Issues

Common Problems and Solutions

IssueCauseSolution
Stale contentLong cache durationUse versioned filenames
Slow updatesCDN propagationWait 5-10 minutes or use cache busting
High bandwidth costsMissing cache headersAdd proper Cache-Control headers
Regional slownessNo nearby edgeConsider Cloud CDN for specific regions
CORS errorsMissing headersAdd Access-Control headers

Debug Headers

Add debug headers to track CDN behavior:

{
"headers": [{
"source": "**/*",
"headers": [
{
"key": "X-Debug-Cache",
"value": "enabled"
},
{
"key": "X-Deployment-Id",
"value": "__BUILD_ID__"
}
]
}]
}

Cost Optimization

Reducing CDN Costs

  1. Optimize cache headers - Reduce origin requests
  2. Compress assets - Lower bandwidth usage
  3. Remove unused files - Clean up old deployments
  4. Use appropriate image formats - WebP saves 25-35%
  5. Implement lazy loading - Load only what's needed

Bandwidth Calculation

Estimate your CDN costs:

Monthly Cost = (Total Bandwidth GB - 10GB free tier) × $0.15

Example for a site with 100GB monthly traffic:

(100GB - 10GB) × $0.15 = $13.50/month

Next Steps


Pro Tip: The Firebase CDN is designed to "just work" for most use cases. Focus on setting proper cache headers and optimizing your assets - Firebase handles the complex CDN management automatically! 🚀