Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix_rn_offline_detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/shared": patch
---

Fix false offline detection in React Native by checking `navigator.product` and `typeof navigator.onLine` before treating the environment as disconnected
13 changes: 13 additions & 0 deletions packages/shared/src/__tests__/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,17 @@ describe('isValidBrowserOnline', () => {

expect(isValidBrowserOnline()).toBe(true);
});

it('returns TRUE in React Native when navigator.onLine is not implemented', () => {
userAgentGetter.mockReturnValue(undefined);
webdriverGetter.mockReturnValue(undefined);
onLineGetter.mockReturnValue(undefined);
connectionGetter.mockReturnValue(undefined);
Object.defineProperty(window.navigator, 'product', {
configurable: true,
get: () => 'ReactNative',
});

expect(isValidBrowserOnline()).toBe(true);
});
});
6 changes: 6 additions & 0 deletions packages/shared/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ export function isBrowserOnline(): boolean {
return false;
}

// Some environments (e.g. React Native) define a Navigator object but do not
// implement navigator.onLine as a boolean. Default to online in those cases.
if (typeof navigator.onLine !== 'boolean') {
return true;
}

// navigator.onLine is the standard API and is reliable for detecting
// complete disconnection (airplane mode, WiFi off, etc.).
// The experimental navigator.connection API (rtt/downlink) was previously
Expand Down
Loading