From e61d85d123174170f5098cbecb63de97649015f0 Mon Sep 17 00:00:00 2001 From: bombillazo Date: Mon, 28 Nov 2022 09:11:59 -0400 Subject: [PATCH 1/9] check if window object exists in web before trying to avoid accessing properties of undefined --- src/internal/nativeModule.web.ts | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index 3eaf0120..92f57a9f 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -7,7 +7,7 @@ * @format */ -import { + import { NetInfoNativeModule, DEVICE_CONNECTIVITY_EVENT, NetInfoNativeModuleState, @@ -71,11 +71,14 @@ declare global { } } -// Check if the browser supports the connection API -const connection = - window.navigator.connection || - window.navigator.mozConnection || - window.navigator.webkitConnection; +const isWindowPresent = typeof window !== 'undefined'; + +// Check if window exists and if the browser supports the connection API +const connection = isWindowPresent + ? window?.navigator.connection || + window?.navigator.mozConnection || + window?.navigator.webkitConnection + : undefined; // Map browser types to native types const typeMapping: Record = { @@ -249,8 +252,10 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.addEventListener('change', nativeHandler); } else { - window.addEventListener('online', nativeHandler, false); - window.addEventListener('offline', nativeHandler, false); + if (isWindowPresent) { + window.addEventListener('online', nativeHandler, false); + window.addEventListener('offline', nativeHandler, false); + } } // Remember handlers @@ -272,8 +277,10 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.removeEventListener('change', nativeHandler); } else { - window.removeEventListener('online', nativeHandler); - window.removeEventListener('offline', nativeHandler); + if (isWindowPresent) { + window.removeEventListener('online', nativeHandler); + window.removeEventListener('offline', nativeHandler); + } } // Remove handlers From b02409bf76c86a286356d422ac1be43512ea28c8 Mon Sep 17 00:00:00 2001 From: bombillazo Date: Mon, 28 Nov 2022 09:12:52 -0400 Subject: [PATCH 2/9] rename boolean value to hasWindow --- src/internal/nativeModule.web.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index 92f57a9f..fac1e3f7 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -71,10 +71,10 @@ declare global { } } -const isWindowPresent = typeof window !== 'undefined'; +const hasWindow = typeof window !== 'undefined'; // Check if window exists and if the browser supports the connection API -const connection = isWindowPresent +const connection = hasWindow ? window?.navigator.connection || window?.navigator.mozConnection || window?.navigator.webkitConnection @@ -252,7 +252,7 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.addEventListener('change', nativeHandler); } else { - if (isWindowPresent) { + if (hasWindow) { window.addEventListener('online', nativeHandler, false); window.addEventListener('offline', nativeHandler, false); } @@ -277,7 +277,7 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.removeEventListener('change', nativeHandler); } else { - if (isWindowPresent) { + if (hasWindow) { window.removeEventListener('online', nativeHandler); window.removeEventListener('offline', nativeHandler); } From 1ac2d25a021cde0ac170c0d905aff570437eb637 Mon Sep 17 00:00:00 2001 From: Hector Ayala Date: Mon, 28 Nov 2022 09:57:50 -0400 Subject: [PATCH 3/9] Update src/internal/nativeModule.web.ts Co-authored-by: Mike Hardy --- src/internal/nativeModule.web.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index fac1e3f7..172693f7 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -71,8 +71,6 @@ declare global { } } -const hasWindow = typeof window !== 'undefined'; - // Check if window exists and if the browser supports the connection API const connection = hasWindow ? window?.navigator.connection || From 9710268cba5c9203a651bb24019d0739fc46cc16 Mon Sep 17 00:00:00 2001 From: Hector Ayala Date: Mon, 28 Nov 2022 09:57:55 -0400 Subject: [PATCH 4/9] Update src/internal/nativeModule.web.ts Co-authored-by: Mike Hardy --- src/internal/nativeModule.web.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index 172693f7..2f2ab802 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -7,7 +7,7 @@ * @format */ - import { +import { NetInfoNativeModule, DEVICE_CONNECTIVITY_EVENT, NetInfoNativeModuleState, From 2beed2937fba86eea38cc623bc846f33cf06c306 Mon Sep 17 00:00:00 2001 From: Hector Ayala Date: Mon, 28 Nov 2022 09:58:00 -0400 Subject: [PATCH 5/9] Update src/internal/nativeModule.web.ts Co-authored-by: Mike Hardy --- src/internal/nativeModule.web.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index 2f2ab802..85e1aa9f 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -72,11 +72,10 @@ declare global { } // Check if window exists and if the browser supports the connection API -const connection = hasWindow - ? window?.navigator.connection || - window?.navigator.mozConnection || - window?.navigator.webkitConnection - : undefined; +const connection = + window?.navigator.connection || + window?.navigator.mozConnection || + window?.navigator.webkitConnection; // Map browser types to native types const typeMapping: Record = { From 89653a33839ec3768267b123d0ff60b36685f5aa Mon Sep 17 00:00:00 2001 From: Hector Ayala Date: Mon, 28 Nov 2022 09:58:07 -0400 Subject: [PATCH 6/9] Update src/internal/nativeModule.web.ts Co-authored-by: Mike Hardy --- src/internal/nativeModule.web.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index 85e1aa9f..c4475af6 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -249,10 +249,8 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.addEventListener('change', nativeHandler); } else { - if (hasWindow) { - window.addEventListener('online', nativeHandler, false); - window.addEventListener('offline', nativeHandler, false); - } + window?.addEventListener('online', nativeHandler, false); + window?.addEventListener('offline', nativeHandler, false); } // Remember handlers From 55acb66c4d01cd417a9b14fc1c75067253b31caf Mon Sep 17 00:00:00 2001 From: Hector Ayala Date: Mon, 28 Nov 2022 09:58:12 -0400 Subject: [PATCH 7/9] Update src/internal/nativeModule.web.ts Co-authored-by: Mike Hardy --- src/internal/nativeModule.web.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index c4475af6..ce2bb0c9 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -272,10 +272,8 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.removeEventListener('change', nativeHandler); } else { - if (hasWindow) { - window.removeEventListener('online', nativeHandler); - window.removeEventListener('offline', nativeHandler); - } + window?.removeEventListener('online', nativeHandler); + window?.removeEventListener('offline', nativeHandler); } // Remove handlers From e0ab775a36e552f8d7298de9434ddac2bc34ee13 Mon Sep 17 00:00:00 2001 From: bombillazo Date: Mon, 28 Nov 2022 10:04:51 -0400 Subject: [PATCH 8/9] revert to explicitly check if window is not undefined --- src/internal/nativeModule.web.ts | 36 +++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index ce2bb0c9..c606d157 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -8,22 +8,22 @@ */ import { - NetInfoNativeModule, DEVICE_CONNECTIVITY_EVENT, + NetInfoNativeModule, NetInfoNativeModuleState, } from './privateTypes'; import { + NetInfoBluetoothState, + NetInfoCellularGeneration, + NetInfoCellularState, + NetInfoEthernetState, + NetInfoNoConnectionState, + NetInfoOtherState, NetInfoState, NetInfoStateType, NetInfoUnknownState, - NetInfoNoConnectionState, - NetInfoCellularState, - NetInfoBluetoothState, - NetInfoEthernetState, NetInfoWifiState, NetInfoWimaxState, - NetInfoOtherState, - NetInfoCellularGeneration, } from './types'; // See https://wicg.github.io/netinfo/#dom-connectiontype @@ -70,12 +70,14 @@ declare global { webkitConnection?: Connection; } } +const isWindowPresent = typeof window !== 'undefined'; // Check if window exists and if the browser supports the connection API -const connection = - window?.navigator.connection || - window?.navigator.mozConnection || - window?.navigator.webkitConnection; +const connection = isWindowPresent + ? window.navigator.connection || + window.navigator.mozConnection || + window.navigator.webkitConnection + : undefined; // Map browser types to native types const typeMapping: Record = { @@ -249,8 +251,10 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.addEventListener('change', nativeHandler); } else { - window?.addEventListener('online', nativeHandler, false); - window?.addEventListener('offline', nativeHandler, false); + if (isWindowPresent) { + window.addEventListener('online', nativeHandler, false); + window.addEventListener('offline', nativeHandler, false); + } } // Remember handlers @@ -272,8 +276,10 @@ const RNCNetInfo: NetInfoNativeModule = { if (connection) { connection.removeEventListener('change', nativeHandler); } else { - window?.removeEventListener('online', nativeHandler); - window?.removeEventListener('offline', nativeHandler); + if (isWindowPresent) { + window.removeEventListener('online', nativeHandler); + window.removeEventListener('offline', nativeHandler); + } } // Remove handlers From 84a2cc95226ebb92f239c7ff50d3076bd2124522 Mon Sep 17 00:00:00 2001 From: Hector Ayala Date: Mon, 28 Nov 2022 11:59:19 -0400 Subject: [PATCH 9/9] Update src/internal/nativeModule.web.ts Co-authored-by: Mike Hardy --- src/internal/nativeModule.web.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/internal/nativeModule.web.ts b/src/internal/nativeModule.web.ts index c606d157..411ab0a4 100644 --- a/src/internal/nativeModule.web.ts +++ b/src/internal/nativeModule.web.ts @@ -70,6 +70,8 @@ declare global { webkitConnection?: Connection; } } +// Use a constant test of this form because in SSR on next.js, optional chaining is not sufficient, +// but this test correctly detects that window is not available and allows for conditionals before access const isWindowPresent = typeof window !== 'undefined'; // Check if window exists and if the browser supports the connection API