Skip to content

Commit

Permalink
Merge pull request #1818 from powerful23/fix-issue-1811
Browse files Browse the repository at this point in the history
use JS.browserOrNode().isBrowser to check window object
  • Loading branch information
powerful23 authored Oct 2, 2018
2 parents 5a1caca + c415cf8 commit ebb9c9e
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/analytics/src/trackers/EventTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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;
}
Expand Down
11 changes: 6 additions & 5 deletions packages/analytics/src/trackers/PageViewTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 5 additions & 1 deletion packages/analytics/src/trackers/SessionTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion packages/analytics/src/vendor/dom-utils/matches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* Copyright (c) 2017, Philip Walton <philip@philipwalton.com>
*/

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 ||
Expand Down
4 changes: 3 additions & 1 deletion packages/analytics/src/vendor/dom-utils/parse-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* Copyright (c) 2017, Philip Walton <philip@philipwalton.com>
*/

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 = {};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/OAuthHelper/FacebookOAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/OAuthHelper/GoogleOAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/ServiceWorker/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ebb9c9e

Please sign in to comment.