From e3e8c85087cc509e2bb30536fe80dcbef82c5e6b Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Sat, 23 Feb 2019 11:34:16 -0600 Subject: [PATCH] fix(ssr): check for window and document --- src/@ionic-native/core/bootstrap.ts | 39 +++++++++++---------- src/@ionic-native/core/decorators/common.ts | 5 +-- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/src/@ionic-native/core/bootstrap.ts b/src/@ionic-native/core/bootstrap.ts index 4fad3cd6..3c748361 100644 --- a/src/@ionic-native/core/bootstrap.ts +++ b/src/@ionic-native/core/bootstrap.ts @@ -1,23 +1,26 @@ export function checkReady() { - const DEVICE_READY_TIMEOUT = 5000; + if (typeof process === 'undefined') { + const win: any = typeof window !== 'undefined' ? window : {}; + const DEVICE_READY_TIMEOUT = 5000; - // To help developers using cordova, we listen for the device ready event and - // log an error if it didn't fire in a reasonable amount of time. Generally, - // when this happens, developers should remove and reinstall plugins, since - // an inconsistent plugin is often the culprit. - const before = Date.now(); + // To help developers using cordova, we listen for the device ready event and + // log an error if it didn't fire in a reasonable amount of time. Generally, + // when this happens, developers should remove and reinstall plugins, since + // an inconsistent plugin is often the culprit. + const before = Date.now(); - let didFireReady = false; - document.addEventListener('deviceready', () => { - console.log(`Ionic Native: deviceready event fired after ${Date.now() - before} ms`); - didFireReady = true; - }); + let didFireReady = false; + win.document.addEventListener('deviceready', () => { + console.log(`Ionic Native: deviceready event fired after ${Date.now() - before} ms`); + didFireReady = true; + }); - setTimeout(() => { - if (!didFireReady && window.cordova) { - console.warn( - `Ionic Native: deviceready did not fire within ${DEVICE_READY_TIMEOUT}ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.` - ); - } - }, DEVICE_READY_TIMEOUT); + setTimeout(() => { + if (!didFireReady && win.cordova) { + console.warn( + `Ionic Native: deviceready did not fire within ${DEVICE_READY_TIMEOUT}ms. This can happen when plugins are in an inconsistent state. Try removing plugins from plugins/ and reinstalling them.` + ); + } + }, DEVICE_READY_TIMEOUT); + } } diff --git a/src/@ionic-native/core/decorators/common.ts b/src/@ionic-native/core/decorators/common.ts index 7dc831e6..1d1628c7 100644 --- a/src/@ionic-native/core/decorators/common.ts +++ b/src/@ionic-native/core/decorators/common.ts @@ -20,9 +20,10 @@ export function getPromise(callback: (resolve: Function, reject?: Function) = } }; - if (window.angular) { + if (typeof window !== 'undefined' && window.angular) { + const doc = window.document; const injector = window.angular - .element(document.querySelector('[ng-app]') || document.body) + .element(doc.querySelector('[ng-app]') || doc.body) .injector(); if (injector) { const $q = injector.get('$q');