diff --git a/packages/analytics/src/trackers/EventTracker.ts b/packages/analytics/src/trackers/EventTracker.ts index a33317d8240..f8c147d078d 100644 --- a/packages/analytics/src/trackers/EventTracker.ts +++ b/packages/analytics/src/trackers/EventTracker.ts @@ -13,7 +13,7 @@ import { delegate } from '../vendor/dom-utils'; import { EventTrackOpts } from '../types'; -import { ConsoleLogger as Logger } from '@aws-amplify/core'; +import { ConsoleLogger as Logger, JS } from '@aws-amplify/core'; const logger = new Logger('EventTracker'); @@ -30,7 +30,7 @@ export default class EventTracker { private _delegates; constructor(tracker, opts) { - if (!window || !window.addEventListener) { + if (!JS.browserOrNode().isBrowser || !window.addEventListener) { logger.debug('not in the supported web environment'); return; } diff --git a/packages/analytics/src/trackers/PageViewTracker.ts b/packages/analytics/src/trackers/PageViewTracker.ts index 402e0fc1688..fe24c2310d4 100644 --- a/packages/analytics/src/trackers/PageViewTracker.ts +++ b/packages/analytics/src/trackers/PageViewTracker.ts @@ -13,13 +13,14 @@ import { pageViewTrackOpts } from '../types'; import MethodEmbed from '../utils/MethodEmbed'; -import { ConsoleLogger as Logger } from '@aws-amplify/core'; +import { ConsoleLogger as Logger, JS } from '@aws-amplify/core'; const logger = new Logger('PageViewTracker'); const PREV_URL_KEY = 'aws-amplify-analytics-prevUrl'; const getUrl = () => { - return window.location.origin + window.location.pathname; + if (!JS.browserOrNode().isBrowser) return ''; + else return window.location.origin + window.location.pathname; }; const defaultOpts: pageViewTrackOpts = { @@ -72,7 +73,7 @@ export default class PageViewTracker { } private async _pageViewTrackDefault() { - if (!window || !window.addEventListener || !window.sessionStorage) { + if (!JS.browserOrNode().isBrowser || !window.addEventListener || !window.sessionStorage) { logger.debug('not in the supported web enviroment'); return; } @@ -101,7 +102,7 @@ export default class PageViewTracker { } private async _trackFunc() { - if (!window || !window.addEventListener || !history.pushState || !window.sessionStorage) { + if (!JS.browserOrNode().isBrowser || !window.addEventListener || !history.pushState || !window.sessionStorage) { logger.debug('not in the supported web enviroment'); return; } @@ -132,7 +133,7 @@ export default class PageViewTracker { private _pageViewTrackSPA() { - if (!window || !window.addEventListener || !history.pushState) { + if (!JS.browserOrNode().isBrowser || !window.addEventListener || !history.pushState) { logger.debug('not in the supported web enviroment'); return; } diff --git a/packages/analytics/src/trackers/SessionTracker.ts b/packages/analytics/src/trackers/SessionTracker.ts index c6d2e1fa425..f0c67f80edc 100644 --- a/packages/analytics/src/trackers/SessionTracker.ts +++ b/packages/analytics/src/trackers/SessionTracker.ts @@ -13,7 +13,7 @@ // the session tracker for web -import { ConsoleLogger as Logger, Hub } from '@aws-amplify/core'; +import { ConsoleLogger as Logger, Hub, JS } from '@aws-amplify/core'; import { SessionTrackOpts } from '../types'; const logger = new Logger('SessionTracker'); @@ -45,6 +45,10 @@ export default class SessionTracker { } private _envCheck() { + if (!JS.browserOrNode().isBrowser) { + return false; + } + if (!document || !document.addEventListener) { logger.debug('not in the supported web environment'); return false; diff --git a/packages/analytics/src/vendor/dom-utils/matches.ts b/packages/analytics/src/vendor/dom-utils/matches.ts index da47087cc00..924feb952c2 100644 --- a/packages/analytics/src/vendor/dom-utils/matches.ts +++ b/packages/analytics/src/vendor/dom-utils/matches.ts @@ -2,7 +2,9 @@ * Copyright (c) 2017, Philip Walton */ -const proto = window && window['Element']? window['Element'].prototype: null; +import { JS } from '@aws-amplify/core'; + +const proto = JS.browserOrNode().isBrowser && window['Element']? window['Element'].prototype: null; const nativeMatches = proto ? proto.matches || proto.matchesSelector || proto.webkitMatchesSelector || diff --git a/packages/analytics/src/vendor/dom-utils/parse-url.ts b/packages/analytics/src/vendor/dom-utils/parse-url.ts index 03702d18119..65e54230205 100644 --- a/packages/analytics/src/vendor/dom-utils/parse-url.ts +++ b/packages/analytics/src/vendor/dom-utils/parse-url.ts @@ -2,11 +2,13 @@ * Copyright (c) 2017, Philip Walton */ +import { JS } from '@aws-amplify/core'; + const HTTP_PORT = '80'; const HTTPS_PORT = '443'; const DEFAULT_PORT = RegExp(':(' + HTTP_PORT + '|' + HTTPS_PORT + ')$'); -const a = document? document.createElement('a') : null; +const a = JS.browserOrNode().isBrowser ? document.createElement('a') : null; const cache = {}; /** diff --git a/packages/auth/src/Auth.ts b/packages/auth/src/Auth.ts index fce8617b323..65a51cdf3d7 100644 --- a/packages/auth/src/Auth.ts +++ b/packages/auth/src/Auth.ts @@ -187,7 +187,7 @@ export default class AuthClass { logger.debug('user already logged in'); }).catch(e => { logger.debug('not logged in, try to parse the url'); - if (!window || !window.location) { + if (!JS.browserOrNode().isBrowser || !window.location) { logger.debug('not in the browser'); return; } diff --git a/packages/core/src/OAuthHelper/FacebookOAuth.ts b/packages/core/src/OAuthHelper/FacebookOAuth.ts index 941f30bfc7e..3970ada638c 100644 --- a/packages/core/src/OAuthHelper/FacebookOAuth.ts +++ b/packages/core/src/OAuthHelper/FacebookOAuth.ts @@ -57,7 +57,7 @@ export default class FacebookOAuth { private _refreshFacebookTokenImpl() { let fb = null; - if (window) fb = window['FB']; + if (JS.browserOrNode().isBrowser) fb = window['FB']; if (!fb) { logger.debug('no fb sdk available'); return Promise.reject('no fb sdk available'); diff --git a/packages/core/src/OAuthHelper/GoogleOAuth.ts b/packages/core/src/OAuthHelper/GoogleOAuth.ts index fa0b69ed481..682513b043d 100644 --- a/packages/core/src/OAuthHelper/GoogleOAuth.ts +++ b/packages/core/src/OAuthHelper/GoogleOAuth.ts @@ -58,7 +58,7 @@ export default class GoogleOAuth { private _refreshGoogleTokenImpl() { let ga = null; - if (window) ga = window['gapi'] && window['gapi'].auth2 ? window['gapi'].auth2 : null; + if (JS.browserOrNode().isBrowser) ga = window['gapi'] && window['gapi'].auth2 ? window['gapi'].auth2 : null; if (!ga) { logger.debug('no gapi auth2 available'); return Promise.reject('no gapi auth2 available'); diff --git a/packages/core/src/ServiceWorker/ServiceWorker.ts b/packages/core/src/ServiceWorker/ServiceWorker.ts index 1025c0daafe..ea10963d012 100644 --- a/packages/core/src/ServiceWorker/ServiceWorker.ts +++ b/packages/core/src/ServiceWorker/ServiceWorker.ts @@ -11,6 +11,7 @@ * and limitations under the License. */ import { ConsoleLogger as Logger } from '../Logger'; +import JS from '../JS'; import Amplify from '../Amplify'; /** * Provides a means to registering a service worker in the browser @@ -107,7 +108,7 @@ class ServiceWorkerClass { if (!this._registration) throw new Error('Service Worker not registered'); this._publicKey = publicKey; return new Promise((resolve, reject) => { - if (window) { + if (JS.browserOrNode().isBrowser) { this._registration.pushManager.getSubscription() .then((subscription) => { if (subscription) {