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(); + }); + + }); + });