GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Adding more injector safety by jamesdaniels · Pull Request #3590 · angular/angularfire

Conversation

@jamesdaniels

@jamesdaniels jamesdaniels commented Dec 13, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Zone wrapper noops for our other helpers
  • Add a warning / error on potential Zone / hydration issues
  • Pass injection context to zoneWrapFn
  • Pass injection context into the Promise wrapper
  • beforeAuthStateChanged should not block

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
jamesdaniels added 2 commits December 13, 2024 13:38
@jamesdaniels
Adding more injector safety
5fff7af
@jamesdaniels
better warning
a401cdb
@jamesdaniels jamesdaniels mentioned this pull request Dec 13, 2024
Firebase v11 support #3573
Open
jamesdaniels added 2 commits December 13, 2024 15:07
@jamesdaniels
Pass injector scope to promises
a184f69
@jamesdaniels
beforeAuthStateChanged should not block
8275675
@jamesdaniels
jamesdaniels merged commit 45ccd39 into angular:main Dec 13, 2024
@jamesdaniels
jamesdaniels deleted the jamesdaniels_moreInjectorSafety branch December 13, 2024 21:35
@davidgeary

davidgeary commented Dec 17, 2024

Copy link
Copy Markdown

@jamesdaniels Just a heads up...

I've just installed RC4 and noticed the "Firebase API called outside injection context" warning in the console. The error message also includes a link to "Find out more" pointing to https://github.com/angular/angularfire/blob/main/docs/zones.md (see line 87 in zone.ts) but this file doesn't appear to exist at the moment.

unimodern and mraustinmiller reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jamesdaniels

jamesdaniels commented Dec 17, 2024

Copy link
Copy Markdown
Contributor Author

@davidgeary thanks yeah, getting that page together now. You can likely safely ignore the warnings, they're only produced in dev mode and are intended to help developers track down any change-detection / rehydration instabilities

davidgeary, OlufemiOyewole, ibnYusrat, and Nicolas-Voges reacted with thumbs up emoji dolanmiu reacted with thumbs down emoji dolanmiu and rubenheymans reacted with confused emoji
All reactions
  • 👍 4 reactions
  • 👎 1 reaction
  • 😕 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@davidgeary

davidgeary commented Dec 17, 2024

Copy link
Copy Markdown

@jamesdaniels Ah, thanks. I was just starting to work through my code to see where the problem was, so you've saved me some time there!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@unimodern unimodern mentioned this pull request Jan 9, 2025
Firebase APIs outside of an Injection context #3607
Open
@ciriousjoker

ciriousjoker commented Jan 9, 2025

Copy link
Copy Markdown

Apparently, once you await something inside an injection context, you lose the injection context.

Here's our workaround:

/**
 * Runs an async function in the injection context. This can be awaited, unlike @see {runInInjectionContext}.
 * For some ungodly reason, only the first awaited call inside the fn callback is actually inside the injection context.
 * After something is awaited, the context is lost.
 *
 * NOTE: Use this sparingly and only when absolutely necessary.
 * This is a band-aid solution for this issue:
 * https://github.com/angular/angularfire/pull/3590
 *
 * @param injector The injector, usually inject(EnvironmentInjector)
 * @param fn The async callback to be awaited
 */
export async function runAsyncInInjectionContext<T>(injector: Injector, fn: () => Promise<T>): Promise<T> {
  return await runInInjectionContext(injector, () => {
    return new Promise((resolve, reject) => {
      fn().then(resolve).catch(reject);
    });
  });
}

Usage:

await runAsyncInInjectionContext(this.injector, async () => {
  await loadBundle(this.firestore, response);
  await getDocs(); // <-- this no longer is inside context
});
unimodern, dweb0, Dino-996, syamanashi, and wikett reacted with thumbs up emoji
All reactions
  • 👍 5 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@anisabboud anisabboud mentioned this pull request Jan 12, 2025
"Warning: Firebase API called outside injection context" on non-async functions #3611
Closed
@rgant rgant mentioned this pull request Feb 19, 2025
Inject() must be called from an injection context such as a constructor in AngularFirestoreDoc after upgrading to Angular v19 #3621
Open
@syamanashi

syamanashi commented Jul 5, 2025

Copy link
Copy Markdown
Contributor

Apparently, once you await something inside an injection context, you lose the injection context.

Here's our workaround:

/**
 * Runs an async function in the injection context. This can be awaited, unlike @see {runInInjectionContext}.
 * For some ungodly reason, only the first awaited call inside the fn callback is actually inside the injection context.
 * After something is awaited, the context is lost.
 *
 * NOTE: Use this sparingly and only when absolutely necessary.
 * This is a band-aid solution for this issue:
 * https://github.com/angular/angularfire/pull/3590
 *
 * @param injector The injector, usually inject(EnvironmentInjector)
 * @param fn The async callback to be awaited
 */
export async function runAsyncInInjectionContext<T>(injector: Injector, fn: () => Promise<T>): Promise<T> {
  return await runInInjectionContext(injector, () => {
    return new Promise((resolve, reject) => {
      fn().then(resolve).catch(reject);
    });
  });
}

Usage:

await runAsyncInInjectionContext(this.injector, async () => {
  await loadBundle(this.firestore, response);
  await getDocs(); // <-- this no longer is inside context
});

This workaround worked for me to suppress the console warnings in Angular v20.0. with "@angular/fire": "^20.0.1",. I tried every other method I could find on any thread about the issue and this is the only thing that suppresses the warnings.

Is this work-around still the only option?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sasos90

sasos90 commented Aug 22, 2025

Copy link
Copy Markdown

If i import the "getDocs" od "doc" or "collection" from "firebase/firestore" then i have no warnings. But if i use them from @angular/fire, then i get warnings. What is the solution here?
I use v19.2.0 of @angular/fire

AlonsoK28 reacted with hooray emoji AlonsoK28 reacted with eyes emoji
All reactions
  • 🎉 1 reaction
  • 👀 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@armando-navarro armando-navarro mentioned this pull request Sep 25, 2026
fix(auth): stop beforeAuthStateChanged from holding the app unstable #3770
Merged
armando-navarro added a commit that referenced this pull request Sep 27, 2026
@armando-navarro
fix(auth): stop beforeAuthStateChanged from holding the app unstable (#…
f182972
…3770)

AngularFire wrapped beforeAuthStateChanged so that registering the
hook added a pending task, cleared only when the callback first runs.
Firebase runs that callback only on a sign-in or sign-out, so for a
visitor who does neither the app never became stable. Registered on
the server, it failed ng build during route extraction and left
server-rendered requests without a response.

This restores the blockUntilFirst: false override from #3590, which
#3613 dropped without comment while adding log-level overrides next
to it. The callback still runs inside Angular's zone and injection
context, and its returned promise still reaches Firebase, so a
rejection still cancels the sign-in. A call outside an injection
context now logs its per-call warning only at the verbose level, as
onMessage does.

Fixes #3748

docs(auth): scope the beforeAuthStateChanged note to rc.1 and earlier

Merging this change closes #3748, so the section's present-tense note
would point at a closed issue. Also removed the false claim that the
@angular/fire/auth import makes ng build hang: the guide registers the
hook only in the browser, so its own build succeeds.
armando-navarro added a commit to armando-navarro/angularfire that referenced this pull request Oct 10, 2026
@armando-navarro
fix(auth): stop beforeAuthStateChanged from holding the app unstable (a…
69d94e6
…ngular#3770)

AngularFire wrapped beforeAuthStateChanged so that registering the
hook added a pending task, cleared only when the callback first runs.
Firebase runs that callback only on a sign-in or sign-out, so for a
visitor who does neither the app never became stable. Registered on
the server, it failed ng build during route extraction and left
server-rendered requests without a response.

This restores the blockUntilFirst: false override from angular#3590, which
angular#3613 dropped without comment while adding log-level overrides next
to it. The callback still runs inside Angular's zone and injection
context, and its returned promise still reaches Firebase, so a
rejection still cancels the sign-in. A call outside an injection
context now logs its per-call warning only at the verbose level, as
onMessage does.

Fixes angular#3748

docs(auth): scope the beforeAuthStateChanged note to rc.1 and earlier

Merging this change closes angular#3748, so the section's present-tense note
would point at a closed issue. Also removed the false claim that the
@angular/fire/auth import makes ng build hang: the guide registers the
hook only in the browser, so its own build succeeds.

The docs/auth.md change is left out, since it edits a section of the
guide that 20.1.x does not have.

(cherry picked from commit f182972)
armando-navarro added a commit that referenced this pull request Oct 10, 2026
@armando-navarro
fix(auth): stop beforeAuthStateChanged from holding the app unstable (#…
cc1fc15
…3770)

AngularFire wrapped beforeAuthStateChanged so that registering the
hook added a pending task, cleared only when the callback first runs.
Firebase runs that callback only on a sign-in or sign-out, so for a
visitor who does neither the app never became stable. Registered on
the server, it failed ng build during route extraction and left
server-rendered requests without a response.

This restores the blockUntilFirst: false override from #3590, which
#3613 dropped without comment while adding log-level overrides next
to it. The callback still runs inside Angular's zone and injection
context, and its returned promise still reaches Firebase, so a
rejection still cancels the sign-in. A call outside an injection
context now logs its per-call warning only at the verbose level, as
onMessage does.

Fixes #3748

docs(auth): scope the beforeAuthStateChanged note to rc.1 and earlier

Merging this change closes #3748, so the section's present-tense note
would point at a closed issue. Also removed the false claim that the
@angular/fire/auth import makes ng build hang: the guide registers the
hook only in the browser, so its own build succeeds.

The docs/auth.md change is left out, since it edits a section of the
guide that 20.1.x does not have.

(cherry picked from commit f182972)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@jamesdaniels @davidgeary @ciriousjoker @syamanashi @sasos90
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.