@@ -56,6 +56,10 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
56
56
. getProvider ( 'installations' )
57
57
. getImmediate ( ) ;
58
58
59
+ if ( ! isSupported ( ) ) {
60
+ throw ERROR_FACTORY . create ( AnalyticsError . UNSUPPORTED_BROWSER ) ;
61
+ }
62
+
59
63
return factory ( app , installations ) ;
60
64
} ,
61
65
ComponentType . PUBLIC
@@ -97,8 +101,50 @@ registerAnalytics(firebase as _FirebaseNamespace);
97
101
declare module '@firebase/app-types' {
98
102
interface FirebaseNamespace {
99
103
analytics ( app ?: FirebaseApp ) : FirebaseAnalytics ;
104
+ isSupported ( ) : boolean ;
100
105
}
101
106
interface FirebaseApp {
102
107
analytics ( ) : FirebaseAnalytics ;
103
108
}
104
109
}
110
+
111
+ function isSupported ( ) : boolean {
112
+ if ( self && 'ServiceWorkerGlobalScope' in self ) {
113
+ // Running in ServiceWorker context
114
+ return isSWControllerSupported ( ) ;
115
+ } else {
116
+ // Assume we are in the window context.
117
+ return isWindowControllerSupported ( ) ;
118
+ }
119
+ }
120
+
121
+ /**
122
+ * Checks to see if the required APIs exist.
123
+ */
124
+ function isWindowControllerSupported ( ) : boolean {
125
+ return (
126
+ 'indexedDB' in window &&
127
+ indexedDB !== null &&
128
+ navigator . cookieEnabled &&
129
+ 'serviceWorker' in navigator &&
130
+ 'PushManager' in window &&
131
+ 'Notification' in window &&
132
+ 'fetch' in window &&
133
+ ServiceWorkerRegistration . prototype . hasOwnProperty ( 'showNotification' ) &&
134
+ PushSubscription . prototype . hasOwnProperty ( 'getKey' )
135
+ ) ;
136
+ }
137
+
138
+ /**
139
+ * Checks to see if the required APIs exist within SW Context.
140
+ */
141
+ function isSWControllerSupported ( ) : boolean {
142
+ return (
143
+ 'indexedDB' in self &&
144
+ indexedDB !== null &&
145
+ 'PushManager' in self &&
146
+ 'Notification' in self &&
147
+ ServiceWorkerRegistration . prototype . hasOwnProperty ( 'showNotification' ) &&
148
+ PushSubscription . prototype . hasOwnProperty ( 'getKey' )
149
+ ) ;
150
+ }
0 commit comments