Repository navigation
fix(messaging): inject null where the browser has no service worker - #3852
Open
armando-navarro wants to merge 1 commit into
Open
armando-navarro wants to merge 1 commit into
armando-navarro wants to merge 1 commit into
Conversation
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
tyler-reitz
approved these changes
Nov 25, 2026
tyler-reitz
left a comment
Collaborator
There was a problem hiding this comment.
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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Checklist
22.0.0, which I am opening alongside this one. Its commit adds one sentence todocs/messaging.mdandnullchecks to the example service.src/messaging/messaging.spec.ts.yarn install,yarn testrun successfully?: yes, with npm:npm ci, then the build, lint and spec runs under Verification.Description
Fixes #3672
Refs #3630
Injecting
Messagingthrows in a browser that has nonavigator.serviceWorker, and when the component that injects it is on the first screen, the app does not render.Where it happens: a page that is not a secure context, such as an app opened from a phone over plain HTTP by IP address (Running FCM on IP stops Angular rendering #3672), and iOS in-app browsers (ServiceWorker registration fails in Facebook/Instagram in-app browser on iOS #3630).
Why: Firebase's
getMessaging()callsnavigator.serviceWorker.addEventListenerwithout checking thatnavigator.serviceWorkerexists.Since when: 19.0.0. Until then the providers waited for Firebase's
isSupported()and injectednullin those browsers. Drop isSupported injection, bump rxfire #3588 removed that.With this change
Messaginginjects asnullthere, as it did through 18.x.Changes
src/messaging/messaging.module.ts: the factory behindprovideMessagingstill runs the app's own factory. When that throws andnavigatorhas noserviceWorker, it injectsnull. Any other throw passes through unchanged.src/core.ts: with twoprovideMessagingregistrations both entries arenullin such a browser, andɵgetDefaultInstanceOfread.appon them. It now skipsnullentries. The same throw existed on the server for two registrations.src/messaging/messaging.spec.ts: four specs. Three removeserviceWorkerfromNavigator.prototype, which is what a page outside a secure context looks like.Behavior to know
Where
Messagingisnull, app code has to check it before callingonMessage,onRegisteredand the like. That was already true during server rendering.Firebase still logs its own
messaging/unsupported-browsererror in those browsers, becausegetMessaging()starts that check before it throws. I left it, since it tells the developer whyMessagingisnull.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.serviceWorkerdoes 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 noPushManager, gets an instance today and still does.onMessage(null, ...)throws, so injectingnullthere 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 onlyprovideMessagingregistered, nothing renders when IndexedDB'sopennever answers.Not covered: an iframe sandboxed without
allow-same-origin. There Chrome reports'serviceWorker' in navigatoras true and throws when the property is read, so injection still throws as it does today.Verification
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 withMessagingnull.Messagingnull, andlocalhoststill gets an instance.ng lint,test:nodeandtest:node-esm(440 specs, 0 failures each) pass.firebase-tools emulators:exec: 192 of 240 executed, 48 skipped, all passing..appwithout thenullcheck (1).navigator.serviceWorker.The
21.1.xbackport follows as a cherry-pick PR after this merges.