Skip to content

Commit

Permalink
Exit early for unsupported browsers
Browse files Browse the repository at this point in the history
Removes the Promise polyfill for IE11 and instead exits the SDK early if
the browser environment is not one known to support push. This is
something I think we should have been doing from the beginning, instead
of checking whether push notifications were supported inside individual
methods.
  • Loading branch information
Jason Pang committed Jan 26, 2017
1 parent 373e2b2 commit d790181
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 54 deletions.
4 changes: 3 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var oneSignalProjectRoot = HOME_PATH + "/code/OneSignal";
var oneSignalSdksDirName = 'sdks';
var publicAssetsTargetPath = oneSignalProjectRoot + "/public/";
var sdksTargetPath = publicAssetsTargetPath + oneSignalSdksDirName;
var IS_ES5 = process.argv.indexOf('--es5') >= 0;
var IS_ES6 = process.argv.indexOf('--es6') >= 0;
var IS_PROD = process.argv.indexOf('--prod') >= 0 || process.argv.indexOf('--production') >= 0 || false;
var IS_STAGING = process.argv.indexOf('--staging') >= 0 || false;
Expand Down Expand Up @@ -51,7 +52,8 @@ gulp.task("transpile-javascript", shell.task([
(IS_TEST ? '--test' : '') + ' ' +
(IS_STAGING ? '--staging' : '') + ' ' +
(SIZE_STATS ? '--stats' : '') +
(IS_ES6 ? '--es6' : '')
(IS_ES6 ? '--es6' : '') +
(IS_ES5 ? '--es5' : '')
]));

function copyFile(prefix) {
Expand Down
13 changes: 2 additions & 11 deletions src/OneSignal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as Cookie from 'js-cookie';
import Database from './Database';
import * as Browser from 'bowser';
import {
isPushNotificationsSupported, logMethodCall, isValidEmail, awaitOneSignalInitAndSupported, getConsoleStyle,
logMethodCall, isValidEmail, awaitOneSignalInitAndSupported, getConsoleStyle,
contains, unsubscribeFromPush, decodeHtmlEntities, getUrlQueryParam, executeAndTimeoutPromiseAfter,
wipeLocalIndexedDb, prepareEmailForHashing, executeCallback, once, md5, sha1, awaitSdkEvent
} from './utils';
Expand Down Expand Up @@ -106,7 +106,7 @@ export default class OneSignal {
*/
static isPushNotificationsSupported() {
logMethodCall('isPushNotificationsSupported');
return isPushNotificationsSupported();
return true;
}

/**
Expand All @@ -128,11 +128,6 @@ export default class OneSignal {
path: '/'
}, options);

if (!isPushNotificationsSupported()) {
log.warn('OneSignal: Push notifications are not supported.');
return;
}

if (Browser.safari && !OneSignal.config.safari_web_id) {
log.warn("OneSignal: Required parameter %csafari_web_id", getConsoleStyle('code'), 'was not passed to OneSignal.init(), skipping SDK initialization.');
return;
Expand Down Expand Up @@ -310,10 +305,6 @@ export default class OneSignal {
* @PublicApi
*/
static registerForPushNotifications(options) {
if (!isPushNotificationsSupported()) {
log.debug('OneSignal: Push notifications are not supported.');
}

// WARNING: Do NOT add callbacks that have to fire to get from here to window.open in _sessionInit.
// Otherwise the pop-up to ask for push permission on HTTP connections will be blocked by Chrome.
function __registerForPushNotifications() {
Expand Down
5 changes: 1 addition & 4 deletions src/bell/Bell.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
isPushNotificationsSupported, removeDomElement, addDomElement, addCssClass, delay, nothing, contains,
removeDomElement, addDomElement, addCssClass, delay, nothing, contains,
decodeHtmlEntities, once
} from '../utils';
import * as log from 'loglevel';
Expand Down Expand Up @@ -361,9 +361,6 @@ export default class Bell {
}

create() {
if (!isPushNotificationsSupported())
return;

if (!this.options.enable)
return;

Expand Down
39 changes: 20 additions & 19 deletions src/entry.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
require('es6-promise').polyfill();

import Environment from './Environment';
import { getSdkLoadCount, incrementSdkLoadCount } from './utils';
import { getSdkLoadCount, incrementSdkLoadCount, isPushNotificationsSupported } from './utils';
import * as log from 'loglevel';


if (Environment.isBrowser()) {
incrementSdkLoadCount();
if (getSdkLoadCount() > 1) {
log.warn(`OneSignal: The web push SDK is included more than once. For optimal performance, please include our ` +
`SDK only once on your page.`);
log.debug(`OneSignal: Exiting from SDK initialization to prevent double-initialization errors. ` +
`Occurred ${getSdkLoadCount()} times.`);
} else {
// We're running in the host page, iFrame of the host page, or popup window
// Load OneSignal's web SDK
if (typeof OneSignal !== "undefined")
var predefinedOneSignalPushes = OneSignal;
if (isPushNotificationsSupported()) {
incrementSdkLoadCount();
if (getSdkLoadCount() > 1) {
log.warn(`OneSignal: The web push SDK is included more than once. For optimal performance, please include our ` +
`SDK only once on your page.`);
log.debug(`OneSignal: Exiting from SDK initialization to prevent double-initialization errors. ` +
`Occurred ${getSdkLoadCount()} times.`);
} else {
// We're running in the host page, iFrame of the host page, or popup window
// Load OneSignal's web SDK
if (typeof OneSignal !== "undefined")
var predefinedOneSignalPushes = OneSignal;

require("expose?OneSignal!./OneSignal.ts");
require("expose?OneSignal!./OneSignal.ts");

if (predefinedOneSignalPushes)
for (var i = 0; i < predefinedOneSignalPushes.length; i++)
OneSignal.push(predefinedOneSignalPushes[i]);
if (predefinedOneSignalPushes)
for (var i = 0; i < predefinedOneSignalPushes.length; i++)
OneSignal.push(predefinedOneSignalPushes[i]);
}
} else {
log.debug('OneSignal: Push notifications are not supported. The SDK will not be initialized.');
}
}
else if (Environment.isServiceWorker()) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/EventHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Database from '../Database';
import * as Browser from 'bowser';
import {
getConsoleStyle, contains, normalizeSubdomain, getDeviceTypeForBrowser, capitalize,
isPushNotificationsSupported, getUrlQueryParam, executeAndTimeoutPromiseAfter, wipeLocalIndexedDb,
getUrlQueryParam, executeAndTimeoutPromiseAfter, wipeLocalIndexedDb,
unsubscribeFromPush, decodeHtmlEntities, logMethodCall
} from '../utils';
import MainHelper from "./MainHelper";
Expand Down
7 changes: 1 addition & 6 deletions src/helpers/HttpHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Database from '../Database';
import * as Browser from 'bowser';
import {
getConsoleStyle, contains, normalizeSubdomain, getDeviceTypeForBrowser, capitalize,
isPushNotificationsSupported, getUrlQueryParam, executeAndTimeoutPromiseAfter, wipeLocalIndexedDb, unsubscribeFromPush
getUrlQueryParam, executeAndTimeoutPromiseAfter, wipeLocalIndexedDb, unsubscribeFromPush
} from '../utils';
import * as objectAssign from 'object-assign';
import * as EventEmitter from 'wolfy87-eventemitter';
Expand Down Expand Up @@ -47,11 +47,6 @@ export default class HttpHelper {
static initHttp(options) {
log.debug(`Called %cinitHttp(${JSON.stringify(options, null, 4)})`, getConsoleStyle('code'));

if (!isPushNotificationsSupported()) {
log.warn('OneSignal: Push notifications are not supported.');
return;
}

ServiceWorkerHelper.applyServiceWorkerEnvPrefixes();

var creator = window.opener || window.parent;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/InitHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Database from '../Database';
import * as Browser from 'bowser';
import {
getConsoleStyle, contains, normalizeSubdomain, getDeviceTypeForBrowser, capitalize, once,
isPushNotificationsSupported, getUrlQueryParam, wipeLocalIndexedDb, unsubscribeFromPush
getUrlQueryParam, wipeLocalIndexedDb, unsubscribeFromPush
} from '../utils';
import * as objectAssign from 'object-assign';
import * as EventEmitter from 'wolfy87-eventemitter';
Expand Down
6 changes: 1 addition & 5 deletions src/helpers/ServiceWorkerHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import Event from "../Event";
import Database from '../Database';
import * as Browser from 'bowser';
import {
getConsoleStyle, contains, normalizeSubdomain, getDeviceTypeForBrowser, capitalize,
isPushNotificationsSupported
getConsoleStyle, contains, normalizeSubdomain, getDeviceTypeForBrowser, capitalize
} from '../utils';
import * as objectAssign from 'object-assign';
import * as EventEmitter from 'wolfy87-eventemitter';
Expand Down Expand Up @@ -152,9 +151,6 @@ export default class ServiceWorkerHelper {
}

static isServiceWorkerActive(callback?) {
if (!isPushNotificationsSupported()) {
return;
}
if (!('serviceWorker' in navigator)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/service-worker/ServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Environment from '../Environment'
import OneSignalApi from '../OneSignalApi';
import * as log from 'loglevel';
import Database from '../Database';
import { isPushNotificationsSupported, getConsoleStyle, contains, trimUndefined, getDeviceTypeForBrowser, substringAfter, isValidUuid, capitalize } from '../utils';
import { getConsoleStyle, contains, trimUndefined, getDeviceTypeForBrowser, substringAfter, isValidUuid, capitalize } from '../utils';
import * as objectAssign from 'object-assign';
import * as swivel from 'swivel';
import * as Browser from 'bowser';
Expand Down
4 changes: 0 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ export function removeDomElement(selector) {
*/
export function awaitOneSignalInitAndSupported() {
return new Promise((resolve, reject) => {
if (!isPushNotificationsSupported()) {
throw new PushNotSupportedError();
}

if (!OneSignal.initialized) {
OneSignal.once(OneSignal.EVENTS.SDK_INITIALIZED, resolve);
} else {
Expand Down
3 changes: 2 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlug

var IS_PROD = process.argv.indexOf('--prod') >= 0 || process.argv.indexOf('--production') >= 0;
var IS_ES6 = process.argv.indexOf('--es6') >= 0;
var IS_ES5 = process.argv.indexOf('--es5') >= 0;
var IS_STAGING = process.argv.indexOf('--staging') >= 0;
var IS_TEST = process.argv.indexOf('--test') >= 0;
var IS_DEV = !IS_PROD && !IS_STAGING;
Expand Down Expand Up @@ -143,7 +144,7 @@ if (SIZE_STATS) {
}

function getAwesomeTypescriptLoaderTsconfigPath() {
if (IS_DEV || IS_ES6) {
if ((IS_DEV || IS_ES6) && !IS_ES5) {
return '?tsconfig=./tsconfig.es6.json';
} else {
return '?tsconfig=./tsconfig.es5.json';
Expand Down

0 comments on commit d790181

Please sign in to comment.