Typing issue while trying to update replaysSessionSampleRate after Sentry is initialized
Is there an existing issue for this?
- [x] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
- [x] I have reviewed the documentation https://docs.sentry.io/
- [x] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases
How do you use Sentry?
Sentry Saas (sentry.io)
Which SDK are you using?
@sentry/react
SDK Version
10.42.0
Framework Version
React 19.2.7
Link to Sentry event
No response
Reproduction Example/SDK Setup
I'm trying to add the replay integration in after I initialize sentry ( so that I can control the set the replaysSessionSampleRate value for certain use-cases). But when I do this I see a type error of Property replaysSessionSampleRate does not exist on type.
/* This is fine ✅ */
init({
nodeEnv: nodeEnv,
publicKey,
projectId,
replaysSessionSampleRate,
replaysOnErrorSampleRate
});
/* later on... */
const c = getClient();
const sentryOptions = c?.getOptions();
sentryOptions.replaysSessionSampleRate = 1; // Property replaysSessionSampleRate does not exist on type ClientOptions<BaseTransportOptions>
/* This is fine ✅ */
export const enableReplay = async () => {
const { replayIntegration } = await import('@sentry/react');
const replay = replayIntegration({
mask: [SENTRY_MASKED_CLASS],
unmask: [SENTRY_UNMASKED_CLASS]
});
addIntegration(replay);
};
That seems to incorrect though as I can see the the replaysSessionSampleRate property is available on the sentryOptions object.
Steps to Reproduce
Modify existing Sentry setup in a react app to set sentryOptions.replaysSessionSampleRate after Sentry is initialized
Expected Result
No typing issues
Actual Result
Typings
Additional Context
The application is actually a react-router application in "Data" mode
Priority
No response
Thanks for your issue. I think that is rather hard to solve (maybe even impossible) as replay is for browser only, but React itself is for both browser and server at the same time. Just for my understanding. Do you have a client side rendered React app or is your app also running on the server with server side rendering?
For now you can use the following to make it work:
import { getClient, type BrowserClient } from '@sentry/react';
const c = getClient<BrowserClient>();
const sentryOptions = c?.getOptions();
if (sentryOptions) {
sentryOptions.replaysSessionSampleRate = 1;
}
const c = getClient<BrowserClient>();
Correct, that's how you can type-safely set this. I don't think we'll change the getClient() signature to return more concrete clients. Mutating the options after Sentry.init is generally not a supported use case. What you have here works but is more of a "use at your own risk" territory.
Do you have a client side rendered React app or is your app also running on the server with server side rendering?
Client-side rendered react app
import { getClient, type BrowserClient } from '@sentry/react'; const c = getClient<BrowserClient>(); const sentryOptions = c?.getOptions(); if (sentryOptions) { sentryOptions.replaysSessionSampleRate = 1; }
Really helpful, thanks!!
Mutating the options after
Sentry.initis generally not a supported use case. What you have here works but is more of a "use at your own risk" territory.
Understood. Thanks!