How to start using Firebase hosting

Firebase is a Google Cloud service, a clear product, mainly for mobile applications. Firebase hosting is just a small part of it.

Introduction to Firebase

Firebase is a mobile and web application development platform developed by Firebase, Inc. in 2011 and was acquired by Google in 2014.

Therefore, now Firebase is not only the Google Cloud service, but also the flagship product of the Google Cloud service.

Firebase is a complex and clear product, mainly for mobile applications.

However, one of its features is an advanced web hosting service.

Firebase hosting features

Firebase hosting provides hosting for static websites. For example, you can use a website generated by a static website generator, or even a website built using a server-side CMS platform, from which you can generate a static copy of the website.

You can host anything, as long as it is not dynamic. For example, if you use Disqus or Facebook comments, a WordPress blog is almost always a good choice and can become a static site.

Firebase hosting can deliver files quicklyCDN, Use HTTPS and provide automatic SSL certificates with custom domain support.

itsThe free tier is generous, A cheap plan (if you no longer buy it), and very friendly to developers: Firebase provides CLI interface tools, easy deployment process and one-click rollback

Why use Firebase hosting?

Firebase is a good choice for deploying static websites and single-page applications.

The reason why I like to use Firebase Hosting is mainly because I have tested many different providers and Firebase providesAmazing speedAcross all continents without the need to put a separate CDN on top, becauseCDN is built-infree.

Similarly, having your own VPS is also a good choice.I don't want to manage my own serverFor a simple website, I prefer to focus on the content rather than the operation, just like I deploy an application on Heroku.

Firebase is easier to set up than Heroku.

Install Firebase CLI tools

Install Firebase CLI using the following commandnpm:

npm install -g firebase-tools

or

yarn global add firebase-tools

and authenticate with the Google account (I assume you already have a Google account) by running

firebase login

Create a project on Firebase

Go to https://console.firebase.google.com/ and create a new project.

A new project on Firebase

Now back to the console, from the site you’re working on, in the root folder, run

firebase init

Firebase initialized

Choose “Hosting” by pressing space, then enter to go on.

Now you need to choose the project you want to deploy the site to.

Choose firebase project

Choose “create a new project”.

Now you choose which folder contains the static version of your site. For example, public.

Reply “No” to the Configure as a single-page app (rewrite all urls to /index.html)? question, and also reply “No” to File public/index.html already exists. Overwrite? to avoid Firebase to add its own default index.html file.

You’re good to go:

Project created

Configure the site

The Firebase CLI app created the firebase.json file in the root site folder.

In this article I tell how to configure a simple feature in Firebase Hosting, by adding a small bit of configuration in the firebase.json file.

I want to set the Cache-Control header directive on all the site assets: images as well as CSS and JS files.

A clean firebase.json file contains the following:

{
  "hosting": {
    "public": "public",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"]
  }
}

It tells Firebase where is the site content, and which files it should ignore. Feel free to add all the folders you have, except public.

We’re going to add a new property in there, called headers:

{
  "hosting": {
    "public": "public",
    "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
    "headers": [
      {
        "source": "**/*[email protected](jpg|jpeg|gif|png|css|js)",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "max-age=1000000" //1 week+
          }
        ]
      }
    ]
  }
}

As you can see we tell that for all files ending with jpg|jpeg|gif|png|css|js Firebase should apply the Cache-Control:max-age=1000000` directive, which means all assets are cached for more than 1 week.

Publish the site

When you are ready to publish the site, you just run

firebase deploy

and Firebase takes care of everything.

You can now open https://yourproject.firebaseapp.com and you should see the website running.

Custom Domain

The next logical step is to make your site use a custom domain.

Go to https://console.firebase.google.com/project/_/hosting/main and click the “Connect Domain” button:

Setup a custom domain

The wizard will ask you for the domain name, then it will provide a TXT record you need to add to your hosting DNS panel to verify the domain.

If the domain is brand new, it might take some time before you can pass this step.

Once this is done, the interface will give you two A records to add as well to your hosting DNS panel.

If you set up yourdomain.com, don’t forget to also set up www.yourdomain.com, by making it a redirect.

Connect a domain

Now you just have to wait for your hosting to update the DNS records and for DNS caches to flush.

Also, keep in mind that your SSL certificate is automatically provisioned but requires a bit of time to be valid.


More services tutorials: