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:
- Uploaded to origin servers in Google's data centers
- Distributed to edge locations worldwide on first request
- Cached intelligently based on your headers configuration
- 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:
- Browser cache - Controlled by your Cache-Control headers
- CDN edge cache - Stores content at edge locations
- 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
Directive | Description | Use Case |
---|---|---|
max-age | Browser cache duration (seconds) | All assets |
s-maxage | CDN cache duration (seconds) | Dynamic content |
immutable | Content never changes | Versioned assets |
no-cache | Always revalidate | Critical HTML |
no-store | Never cache | Sensitive data |
stale-while-revalidate | Serve stale while updating | Frequently 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
- Cache Hit Ratio - Percentage of requests served from cache
- Origin Bandwidth - Data transferred from origin servers
- Response Time by Region - Latency for different geographic areas
- Error Rates - 4xx and 5xx responses
Using Firebase Console
Monitor CDN performance in the Firebase Console:
- Go to Hosting → Usage
- View bandwidth consumption by region
- Check cache effectiveness
- 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:
- Cache-Control headers - Fine-tune caching duration
- Vary headers - Control cache key generation
- 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 ✅
- Use immutable assets with versioned filenames
- Set appropriate cache durations based on content type
- Monitor cache hit ratios regularly
- Implement resource hints (preconnect, prefetch)
- Compress images before uploading
- Use CDN-friendly URL structures
Don'ts ❌
- Don't cache sensitive content (use no-store)
- Don't use short cache durations for static assets
- Don't ignore Vary headers for dynamic content
- Don't deploy unoptimized images
- Don't forget about mobile users in different regions
Troubleshooting CDN Issues
Common Problems and Solutions
Issue | Cause | Solution |
---|---|---|
Stale content | Long cache duration | Use versioned filenames |
Slow updates | CDN propagation | Wait 5-10 minutes or use cache busting |
High bandwidth costs | Missing cache headers | Add proper Cache-Control headers |
Regional slowness | No nearby edge | Consider Cloud CDN for specific regions |
CORS errors | Missing headers | Add 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
- Optimize cache headers - Reduce origin requests
- Compress assets - Lower bandwidth usage
- Remove unused files - Clean up old deployments
- Use appropriate image formats - WebP saves 25-35%
- 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
- Learn about Hosting Management for deployment strategies
- Explore Performance Optimization techniques
- Set up Monitoring for your CDN
- Review Limitations and quotas
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! 🚀