
Cloud
Learning Level
Firebase Hosting is a fast, secure, and scalable way to deploy static content, single-page applications (SPAs), and progressive web apps (PWAs). It provides automatic HTTPS, global CDN, and one-click deployments directly from your source code repository.
By the end of this lesson, you'll understand:
Firebase Hosting is a fully managed static hosting service that:
# Install Firebase CLI globally
npm install -g firebase-tools
# Verify installation
firebase --version
# Login to Firebase
firebase login# Create a new directory
mkdir my-firebase-site
cd my-firebase-site
# Initialize Firebase in the directory
firebase init hosting
# Select or create a Firebase project
# Choose what to deploy (public directory)public/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Firebase Site</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Welcome to Firebase Hosting!</h1>
<p>This site is hosted globally with automatic HTTPS.</p>
</div>
<script src="app.js"></script>
</body>
</html># Preview locally
firebase emulators:start --only hosting
# Deploy to Firebase Hosting
firebase deploy{
"hosting": {
"public": "public",
"ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
"rewrites": [{"source": "**", "destination": "/index.html"}],
"redirects": [{"source": "/old-page", "destination": "/new-page", "type": 301}],
"headers": [
{"source": "**/*.@(js|css)", "headers": [{"key": "Cache-Control", "value": "public, max-age=31536000"}]},
{"source": "/index.html", "headers": [{"key": "Cache-Control", "value": "public, max-age=3600"}]}
]
}
}firebase.json for SPA:
{
"hosting": {
"public": "build",
"rewrites": [{"source": "**", "destination": "/index.html"}]
}
}Build and deploy React app:
npm install
npm run build
firebase deploy# List current domains
firebase hosting:sites:list
# Add custom domain
# 1. Go to Firebase Console → Hosting
# 2. Click "Connect domain"
# 3. Enter your domain
# 4. Verify domain ownership
# 5. Update DNS recordsfunctions/index.js:
const functions = require('firebase-functions');
const express = require('express');
const app = express();
app.get('/api/users', (req, res) => {
res.json([
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]);
});
exports.api = functions.https.onRequest(app);firebase.json with rewrites:
{
"hosting": {
"public": "public",
"rewrites": [
{"source": "/api/**", "function": "api"},
{"source": "**", "destination": "/index.html"}
]
}
}# List all versions
firebase hosting:versions:list
# Promote a previous version
firebase hosting:versions:promote VERSION_ID
# View deployment history
firebase hosting:sites:listnpm run build
du -sh build/.firebaserc:
{
"projects": {
"default": "my-project-prod",
"staging": "my-project-staging"
}
}firebase deploy --project=staging
firebase deploy --project=devfirebase hosting:channel:deploy preview-branch
firebase hosting:channel:promote preview-branch.github/workflows/deploy.yml:
name: Deploy to Firebase
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Deploy to Firebase
uses: FirebaseExtended/action-hosting-deploy@v0
with:
repoToken: ${{ secrets.GITHUB_TOKEN }}
firebaseServiceAccount: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }}
projectId: your-project-idExplore Cloud Functions for backend integration, or learn about Firestore for real-time data storage.
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward