diff --git a/packages-exp/messaging-exp/src/helpers/isSupported.ts b/packages-exp/messaging-exp/src/helpers/isSupported.ts index c73122eaad1..2c7c0618fdc 100644 --- a/packages-exp/messaging-exp/src/helpers/isSupported.ts +++ b/packages-exp/messaging-exp/src/helpers/isSupported.ts @@ -15,20 +15,10 @@ * limitations under the License. */ -export function isSupported(): boolean { - if (self && 'ServiceWorkerGlobalScope' in self) { - // Running in ServiceWorker context - return isSWControllerSupported(); - } else { - // Assume we are in the window context. - return isWindowControllerSupported(); - } -} - /** * Checks to see if the required APIs exist. */ -function isWindowControllerSupported(): boolean { +export function isWindowSupported(): boolean { return ( 'indexedDB' in window && indexedDB !== null && @@ -45,7 +35,7 @@ function isWindowControllerSupported(): boolean { /** * Checks to see if the required APIs exist within SW Context. */ -function isSWControllerSupported(): boolean { +export function isSwSupported(): boolean { return ( 'indexedDB' in self && indexedDB !== null && diff --git a/packages-exp/messaging-exp/src/helpers/register.ts b/packages-exp/messaging-exp/src/helpers/registerMessaging.ts similarity index 86% rename from packages-exp/messaging-exp/src/helpers/register.ts rename to packages-exp/messaging-exp/src/helpers/registerMessaging.ts index cb6b99259bb..367e7781b3f 100644 --- a/packages-exp/messaging-exp/src/helpers/register.ts +++ b/packages-exp/messaging-exp/src/helpers/registerMessaging.ts @@ -21,19 +21,13 @@ import { ComponentType, InstanceFactory } from '@firebase/component'; -import { ERROR_FACTORY, ErrorCode } from '../util/errors'; import { MessagingService } from '../messaging-service'; import { _registerComponent } from '@firebase/app-exp'; -import { isSupported } from './isSupported'; const messagingFactory: InstanceFactory<'messaging-exp'> = ( container: ComponentContainer ) => { - if (!isSupported()) { - throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); - } - return new MessagingService( container.getProvider('app-exp').getImmediate(), container.getProvider('installations-exp-internal').getImmediate(), diff --git a/packages-exp/messaging-exp/src/index.sw.ts b/packages-exp/messaging-exp/src/index.sw.ts index 75e588fc62a..2862fb783cb 100644 --- a/packages-exp/messaging-exp/src/index.sw.ts +++ b/packages-exp/messaging-exp/src/index.sw.ts @@ -15,10 +15,17 @@ * limitations under the License. */ + import { FirebaseMessaging } from './interfaces/public-types'; import { registerMessaging } from './helpers/register'; import '@firebase/installations-exp'; +import { ERROR_FACTORY, ErrorCode } from './util/errors'; + +import { FirebaseMessaging } from '@firebase/messaging-types-exp'; +import { isSwSupported } from './helpers/isSupported'; +import { registerMessaging } from './helpers/registerMessaging'; + export { onBackgroundMessage, getMessaging } from './api'; declare module '@firebase/component' { @@ -27,4 +34,8 @@ declare module '@firebase/component' { } } +if (!isSwSupported()) { + throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); +} + registerMessaging(); diff --git a/packages-exp/messaging-exp/src/index.ts b/packages-exp/messaging-exp/src/index.ts index fd12f5b4867..004574cdcae 100644 --- a/packages-exp/messaging-exp/src/index.ts +++ b/packages-exp/messaging-exp/src/index.ts @@ -28,4 +28,8 @@ declare module '@firebase/component' { } } +if (!isWindowSupported()) { + throw ERROR_FACTORY.create(ErrorCode.UNSUPPORTED_BROWSER); +} + registerMessaging();