Search code examples
javascriptfirebasegoogle-cloud-platformgoogle-cloud-firestorefirebase-hosting

Is Firebase Config still necessary if website will be hosted at Firebase itself?


When a website is hosted at Firebase and not plan to initialize other project, do you still need to explicitly initialize it?

// Initialize default app
// Retrieve your own options values by adding a web app on
// https://console.firebase.google.com
firebase.initializeApp({
  apiKey: "AIza....",                             // Auth / General Use
  appId: "1:27992087142:web:ce....",              // General Use
  projectId: "my-firebase-project",               // General Use
  authDomain: "YOUR_APP.firebaseapp.com",         // Auth with popup/redirect
  databaseURL: "https://YOUR_APP.firebaseio.com", // Realtime Database
  storageBucket: "YOUR_APP.appspot.com",          // Storage
  messagingSenderId: "123456789",                 // Cloud Messaging
  me

asurementId: "G-12345"                        // Analytics
});


if (!firebase.apps.length)
  firebase.initializeApp(firebaseConfig);

https://firebase.google.com/docs/reference/js/v8/firebase#initializeapp

Currently using version 8

 <!-- Add Firebase products that you want to use -->
  <script src="/__/firebase/8.1.1/firebase-auth.js"></script>
  <script src="/__/firebase/8.1.1/firebase-firestore.js"></script>
  <script src="/__/firebase/8.1.1/firebase-storage.js"></script>

Solution

  • As soon as you host a web app which interacts with a Firebase service like DBs, Cloud Storage, Authentication, etc. you need to initialize the app as shown in your question. This is valid if you host this web app on Firebase Hosting or anywhere else.

    On the other hand, if you just want to host a web app on Firebase Hosting without any interaction with a Firebase service (e.g. a website composed of a set of static HTML/CSS files, or an app that calls APIs exposed somewhere else, etc.) you don't need to initialize the app in your app code.