fix(shared): Added support for global navigator check to isValidBrowser#8827
fix(shared): Added support for global navigator check to isValidBrowser#8827royanger wants to merge 2 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
🦋 Changeset detectedLatest commit: d7130c8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 21 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
Comment |
@clerk/astro
@clerk/backend
@clerk/chrome-extension
@clerk/clerk-js
@clerk/expo
@clerk/expo-passkeys
@clerk/express
@clerk/fastify
@clerk/hono
@clerk/localizations
@clerk/nextjs
@clerk/nuxt
@clerk/react
@clerk/react-router
@clerk/shared
@clerk/tanstack-react-start
@clerk/testing
@clerk/ui
@clerk/upgrade
@clerk/vue
commit: |
API Changes Report
Summary
No API Changes DetectedAll packages have stable APIs with no detected changes. Report generated by Break Check Last ran on |
| function getNavigator(): Navigator | null { | ||
| if (typeof window !== 'undefined' && window.navigator) { | ||
| return window.navigator; | ||
| } | ||
| if (typeof navigator !== 'undefined') { | ||
| return navigator; | ||
| } | ||
| return null; | ||
| } | ||
|
|
There was a problem hiding this comment.
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 WorkerNavigator, and keep Node SSR false even though modern Node exposes globalThis.navigator.
| function getNavigator(): Navigator | null { | |
| if (typeof window !== 'undefined' && window.navigator) { | |
| return window.navigator; | |
| } | |
| if (typeof navigator !== 'undefined') { | |
| return navigator; | |
| } | |
| return null; | |
| } | |
| function inBrowserWorker(): boolean { | |
| return typeof WorkerGlobalScope !== 'undefined' && globalThis instanceof WorkerGlobalScope; | |
| } | |
| function getNavigator(): Navigator | null { | |
| if (typeof window !== 'undefined' && window.navigator) { | |
| return window.navigator; | |
| } | |
| if (inBrowserWorker() && typeof globalThis.navigator !== 'undefined') { | |
| return globalThis.navigator; | |
| } | |
| return null; | |
| } | |
| it('returns false when there is no window and no navigator (e.g. SSR)', () => { | ||
| const windowSpy = vi.spyOn(global, 'window', 'get'); | ||
| // @ts-ignore - Test | ||
| windowSpy.mockReturnValue(undefined); | ||
| const navigatorSpy = vi.spyOn(global, 'navigator', 'get'); | ||
| // @ts-ignore - Test | ||
| navigatorSpy.mockReturnValue(undefined); | ||
|
|
||
| expect(isValidBrowser()).toBe(false); | ||
| }); |
There was a problem hiding this comment.
See: Suggestions in browser.ts
| it('returns false when there is no window and no navigator (e.g. SSR)', () => { | |
| const windowSpy = vi.spyOn(global, 'window', 'get'); | |
| // @ts-ignore - Test | |
| windowSpy.mockReturnValue(undefined); | |
| const navigatorSpy = vi.spyOn(global, 'navigator', 'get'); | |
| // @ts-ignore - Test | |
| navigatorSpy.mockReturnValue(undefined); | |
| expect(isValidBrowser()).toBe(false); | |
| }); | |
| it('returns false when no window but a non-worker global navigator exists (Node SSR)', () => { | |
| vi.spyOn(global, 'window', 'get').mockReturnValue(undefined as any); | |
| vi.spyOn(globalThis, 'navigator', 'get').mockReturnValue({ userAgent: 'Node.js/25' } as Navigator); | |
| expect(isValidBrowser()).toBe(false); | |
| expect(isValidBrowserOnline()).toBe(false); | |
| }); |
Description
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit