-
Notifications
You must be signed in to change notification settings - Fork 453
fix(shared): Added support for global navigator check to isValidBrowser #8827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@clerk/shared': patch | ||
| --- | ||
|
|
||
| Resolve the browser connectivity heuristics (`isValidBrowser`, `isBrowserOnline`, and therefore `isValidBrowserOnline`) from the global `navigator` when `window` is unavailable. In runtimes that have no `window` but do expose a global `navigator` — most notably an MV3 extension background **service worker** (where `@clerk/chrome-extension` loads the background client) — these checks previously always reported "invalid/offline". That caused `getToken()` failures to be re-thrown as a misleading `clerk_offline` error and capped network retries lower than intended. The checks now read real connectivity from the worker's `navigator`. Environments with no navigator at all (e.g. SSR) continue to report `false`, and behavior in standard browsers and React Native is unchanged. |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,13 +49,35 @@ export function userAgentIsRobot(userAgent: string): boolean { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| return !userAgent ? false : botAgentRegex.test(userAgent); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Resolves the `Navigator` object from either the DOM `window` (standard browsers) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * or the global scope. Web/Service Workers — e.g. an MV3 extension background service | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * worker — have no `window`, but do expose a `WorkerNavigator` as `globalThis.navigator` | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * with the `onLine`/`userAgent` properties our heuristics rely on. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns `null` only when no navigator is available anywhere. We intentionally do NOT | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * treat the absence of a navigator as a valid environment — only a real navigator object | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * enables the browser/online heuristics below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| function getNavigator(): Navigator | null { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof window !== 'undefined' && window.navigator) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return window.navigator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof navigator !== 'undefined') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return navigator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+64
to
+73
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I threw this at Codex and it suggested narrowing the fallback to browser worker globals, not "anything with a global navigator." This should preserves existing browser behavior, enable MV3/service-worker
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Checks if the current environment is a browser and the user agent is not a bot. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function isValidBrowser(): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const navigator = inBrowser() ? window?.navigator : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const navigator = getNavigator(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!navigator) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -68,7 +90,7 @@ export function isValidBrowser(): boolean { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| * @returns | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function isBrowserOnline(): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const navigator = inBrowser() ? window?.navigator : null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const navigator = getNavigator(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!navigator) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See: Suggestions in
browser.ts