From 5b1bdc37e049b34cb5b64ec2426a14232336d53d Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Sat, 23 Feb 2019 16:54:09 -0600 Subject: [PATCH] fix(ssr): fix window references --- src/@ionic-native/core/decorators/common.ts | 2 +- src/@ionic-native/core/ng1.ts | 2 +- src/@ionic-native/core/util.ts | 2 +- src/@ionic-native/plugins/geofence/index.ts | 2 +- src/@ionic-native/plugins/in-app-browser/index.ts | 4 +++- src/@ionic-native/plugins/push/index.ts | 4 +++- src/@ionic-native/plugins/themeable-browser/index.ts | 4 +++- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/@ionic-native/core/decorators/common.ts b/src/@ionic-native/core/decorators/common.ts index 1d1628c7..30fed6e7 100644 --- a/src/@ionic-native/core/decorators/common.ts +++ b/src/@ionic-native/core/decorators/common.ts @@ -186,7 +186,7 @@ export function checkAvailability( pluginInstance = getPlugin(pluginRef); if (!pluginInstance || (!!methodName && typeof pluginInstance[methodName] === 'undefined')) { - if (!window.cordova) { + if (typeof window === 'undefined' || !window.cordova) { cordovaWarn(pluginName, methodName); return ERR_CORDOVA_NOT_AVAILABLE; } diff --git a/src/@ionic-native/core/ng1.ts b/src/@ionic-native/core/ng1.ts index 9c11b3e0..5fb7993d 100644 --- a/src/@ionic-native/core/ng1.ts +++ b/src/@ionic-native/core/ng1.ts @@ -6,7 +6,7 @@ declare const window: any; * creates Angular 1 services of the form $cordovaSERVICE, ex: $cordovaStatusBar. */ export function initAngular1(plugins: any) { - if (window.angular) { + if (typeof window !== 'undefined' && window.angular) { const ngModule = window.angular.module('ionic.native', []); for (const name in plugins) { diff --git a/src/@ionic-native/core/util.ts b/src/@ionic-native/core/util.ts index e4eb2595..f734562e 100644 --- a/src/@ionic-native/core/util.ts +++ b/src/@ionic-native/core/util.ts @@ -20,7 +20,7 @@ export function get(element: Element | Window, path: string) { */ export function getPromise(callback: Function = () => {}): Promise { const tryNativePromise = () => { - if (window.Promise) { + if (typeof Promise === 'function' || (typeof window !== 'undefined' && window.Promise)) { return new Promise((resolve, reject) => { callback(resolve, reject); }); diff --git a/src/@ionic-native/plugins/geofence/index.ts b/src/@ionic-native/plugins/geofence/index.ts index ca1f4434..dcefb3f1 100644 --- a/src/@ionic-native/plugins/geofence/index.ts +++ b/src/@ionic-native/plugins/geofence/index.ts @@ -147,7 +147,7 @@ export class Geofence extends IonicNativePlugin { */ onNotificationClicked(): Observable { return new Observable(observer => { - window && + typeof window !== 'undefined' && window.geofence && (window.geofence.onNotificationClicked = observer.next.bind(observer)); return () => (window.geofence.onNotificationClicked = () => {}); diff --git a/src/@ionic-native/plugins/in-app-browser/index.ts b/src/@ionic-native/plugins/in-app-browser/index.ts index 968419c0..c12e4aae 100644 --- a/src/@ionic-native/plugins/in-app-browser/index.ts +++ b/src/@ionic-native/plugins/in-app-browser/index.ts @@ -129,7 +129,9 @@ export class InAppBrowserObject { this._objectInstance = cordova.InAppBrowser.open(url, target, options); } catch (e) { - window.open(url, target); + if (typeof window !== 'undefined') { + window.open(url, target); + } console.warn( 'Native: InAppBrowser is not installed or you are running on a browser. Falling back to window.open.' ); diff --git a/src/@ionic-native/plugins/push/index.ts b/src/@ionic-native/plugins/push/index.ts index 8cca6291..806fd502 100644 --- a/src/@ionic-native/plugins/push/index.ts +++ b/src/@ionic-native/plugins/push/index.ts @@ -391,7 +391,9 @@ export class PushObject { if ( checkAvailability('PushNotification', 'init', 'PushNotification') === true ) { - this._objectInstance = window.PushNotification.init(options); + if (typeof window !== 'undefined') { + this._objectInstance = window.PushNotification.init(options); + } } } diff --git a/src/@ionic-native/plugins/themeable-browser/index.ts b/src/@ionic-native/plugins/themeable-browser/index.ts index 2388cc84..8c43986b 100644 --- a/src/@ionic-native/plugins/themeable-browser/index.ts +++ b/src/@ionic-native/plugins/themeable-browser/index.ts @@ -85,7 +85,9 @@ export class ThemeableBrowserObject { styleOptions ); } catch (e) { - window.open(url); + if (typeof window !== 'undefined') { + window.open(url); + } console.warn( 'Native: ThemeableBrowser is not installed or you are running on a browser. Falling back to window.open.' );