Skip to content

fix(messaging): inject null where the browser has no service worker - #3852

Open
armando-navarro wants to merge 1 commit into
angular:mainfrom
armando-navarro:fix-messaging-without-service-worker
Open

armando-navarro wants to merge 1 commit into
angular:mainfrom
armando-navarro:fix-messaging-without-service-worker

Conversation

@armando-navarro

Copy link
Copy Markdown
Collaborator

Checklist

  • Issue number for this PR: Running FCM on IP stops Angular rendering #3672
  • Docs included?: in the docs PR for 22.0.0, which I am opening alongside this one. Its commit adds one sentence to docs/messaging.md and null checks to the example service.
  • Test units included?: yes, four specs in src/messaging/messaging.spec.ts.
  • Breaking change?: no.
  • In a clean directory, yarn install, yarn test run successfully?: yes, with npm: npm ci, then the build, lint and spec runs under Verification.

Description

Fixes #3672

Refs #3630

Injecting Messaging throws in a browser that has no navigator.serviceWorker, and when the component that injects it is on the first screen, the app does not render.

With this change Messaging injects as null there, as it did through 18.x.

Changes

  • src/messaging/messaging.module.ts: the factory behind provideMessaging still runs the app's own factory. When that throws and navigator has no serviceWorker, it injects null. Any other throw passes through unchanged.

  • src/core.ts: with two provideMessaging registrations both entries are null in such a browser, and ɵgetDefaultInstanceOf read .app on them. It now skips null entries. The same throw existed on the server for two registrations.

  • src/messaging/messaging.spec.ts: four specs. Three remove serviceWorker from Navigator.prototype, which is what a page outside a secure context looks like.

Behavior to know

  • Where Messaging is null, app code has to check it before calling onMessage, onRegistered and the like. That was already true during server rendering.

  • Firebase still logs its own messaging/unsupported-browser error in those browsers, because getMessaging() starts that check before it throws. I left it, since it tells the developer why Messaging is null.

  • The app's factory runs first on purpose. Checking the browser first would ignore a factory that returns its own object, for example a test double in a jsdom unit test, where navigator.serviceWorker does not exist either.

  • Only a missing service worker is covered. A browser that has service workers but fails the rest of isSupported(), such as an iPhone Safari tab with no PushManager, gets an instance today and still does. onMessage(null, ...) throws, so injecting null there would break apps that run there now.

  • I did not bring back the app initializer that awaited isSupported(). In an 18.0.1 app with only provideMessaging registered, nothing renders when IndexedDB's open never answers.

  • Not covered: an iframe sandboxed without allow-same-origin. There Chrome reports 'serviceWorker' in navigator as true and throws when the property is read, so injection still throws as it does today.

Verification

  • Reproduced on real apps loaded over a LAN IP address with a component injecting Messaging: 20.0.1, 21.0.0 and 22.0.0-rc.1 render nothing and log the two errors quoted in Running FCM on IP stops Angular rendering #3672, while 18.0.1 renders with Messaging null.
  • With this branch's build in an Angular 22.2.6 app: the page renders on the LAN IP address with Messaging null, and localhost still gets an instance.
  • Build, ng lint, test:node and test:node-esm (440 specs, 0 failures each) pass.
  • Chrome headless through firebase-tools emulators:exec: 192 of 240 executed, 48 skipped, all passing.
  • I mutation-tested the change four ways, and each one fails only the specs written for it: always rethrowing (2 specs fail), never rethrowing (1), checking the browser before running the factory (1), and reading .app without the null check (1).
  • Not run: a real iOS in-app browser and a Firefox private window, which also has no navigator.serviceWorker.

The 21.1.x backport follows as a cherry-pick PR after this merges.

Firebase's getMessaging() calls navigator.serviceWorker.addEventListener
without checking that navigator.serviceWorker exists. On a page that is
not a secure context, and in iOS in-app browsers, it does not exist, so
injecting Messaging threw and the app failed to bootstrap. Until 19.0.0
the providers awaited isSupported() and injected null there. angular#3588
removed that check.

Run the app's factory as before. When it throws and navigator has no
serviceWorker, inject null. Any other throw still propagates. Checking
before running the factory would have ignored a factory that returns its
own object, such as a test double under jsdom.

With two provideMessaging registrations both entries are then null, and
ɵgetDefaultInstanceOf read .app on them. Skip null entries there.

Fixes angular#3672
Refs angular#3630
@armando-navarro armando-navarro added backport: 21.1.x Cherry-pick onto the 21.1.x branch for a 21.1 minor release bump: patch Size of the change: a fix or docs change that reaches users comp: core FirebaseApp DI core (src/app). comp: messaging Cloud Messaging / FCM (src/messaging). type: bug Defect: expected behavior doesn't happen. labels Nov 25, 2026
@armando-navarro armando-navarro added this to the 22.0.0 milestone Nov 25, 2026

@tyler-reitz tyler-reitz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I ran the messaging specs in Chrome and reverted each half of the fix to check they go red, and they do. One question: the description says a docs PR for messaging.md is going up alongside this, but the only open docs PR I see is #3850, which is remote-config. Is the messaging one still coming?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport: 21.1.x Cherry-pick onto the 21.1.x branch for a 21.1 minor release bump: patch Size of the change: a fix or docs change that reaches users comp: core FirebaseApp DI core (src/app). comp: messaging Cloud Messaging / FCM (src/messaging). type: bug Defect: expected behavior doesn't happen.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Running FCM on IP stops Angular rendering

2 participants