Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firebase serve inconsistently adds appId to /__/firebase/init.js #1945

Open
katowulf opened this issue Jan 29, 2020 · 13 comments
Open

firebase serve inconsistently adds appId to /__/firebase/init.js #1945

katowulf opened this issue Jan 29, 2020 · 13 comments

Comments

@katowulf
Copy link

[REQUIRED] Environment info

firebase-tools: 7.12.1

Platform: macOS and Windows (both on 7.12.1)

[REQUIRED] Test case

Reproducing is inconsistent. However, I encountered this using the Messaging quickstart with this index.html file.

I can share the demo project if you want to repro locally.

[REQUIRED] Steps to reproduce

git clone https://github.com/firebase/quickstart-js.git
cd quickstart-js/messaging
firebase serve -p 8081
open https://localhost:8081/__/firebase/init.js
Note that the appId is not present.
Try visiting https://localhost:8081 and note the JS errors (quickstart won't run)

This was reproducible in several projects in different states (with only one web app, with multiple web apps, old projects and newly created ones, multiple accounts). It's consistent for any given app, but inconsistent in which apps work.

Also important to note, the apps DO HAVE an appId when viewed in the console:
image

[REQUIRED] Expected behavior

The init.js file should contain appId.

[REQUIRED] Actual behavior

The init.js file contains something similar to the following:

if (typeof firebase === 'undefined') throw new Error('hosting/init-error: Firebase SDK not detected. You must include it before /__/firebase/init.js');
var firebaseConfig = {
  "projectId": "kato-...",
  "databaseURL": "https://kato-...io.com",
  "storageBucket": "kato-...ot.com",
  "locationId": "us-central",
  "apiKey": "AIza...ePs",
  "authDomain": "kato-...app.com",
  "messagingSenderId": "83...95"
};
if (firebaseConfig) {
  firebase.initializeApp(firebaseConfig);
}
@katowulf katowulf changed the title firebase serve fails to add appId to /__/firebase/init.js inconsistently firebase serve inconsistently adds appId to /__/firebase/init.js Jan 29, 2020
@samtstern
Copy link
Contributor

samtstern commented Jan 29, 2020 via email

@google-oss-bot
Copy link
Contributor

Hey @katowulf. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 3 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@samtstern samtstern removed Needs: Author Feedback Issues awaiting author feedback no-recent-activity labels Mar 4, 2020
@samtstern
Copy link
Contributor

@kmcnellis showing no mercy throwing the stale bot at @katowulf !

@katowulf
Copy link
Author

katowulf commented Apr 5, 2020

In this particular case, it wasn't flaky connectivity, given that it's consistent for a given app/project combination, but inconsistent between multiple apps/projects. Multiple users tried about 10 projects in different states (with only one web app, with multiple web apps, old projects and newly created ones, multiple accounts) but couldn't pin down a pattern.

@Mauratay
Copy link

I have the same problem, did you all come up with a possible cause on this?

@katowulf
Copy link
Author

@Mauratay I think adding the appId into init.js will resolve the issue. Most likely, Sam is correct and it's because the server is not available at the point the project is initialized and the init.js is created.

@Mauratay
Copy link

Makes sense, wanted to know if it was a problem in particular with the project. Thanks

@samtstern
Copy link
Contributor

We wanted to make the Hosting emulator work offline so we made the helper that fetches the web config for __init.js into a soft fail rather than a hard fail. However what I think is happening is now it's failing for some people just due to spotty connection / intermittent backend errors and producing the strange case where you are online but your __init.js file is incomplete.

I would guess 9 times out of 10 restarting the emulator fixes it, but if you get it consistently please share debug logs!

@ghivert
Copy link

ghivert commented Nov 7, 2020

Hi!

I’m using the Firebase emulators for 6 months, and I got a strange bug some weeks ago.
I’m using a lot of Firebase services and I’m using them with the hosting emulator like this:

    <script src="/__/firebase/8.0.1/firebase-app.js"></script>
    <script src="/__/firebase/8.0.1/firebase-auth.js"></script>
    <script src="/__/firebase/8.0.1/firebase-firestore.js"></script>
    <script src="/__/firebase/8.0.1/firebase-storage.js"></script>
    <script src="/__/firebase/8.0.1/firebase-functions.js"></script>
    <script src="/__/firebase/8.0.1/firebase-analytics.js"></script>
    <script src="/__/firebase/8.0.1/firebase-performance.js"></script>
    <script src="/__/firebase/init.js?useEmulator=true"></script>

One day, i got an error message Missing appId in configuration from firebase-analytics.

I struggled on this, and go to see /__/firebase/init.js. Strangely, it’s not packing the appId, although Firebase Analytics is configured on the Firebase console. So, the init.js served by the emulator:

if (typeof firebase === 'undefined') throw new Error('hosting/init-error: Firebase SDK not detected. You must include it before /__/firebase/init.js');
var firebaseConfig = {
  "projectId": "projectId",
  "databaseURL": "databaseURL",
  "storageBucket": "storageBucket,
  "locationId": "europe-west",
  "apiKey": "apiKey",
  "authDomain": "authDomain",
  "messagingSenderId": "messagingSenderId"
};
if (firebaseConfig) {
  firebase.initializeApp(firebaseConfig);

  var firebaseEmulators = undefined;
  if (firebaseEmulators) {
    console.log("Automatically connecting Firebase SDKs to running emulators:");
    Object.keys(firebaseEmulators).forEach(function(key) {
      console.log('\t' + key + ': http://' +  firebaseEmulators[key].host + ':' + firebaseEmulators[key].port );
    });

    if (firebaseEmulators.database && typeof firebase.database === 'function') {
      firebase.database().useEmulator(firebaseEmulators.database.host, firebaseEmulators.database.port);
    }

    if (firebaseEmulators.firestore && typeof firebase.firestore === 'function') {
      firebase.firestore().useEmulator(firebaseEmulators.firestore.host, firebaseEmulators.firestore.port);
    }

    if (firebaseEmulators.functions && typeof firebase.functions === 'function') {
      firebase.functions().useEmulator(firebaseEmulators.functions.host, firebaseEmulators.functions.port);
    }

    if (firebaseEmulators.auth && typeof firebase.auth === 'function') {
      firebase.auth().useEmulator('http://' + firebaseEmulators.auth.host + ':' + firebaseEmulators.auth.port);
    }
  } else {
    console.log("To automatically connect the Firebase SDKs to running emulators, replace '/__/firebase/init.js' with '/__/firebase/init.js?useEmulator=true' in your index.html");
  }
}

I couldn’t get the appId. The quick and dirty solution is to use firebase-analytics only in production, but it’s weird that one day, the emulator just stopped working. Is there anything I can do?

@samtstern
Copy link
Contributor

@ghivert thank you for the clear report! That's actually a new issue, let me open a new one to discuss.

@samtstern
Copy link
Contributor

@ghivert #2798

@kmcnellis kmcnellis self-assigned this Mar 5, 2021
@kmcnellis kmcnellis removed their assignment Sep 16, 2022
@yuchenshi
Copy link
Member

I'm closing this for now since we've had a lot of CLI updates since then, but please let us know if this is still happening to the latest version

@seboliver16
Copy link

This issue is 100% still happening and painful

@kmcnellis kmcnellis reopened this Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants