From 8081e90095319fd710a521dd622e7acea0d75423 Mon Sep 17 00:00:00 2001 From: Armando Navarro Date: Wed, 25 Nov 2026 13:56:11 -0800 Subject: [PATCH] fix(messaging): inject null where the browser has no service worker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. #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 #3672 Refs #3630 --- src/core.ts | 5 ++- src/messaging/messaging.module.ts | 12 ++++- src/messaging/messaging.spec.ts | 75 +++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/src/core.ts b/src/core.ts index e0ad8f7f5..f8c7e1533 100644 --- a/src/core.ts +++ b/src/core.ts @@ -18,7 +18,10 @@ export function ɵgetDefaultInstanceOf(identifier: string, provided: if (provided) { // Was provide* only called once? If so grab that if (provided.length === 1) { return provided[0]; } - const providedUsingDefaultApp = provided.filter((it: any) => it.app === defaultApp); + + // An entry is null where its product is unavailable, such as messaging on the server. + const providedUsingDefaultApp = provided.filter((it: any) => it?.app === defaultApp); + // Was provide* only called once, using the default app? If so use that if (providedUsingDefaultApp.length === 1) { return providedUsingDefaultApp[0]; } } diff --git a/src/messaging/messaging.module.ts b/src/messaging/messaging.module.ts index 0942e053a..7a0f34c4e 100644 --- a/src/messaging/messaging.module.ts +++ b/src/messaging/messaging.module.ts @@ -26,8 +26,16 @@ export function defaultMessagingInstanceFactory(provided: FirebaseMessaging[]|un export function messagingInstanceFactory(fn: (injector: Injector) => FirebaseMessaging) { return (zone: NgZone, injector: Injector, platformId: object) => { if (isPlatformServer(platformId)) { return null; } - const messaging = zone.runOutsideAngular(() => fn(injector)); - return new Messaging(messaging); + + try { + const messaging = zone.runOutsideAngular(() => fn(injector)); + return new Messaging(messaging); + } catch (error) { + // Firebase's `getMessaging` throws where `navigator.serviceWorker` is missing. + if ('serviceWorker' in navigator) { throw error; } + + return null; + } }; } diff --git a/src/messaging/messaging.spec.ts b/src/messaging/messaging.spec.ts index 5a48de626..e0e57455c 100644 --- a/src/messaging/messaging.spec.ts +++ b/src/messaging/messaging.spec.ts @@ -38,4 +38,79 @@ describe('Messaging', () => { }); + describe('when the factory throws', () => { + + it('should rethrow in a browser with service workers', () => { + TestBed.configureTestingModule({ + providers: [ + provideFirebaseApp(() => initializeApp(COMMON_CONFIG, rando())), + provideMessaging(() => { throw new Error('thrown by the factory'); }), + ], + }); + + expect(() => TestBed.inject(Messaging)).toThrowError('thrown by the factory'); + }); + + }); + + describe('in a browser without service workers', () => { + let serviceWorkerProperty: PropertyDescriptor | undefined; + + beforeEach(() => { + // A page that is not a secure context has no `serviceWorker` property at all. + serviceWorkerProperty = Object.getOwnPropertyDescriptor(Navigator.prototype, 'serviceWorker'); + Reflect.deleteProperty(Navigator.prototype, 'serviceWorker'); + + appName = rando(); + }); + + afterEach(() => { + if (serviceWorkerProperty) { + Object.defineProperty(Navigator.prototype, 'serviceWorker', serviceWorkerProperty); + } + }); + + it('should inject null where getMessaging throws', () => { + TestBed.configureTestingModule({ + providers: [ + provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)), + provideMessaging(() => getMessaging(getApp(appName))), + ], + }); + + expect('serviceWorker' in navigator).toBeFalse(); + expect(TestBed.inject(Messaging)).toBeNull(); + }); + + it('should inject what the factory returns when it does not throw', () => { + let returnedByFactory: Messaging | undefined; + + TestBed.configureTestingModule({ + providers: [ + provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)), + provideMessaging(() => { + returnedByFactory = { app: getApp(appName) }; + return returnedByFactory; + }), + ], + }); + + expect(TestBed.inject(Messaging)).toBe(returnedByFactory); + }); + + it('should inject null with two registrations', () => { + TestBed.configureTestingModule({ + providers: [ + provideFirebaseApp(() => initializeApp(COMMON_CONFIG)), + provideMessaging(() => getMessaging()), + provideFirebaseApp(() => initializeApp(COMMON_CONFIG, appName)), + provideMessaging(() => getMessaging(getApp(appName))), + ], + }); + + expect(TestBed.inject(Messaging)).toBeNull(); + }); + + }); + });