Skip to content

Commit a377c68

Browse files
committed
issue firebase#2393 fix for analytics module
1 parent 3ac0fe3 commit a377c68

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

packages/analytics/index.ts

+46
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ export function registerAnalytics(instance: _FirebaseNamespace): void {
5656
.getProvider('installations')
5757
.getImmediate();
5858

59+
if (!isSupported()) {
60+
throw ERROR_FACTORY.create(AnalyticsError.UNSUPPORTED_BROWSER);
61+
}
62+
5963
return factory(app, installations);
6064
},
6165
ComponentType.PUBLIC
@@ -97,8 +101,50 @@ registerAnalytics(firebase as _FirebaseNamespace);
97101
declare module '@firebase/app-types' {
98102
interface FirebaseNamespace {
99103
analytics(app?: FirebaseApp): FirebaseAnalytics;
104+
isSupported(): boolean;
100105
}
101106
interface FirebaseApp {
102107
analytics(): FirebaseAnalytics;
103108
}
104109
}
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+
}

packages/analytics/src/errors.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ export const enum AnalyticsError {
2222
NO_GA_ID = 'no-ga-id',
2323
ALREADY_EXISTS = 'already-exists',
2424
ALREADY_INITIALIZED = 'already-initialized',
25-
INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed'
25+
INTEROP_COMPONENT_REG_FAILED = 'interop-component-reg-failed',
26+
UNSUPPORTED_BROWSER = 'unsupported-browser'
2627
}
2728

2829
const ERRORS: ErrorMap<AnalyticsError> = {
@@ -39,7 +40,9 @@ const ERRORS: ErrorMap<AnalyticsError> = {
3940
'settings() must be called before initializing any Analytics instance' +
4041
'or it will have no effect.',
4142
[AnalyticsError.INTEROP_COMPONENT_REG_FAILED]:
42-
'Firebase Analytics Interop Component failed to instantiate'
43+
'Firebase Analytics Interop Component failed to instantiate',
44+
[AnalyticsError.UNSUPPORTED_BROWSER]:
45+
"This browser doesn't support the API's required to use the firebase SDK."
4346
};
4447

4548
interface ErrorParams {

packages/firebase/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5083,6 +5083,7 @@ declare namespace firebase.analytics {
50835083
id?: string;
50845084
name?: string;
50855085
}
5086+
function isSupported(): boolean;
50865087
}
50875088

50885089
declare namespace firebase.auth.Auth {

0 commit comments

Comments
 (0)