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

[7.2.0] Angular HMR: FirebaseError: Firestore has already been started and persistence can no longer be enabled #3135

Open
hakimio opened this issue Jan 27, 2022 · 5 comments

Comments

@hakimio
Copy link

hakimio commented Jan 27, 2022

Version info

Angular: 13.2

Firebase: 9.6

AngularFire: 7.2

How to reproduce these conditions

Steps to set up and reproduce

  • serve Angular project with ng serve --hmr
  • make some change in the application

Debug output

** Errors in the JavaScript console **

FirebaseError: Firestore has already been started and persistence can no longer be enabled. You can only enable persistence before calling any other methods on a Firestore object.

Expected behavior

No errors.

Actual behavior

Project fails to run with the above error.

Might be related to #2655

@DibyodyutiMondal
Copy link

DibyodyutiMondal commented Apr 21, 2022

I am getting an error in HMR mode too

I sign in with a user from the auth emulator, and then make changes in the code, and save.

Browser console error after making changes

FirebaseError: Firebase: Error (auth/emulator-config-failed).
    at createErrorInternal (index-7078a255.js:473:41)
    at _assert (index-7078a255.js:479:15)
    at connectAuthEmulator (index-7078a255.js:2569:5)
    at angular-fire.js:227:48
    at angular-fire.js:160:59
    at _ZoneDelegate.invoke (zone.js:372:1)
    at Zone.run (zone.js:134:1)
    at NgZone.runOutsideAngular (core.mjs:25486:1)
    at runOutsideAngular (angular-fire.js:160:35)
    at angular-fire.js:227:21

Error is being thrown from inside connectAuthEmulator. In my code, connectAuthEmulator is called inside of provideAuth(), but only if the app is in development mode. In turn, provideAuth is being used inside of AppModule imports array.

function connectAuthEmulator(auth, url, options) {
    const authInternal = _castAuth(auth);
    _assert(authInternal._canInitEmulator, authInternal, "emulator-config-failed" /* EMULATOR_CONFIG_FAILED */);
    _assert(/^https?:\/\//.test(url), authInternal, "invalid-emulator-scheme" /* INVALID_EMULATOR_SCHEME */);
    ...

@fatalcaron
Copy link

Same thing for me, I disable the Anuglar cli --hmr mode and it's working. Waiting for a fix or a workaround.

@DibyodyutiMondal
Copy link

I won't say I needed to disable hmr.

    const auth = getAuth(app);
    // check for dev mode, and if emulator config is set by previous hmr
    if (dev_mode && !auth.emulatorConfig) {
      connectAuthEmulator(auth, 'http://localhost:5001', { disableWarnings: true });
    }

@hakimio
Copy link
Author

hakimio commented May 23, 2022

So, basically for Firestore error, similar workaround might work:

        provideFirestore(() => {
            const firestore = getFirestore();
            if (!firestore['_initialized']) {
                enableMultiTabIndexedDbPersistence(firestore);
            }

            return firestore;
        }),

and functions:

        provideFunctions(() => {
            const functions = getFunctions(getApp(), environment.firebase.region);
            if (!environment.production && !functions['emulatorOrigin']) {
                connectFunctionsEmulator(functions, environment.firebase.host, environment.firebase.port);
            }
            return functions;
        }),

@fatalcaron
Copy link

Thanks, this workaround work for me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants