diff --git a/CHANGELOG.md b/CHANGELOG.md index ca349f748e..6985cca418 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # Unreleased - fix: pas maxBreadcrumbs to Android init +- feat: Allow disabling native SDK initialization but still use it #1259 ## 2.1.0 diff --git a/src/js/options.ts b/src/js/options.ts index 916756679c..d83d01d91f 100644 --- a/src/js/options.ts +++ b/src/js/options.ts @@ -20,6 +20,19 @@ export interface ReactNativeOptions extends BrowserOptions { */ enableNativeCrashHandling?: boolean; + /** + * Initializes the native SDK on init. + * Set this to `false` if you have an existing native SDK and don't want to re-initialize. + * + * NOTE: Be careful and only use this if you know what you are doing. + * If you use this flag, make sure a native SDK is running before the JS Engine initializes or events might not be captured. + * Also, make sure the DSN on both the React Native side and the native side are the same one. + * We strongly recommend checking the documentation if you need to use this. + * + * @default true + */ + shouldInitializeNativeSdk?: boolean; + /** Maximum time to wait to drain the request queue, before the process is allowed to exit. */ shutdownTimeout?: number; diff --git a/src/js/sdk.ts b/src/js/sdk.ts index 4f37cd4d06..622b278e9e 100644 --- a/src/js/sdk.ts +++ b/src/js/sdk.ts @@ -23,6 +23,7 @@ const DEFAULT_OPTIONS: ReactNativeOptions = { enableNative: true, enableNativeCrashHandling: true, enableNativeNagger: true, + shouldInitializeNativeSdk: true, }; /** diff --git a/src/js/wrapper.ts b/src/js/wrapper.ts index 238ae8feb9..bf7ab3f6bf 100644 --- a/src/js/wrapper.ts +++ b/src/js/wrapper.ts @@ -79,9 +79,19 @@ export const NATIVE = { * Starts native with the provided options. * @param options ReactNativeOptions */ - async startWithOptions( - options: ReactNativeOptions = { enableNative: true } - ): Promise { + async startWithOptions(_options: ReactNativeOptions): Promise { + const options = { + enableNative: true, + shouldInitializeNativeSdk: true, + ..._options, + }; + + if (!options.shouldInitializeNativeSdk) { + if (options.enableNativeNagger) { + logger.warn("Note: Native Sentry SDK was not initialized."); + } + return false; + } if (!options.dsn) { logger.warn( "Warning: No DSN was provided. The Sentry SDK will be disabled. Native SDK will also not be initalized." diff --git a/test/wrapper.test.ts b/test/wrapper.test.ts index b04a5b8c52..54ac0edd75 100644 --- a/test/wrapper.test.ts +++ b/test/wrapper.test.ts @@ -92,6 +92,29 @@ describe("Tests Native Wrapper", () => { "Note: Native Sentry SDK is disabled." ); }); + + test("does not initialize with shouldInitializeNativeSdk: false", async () => { + const RN = require("react-native"); + + RN.NativeModules.RNSentry.startWithOptions = jest.fn(); + logger.warn = jest.fn(); + + await NATIVE.startWithOptions({ + dsn: "test", + enableNative: true, + shouldInitializeNativeSdk: false, + }); + + expect(RN.NativeModules.RNSentry.startWithOptions).not.toBeCalled(); + + await NATIVE.addBreadcrumb({ + message: "test", + }); + + expect(RN.NativeModules.RNSentry.addBreadcrumb).toBeCalledWith({ + message: "test", + }); + }); }); describe("sendEvent", () => { @@ -222,7 +245,7 @@ describe("Tests Native Wrapper", () => { message: { message: event.message, }, - type: 'event', + type: "event", }); const header = JSON.stringify({ event_id: event.event_id, @@ -231,7 +254,7 @@ describe("Tests Native Wrapper", () => { const item = JSON.stringify({ content_type: "application/json", length: 1, - type: 'event', + type: "event", }); await expect(NATIVE.sendEvent(event)).resolves.toMatch(