Skip to content

Commit f0533a2

Browse files
fix(shared): avoid false offline detection in React Native (#8084)
Co-authored-by: Christopher Canin <chris@clerk.dev>
1 parent ac2f1c1 commit f0533a2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@clerk/shared": patch
3+
---
4+
5+
Fix false offline detection in React Native by checking `navigator.product` and `typeof navigator.onLine` before treating the environment as disconnected

packages/shared/src/__tests__/browser.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,17 @@ describe('isValidBrowserOnline', () => {
194194

195195
expect(isValidBrowserOnline()).toBe(true);
196196
});
197+
198+
it('returns TRUE in React Native when navigator.onLine is not implemented', () => {
199+
userAgentGetter.mockReturnValue(undefined);
200+
webdriverGetter.mockReturnValue(undefined);
201+
onLineGetter.mockReturnValue(undefined);
202+
connectionGetter.mockReturnValue(undefined);
203+
Object.defineProperty(window.navigator, 'product', {
204+
configurable: true,
205+
get: () => 'ReactNative',
206+
});
207+
208+
expect(isValidBrowserOnline()).toBe(true);
209+
});
197210
});

packages/shared/src/browser.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ export function isBrowserOnline(): boolean {
7373
return false;
7474
}
7575

76+
// Some environments (e.g. React Native) define a Navigator object but do not
77+
// implement navigator.onLine as a boolean. Default to online in those cases.
78+
if (typeof navigator.onLine !== 'boolean') {
79+
return true;
80+
}
81+
7682
// navigator.onLine is the standard API and is reliable for detecting
7783
// complete disconnection (airplane mode, WiFi off, etc.).
7884
// The experimental navigator.connection API (rtt/downlink) was previously

0 commit comments

Comments
 (0)