Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 31 ยท Storage and Functions

Hosting, Deploying and Environments

Deploy a frontend with Firebase Hosting or next to Supabase, keep dev, staging and production projects apart, automate CI deploys and control costs.

Your app is more than a database. Someone has to serve the HTML, CSS and JavaScript, and you need a safe way to ship changes without breaking real users. A BaaS gives you the back end, but you still decide where the front end lives, how many environments you run and how a change travels from your laptop to production.

In this lesson you will meet Firebase Hosting, see how a Supabase project fits with any static host, learn the preview, staging and production pattern, and think about rollbacks, domains and cost. The commands and config files below are read-only because they need real accounts. The TryIts are our own stand-ins: a planner that turns environment config into a deploy plan, and a tiny release history that shows how a rollback works.

Ship in three steps

Preview A throwaway copy for each pull request.
Staging A full copy with its own backend project.
Production Real users, real data, careful changes.

Each step gets its own backend project and its own keys.

1 / 2

Firebase Hosting

Firebase Hosting serves static files (and single-page apps) from a global CDN over HTTPS. You build your app into a folder, such as dist, and upload it. Each upload creates an immutable version, and "going live" means pointing the site at a version.

Terminal
# Firebase (read-only)
npm install -g firebase-tools
firebase login
firebase init hosting          # asks for the public folder and SPA rewrite
npm run build
firebase deploy --only hosting

The settings live in firebase.json:

JSON
{
  "hosting": {
    "public": "dist",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "cleanUrls": true,
    "rewrites": [{ "source": "**", "destination": "/index.html" }],
    "headers": [
      {
        "source": "**/*.@(js|css|woff2)",
        "headers": [{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }]
      }
    ]
  }
}

The rewrites rule is what makes a single-page app work: any path that is not a real file returns index.html, and your router takes over. The headers rule caches fingerprinted assets (such as app.4f3a9c.js) for a year, while index.html stays uncached so users see new releases quickly.

Firebase can also rewrite a path to a Cloud Function, so /api/** can run server code on the same domain. See Cloud Functions and Edge Functions.

Supabase has no front-end hosting

Supabase hosts your back end: database, auth, storage, realtime and functions. It does not host your website. Your front end goes to any static or app host: Firebase Hosting, Netlify, Vercel, Cloudflare Pages, or your own server. The link between the two is just configuration, the project URL and the public key:

Terminal
# .env.production (read-only, safe to expose: these are public values)
VITE_SUPABASE_URL=https://abcdefghijklmnop.supabase.co
VITE_SUPABASE_ANON_KEY=eyJhbGciOi...

Anything prefixed for the browser ends up in your JavaScript bundle, so only public values belong there. Secrets such as the service role key stay on the server or in function secrets. Read Creating Projects and Using the SDKs again if that rule is fuzzy.

The Supabase CLI deploys the parts that live on their side:

Terminal
# Supabase (read-only)
supabase login
supabase link --project-ref abcdefghijklmnop
supabase db push                       # apply new migrations
supabase functions deploy send-welcome # deploy one edge function
supabase secrets set STRIPE_KEY=sk_live_...

Schema changes should travel as migration files in git, not as clicks in the dashboard. Then every environment can reach the same schema by running the same files in order. Supabase also offers preview branches that create a temporary database for a pull request; check the current docs for plan and setup details.

One project per environment

The most important rule is simple: never point development at production. Create separate projects:

Environment Firebase project Supabase project Data
dev shop-dev shop-dev fake, can be wiped
staging shop-staging shop-staging fake or anonymized copy
production shop-prod shop-prod real

Firebase maps names to project IDs with aliases in .firebaserc:

JSON
{
  "projects": {
    "default": "shop-dev",
    "staging": "shop-staging",
    "production": "shop-prod"
  }
}
Terminal
# Firebase (read-only)
firebase use staging
firebase deploy --only hosting,firestore:rules
firebase deploy --only hosting --project production

Every deploy command should name its target explicitly in scripts. A deploy that relies on "whatever firebase use was last" is how test code lands in production.

Preview deployments

Firebase Hosting has preview channels: temporary URLs that run alongside the live site.

Terminal
# Firebase (read-only)
firebase hosting:channel:deploy pr-42 --expires 7d
# prints a URL like https://shop-staging--pr-42-abc123.web.app

A preview of the front end still talks to a backend. Point previews at the staging project, not production, so reviewers can click around without touching real data. The URL expires on its own, which keeps costs and clutter down.

CI deploys

Deploy from continuous integration, not from laptops. The pipeline builds once, runs tests and then deploys with credentials stored as CI secrets:

YAML
# .github/workflows/deploy.yml (read-only)
on:
  push:
    branches: [main]
jobs:
  deploy:
    runs-on: ubuntu-latest
    environment: staging
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm test && npm run build
      - run: npx firebase-tools deploy --only hosting --project staging
        env:
          GOOGLE_APPLICATION_CREDENTIALS: ${{ runner.temp }}/key.json
      - run: npx supabase db push
        env:
          SUPABASE_ACCESS_TOKEN: ${{ secrets.SUPABASE_ACCESS_TOKEN }}
          SUPABASE_DB_PASSWORD: ${{ secrets.SUPABASE_DB_PASSWORD }}

This sketch leaves out writing the key file and linking the project. The idea matters more than the exact lines: migrations first, then code, production only after approval, and secrets never in the repository.

Planning a deploy from config

Real pipelines are driven by data: which branch goes where, which project to use, what needs a human to approve. The stand-in below turns a small environment table into an ordered plan. It prints commands only; it deploys nothing. It runs on our Node server.

Node.js Stand-in deploy planner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice that the migration step comes before the build and the hosting deploy, and that production waits for approval. A feature branch can never reach production because the branch rule skips it.

Rollbacks

Because every Hosting deploy is an immutable version, a rollback is cheap: point the live site at an older version. In the Firebase console you can roll back with one click, and the CLI can copy a channel's version to live:

Terminal
# Firebase (read-only)
firebase hosting:clone shop-staging:pr-42 shop-prod:live

The database is different. You cannot "roll back" user data by redeploying code. Write migrations that are backwards compatible (add a column first, start using it, remove the old column in a later release), so the previous front end still works against the newer schema. Both platforms offer backups on paid plans; check their docs for retention and restore details.

The stand-in below models a release history: deploying adds a version, rollback moves a pointer, and nothing is deleted.

JS Stand-in release history with rollback

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The old versions were kept, which is exactly why a rollback is instant. Real Hosting keeps a history of versions, though older ones can be pruned by your retention settings.

Custom domains

Both Firebase Hosting and most Supabase-friendly hosts support custom domains. The flow is the same: add the domain in the dashboard, create the DNS records they show you (usually an A/AAAA or CNAME record plus a verification TXT record), and wait for DNS to propagate. HTTPS certificates are issued automatically. Supabase also offers a custom domain add-on for the API URL itself, which is a paid feature, so decide whether your users ever see that URL before paying for it.

Cost control

Hosting is usually cheap; databases and functions are where surprises appear. A few habits help:

  • Set budget alerts in the Google Cloud console and the Supabase dashboard before launch.
  • Keep staging and dev on free or small plans, and delete preview channels you no longer need.
  • Cache static assets aggressively so the CDN, not your origin, answers requests.
  • Watch the metered items: Firestore reads, function invocations and egress bandwidth on Firebase; database size, egress and monthly active users on Supabase.
  • Never leave a runaway loop or an unindexed listener on in production; see Security Pitfalls in Both Platforms for abuse budgets.

Prices and free tiers change, so always read the current pricing pages instead of trusting a number from a tutorial.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why do single-page apps on Firebase Hosting need a rewrite to /index.html?

    A URL such as /orders/7 is not a file. The rewrite returns index.html and lets your router show the right page.
  2. 2. Where should a pull request preview point its backend?

    Previews are shared with reviewers. Pointing them at staging keeps real user data safe.
  3. 3. Which value may be placed in a browser environment variable?

    URL and anon key are public by design and protected by RLS. The others are secrets that would end up in the bundle.
  4. 4. Why should migrations be backwards compatible?

    Redeploying an old front end is easy. Reverting a destructive schema change is not, so add first and remove later.

Summary

  • Firebase Hosting serves a built folder from a CDN; firebase.json holds rewrites, headers and the public folder.
  • Supabase hosts the back end only; the front end goes to any host and receives the public URL and anon key.
  • Use a separate project per environment, name the target in every deploy command, and point previews at staging.
  • Deploy from CI with secrets in the CI system: migrations first, then code, with approval for production.
  • Rollbacks are instant for code and hard for data, so keep migrations backwards compatible.
  • Set budget alerts and watch the metered items.

Next: Modeling Data: Documents vs Tables