11. Hosting: Global CDN Deployment
Deploying Web Apps with Firebase Hosting
Firebase Hosting provides fast, secure, and global hosting for web applications. It uses a global CDN to deliver your static assets with low latency. Hosting supports custom domains, SSL certificates, and atomic deploys. It's integrated with Cloud Functions for dynamic content.
Firebase Hosting is built on Google Cloud CDN, providing enterprise-grade performance and reliability. Assets are cached globally, ensuring fast load times for users worldwide. Hosting supports versioning, allowing you to roll back to previous versions. The CLI provides easy deployment with minimal configuration. Understanding Hosting is essential for deploying production web applications. Use Hosting for static sites, single-page applications, and progressive web apps. Combine with Cloud Functions for server-side rendering and API endpoints.
Deploying Your Application
Deploying to Firebase Hosting is simple with the Firebase CLI. Initialize your project, configure the hosting settings, and run firebase deploy. The CLI automatically uploads your build directory to the CDN. You can deploy specific environments like staging and production. Firebase Hosting supports preview channels for testing changes before deployment. Preview channels provide temporary URLs for each pull request. This enables collaborative development and testing. The deployment process is atomic — your site is updated instantly when the deploy completes. Use custom domain configuration for production.
// firebase.json - Hosting configuration
{
"hosting": {
"public": "dist",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{
"source": "**/*.@(jpg|jpeg|gif|png|svg|webp)",
"headers": [
{
"key": "Cache-Control",
"value": "public, max-age=31536000, immutable"
}
]
}
]
}
}
SSL and Custom Domains
Firebase Hosting automatically provisions SSL certificates for your custom domains. You can configure custom domains through the Firebase Console. The setup process includes DNS verification and certificate provisioning. SSL certificates are automatically renewed. This ensures your site is always served over HTTPS. Custom domains are essential for production applications. Firebase Hosting supports subdomains and apex domains. You can also configure multiple domains for a single project. SSL is a requirement for modern web applications. Firebase Hosting makes it easy to secure your site.
# Deploy to Firebase Hosting firebase deploy --only hosting # Deploy to a specific target firebase target:apply hosting staging my-app-staging firebase deploy --only hosting:staging # Create a preview channel firebase hosting:channel:deploy PR_123 # List deployed versions firebase hosting:list # Rollback to a previous version firebase hosting:rollback --version=12345
Performance Optimization
Performance is critical for user experience and SEO. Firebase Hosting's CDN provides fast delivery of static assets. Use caching headers to optimize asset delivery — immutable cache for versioned assets, revalidate for dynamic content. Minify and compress assets to reduce file sizes. Use responsive images and lazy loading. Implement code splitting for JavaScript applications. Use the Hosting configuration to set custom headers. Performance optimization is essential for user engagement and conversion. Fast load times improve user experience and search rankings. Use the Chrome DevTools Lighthouse to audit and improve performance.
- Cache-Control: Set appropriate headers for different asset types.
- Minification: Minify JavaScript, CSS, and HTML.
- Image Optimization: Use responsive images and modern formats.
- Code Splitting: Split JavaScript bundles for faster initial load.
- Lazy Loading: Load non-critical resources only when needed.
Knowledge Check
Ready to test your understanding of 11. Hosting: Global CDN Deployment?