-
Notifications
You must be signed in to change notification settings - Fork 115
/
OneSignalShimLoader.ts
73 lines (63 loc) · 2.29 KB
/
OneSignalShimLoader.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {
isIosSafari,
isPushNotificationsSupported,
} from './BrowserSupportsPush';
// NOTE: Careful if adding imports, ES5 targets can't clean up functions never called.
// See sdk.ts for what entry points this handles
export class OneSignalShimLoader {
private static VERSION =
typeof __VERSION__ === 'undefined' ? 1 : Number(__VERSION__);
private static addScriptToPage(url: string): void {
const scriptElement = document.createElement('script');
scriptElement.src = url;
// Using defer over async; async timing is inconsistent and may interrupt DOM rendering
scriptElement.defer = true;
document.head.appendChild(scriptElement);
}
// Same logic from SdkEnvironment
private static getPathAndPrefix(): string {
const buildOrigin =
typeof __BUILD_ORIGIN__ !== 'undefined'
? __BUILD_ORIGIN__ || 'localhost'
: 'localhost';
const productionOrigin = 'https://cdn.onesignal.com/sdks/web/v16/';
if (typeof __BUILD_TYPE__ === 'undefined') {
return productionOrigin;
}
const isHttps = typeof __IS_HTTPS__ !== 'undefined' ? __IS_HTTPS__ : true;
const protocol = isHttps ? 'https' : 'http';
const port = isHttps ? 4001 : 4000;
switch (__BUILD_TYPE__) {
case 'development':
return __NO_DEV_PORT__
? `${protocol}://${buildOrigin}/sdks/web/v16/Dev-`
: `${protocol}://${buildOrigin}:${port}/sdks/web/v16/Dev-`;
case 'staging':
return `https://${buildOrigin}/sdks/web/v16/Staging-`;
default:
return productionOrigin;
}
}
private static loadFullPageSDK(): void {
OneSignalShimLoader.addScriptToPage(
`${OneSignalShimLoader.getPathAndPrefix()}OneSignalSDK.page.es6.js?v=${
OneSignalShimLoader.VERSION
}`,
);
}
public static start(): void {
if (isPushNotificationsSupported()) {
OneSignalShimLoader.loadFullPageSDK();
} else {
this.printEnvironmentNotSupported();
}
}
private static printEnvironmentNotSupported() {
let logMessage = 'OneSignal: SDK is not compatible with this browser.';
if (isIosSafari()) {
logMessage +=
' To support iOS please install as a Web App. See the OneSignal guide https://documentation.onesignal.com/docs/safari-web-push-for-ios';
}
console.log(logMessage);
}
}