From d10777a33b42cf79e253cee69e21f5a02dc539fe Mon Sep 17 00:00:00 2001 From: Marat Dyatko <31615495+dyatko@users.noreply.github.com> Date: Wed, 1 Aug 2018 10:41:51 +0200 Subject: [PATCH] fix(network): bind listener to document instead of window (#2622) We noticed that in some cases network state listeners are stopped triggering, but `navigator.onLine` still reflects the state correctly. I used the following code to debug and found out an issue with `window` and `document` difference: ```js window.addEventListener("online", ()=>console.warn('WINDOW ONLINE'), false); // won't be triggered window.addEventListener("online", ()=>console.warn('WINDOW ONLINE'), false); // won't be triggered document.addEventListener("online", ()=>console.warn('DOCUMENT ONLINE'), false); // triggered document.addEventListener("online", ()=>console.warn('DOCUMENT ONLINE'), false); // triggered ``` Also, according to plugin documentation, listeners should be bound to `document`: https://github.com/apache/cordova-plugin-network-information#offline --- src/@ionic-native/plugins/network/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/@ionic-native/plugins/network/index.ts b/src/@ionic-native/plugins/network/index.ts index ef162d10..d196168e 100644 --- a/src/@ionic-native/plugins/network/index.ts +++ b/src/@ionic-native/plugins/network/index.ts @@ -89,7 +89,8 @@ export class Network extends IonicNativePlugin { */ @Cordova({ eventObservable: true, - event: 'offline' + event: 'offline', + element: document }) onDisconnect(): Observable { return; @@ -101,7 +102,8 @@ export class Network extends IonicNativePlugin { */ @Cordova({ eventObservable: true, - event: 'online' + event: 'online', + element: document }) onConnect(): Observable { return;