It’s getting popular to use Firebase hosting to host static website, however, you might not know you can still have server-side code hosted in Firebase for your static website, and that is the Firebase Cloud Function. Once of the case you might want to use the Cloud Function is to write something to Firebase database, for example, a hit counter. In this topic, I’ll show you how to use Firebase Cloud Function and Firebase Realtime Database to count website visitor.

Deploy static page to Firebase Cloud Hosting

Firebase Cloud Hosting is a great place to host your static pages. It’s easier to setup than GitHub Pages, and you get a set of functionalities that designed for web developers. Once you have your static pages ready, do firebase deploy and it will be deployed to the cloud and accessible from anywhere.

Refer to Quickstart from documentation to get started with Firebase Cloud Hosting.

Create Firebase Cloud Function

Now, let’s make use of Firebase Cloud Function to host some server-side logic to count website hit. In this piece of code, we will connect Cloud Function with Firebase Realtime Database so that the function can update hit counter stored in the database.