Skip to content

Conversation

@yousif-bugsnag
Copy link
Contributor

@yousif-bugsnag yousif-bugsnag commented Nov 21, 2025

Goal

createReactNativeClient allows upstream libraries such as Expo to create and export their own customized client instance, however there are some situations where the client may still be imported from @bugsnag/react-native-performance directly, for example in the navigation plugins or if a user inadvertently imports directly @bugsnag/react-native-performance. When that happens, we can end up with a separate, duplicate client instance

This PR refactors the React Native client from a direct export to a singleton proxy pattern in order to ensure there is only ever a single client instance per app regardless of how it's imported.

Design

Upstream libraries (like Expo) can create and register a client via registerClient() upfront before any usage - otherwise the client is instantiated lazily on first property access. This ensures there is only ever a single client instance per app regardless of how it's imported.

This also has the slight added advantage of only initializing the client when it's actually used (e.g. when start is called), rather than being a side effect of importing.

Once the client instance is initialized, all of it's properties are added to the proxy target and the get trap is removed, effectively bypassing the proxy for all subsequent calls. This optimization means there is no significant performance hit once the client is initialized.

Testing

Added new unit tests to verify the new singleton behavior and also tested manually with Expo

@github-actions
Copy link

github-actions bot commented Nov 21, 2025

Browser bundle size

NPM build

Package
Before 235.35 kB
After 235.35 kB
± No change

CDN build

Unminified Minfied Minified + gzipped
Before 119.43 kB 46.18 kB 13.29 kB
After 119.43 kB 46.18 kB 13.29 kB
± No change No change No change

Code coverage

Ok File (✨=New File) Lines Branches Functions Statements
🔴 /home/runner/work/bugsnag-js-performance/bugsnag-js-performance/packages/platforms/react-native/lib/client.ts 100%
(+0%)
50%
(-50%)
100%
(+0%)
93.75%
(-6.25%)
/home/runner/work/bugsnag-js-performance/bugsnag-js-performance/packages/platforms/react-native/lib/create-client.ts 100%
(+0%)
97.53%
(+0.2%)
100%
(+0%)
100%
(+0%)
🔴 /home/runner/work/bugsnag-js-performance/bugsnag-js-performance/packages/platforms/react-native/lib/index.ts 100%
(+0%)
100%
(+0%)
16.66%
(-3.34%)
100%
(+0%)

Total:

Lines Branches Functions Statements
85%(+0.06%) 63.98%(+0.11%) 75.76%(-0.03%) 81.79%(+0.04%)

Generated against abe859f on 21 November 2025 at 16:43:50 UTC

@yousif-bugsnag yousif-bugsnag marked this pull request as ready for review November 21, 2025 16:22
@yousif-bugsnag yousif-bugsnag force-pushed the PLAT-15226/singleton-react-native-client branch from 17519a5 to abe859f Compare November 21, 2025 16:38
Copy link
Contributor

@lemnik lemnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@yousif-bugsnag yousif-bugsnag merged commit 3b07bb2 into next Nov 24, 2025
34 checks passed
@yousif-bugsnag yousif-bugsnag deleted the PLAT-15226/singleton-react-native-client branch November 24, 2025 12:28
@yousif-bugsnag yousif-bugsnag mentioned this pull request Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants