-
Notifications
You must be signed in to change notification settings - Fork 37
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
createInstance can throw: how to graceful fallback #134
Comments
…le from the context this allows an application to catch a thrown error from `createInstance` and use `null` as a fallback for the OptimizelyProvider, keeping the application working instead of crashing. see optimizely#134 for more info fixes optimizely#134
+1 Your PR would save me a re-render I'm currently doing something like this:
I shouldn't have to be doing this |
@Develliot the problem is that with your code, the optimizely hooks throw when rendered without |
@fabb @Develliot We are picking this up and will try to fix it over the next few days. its a legit ask to add safeguard into React SDK to make sure |
Hi, |
Why can't the provider just be handed the SDK key and deal with client instance all internally either SSR or async why is it a separate thing that we need to think about, it would be a much nice dev experience if we only need to think about the provider and the use decision hook. |
# Summary Handle React SDK fail catching if ._client returns null when creating new ReactClientSDK instance from `createInstance` function. # Test Plan Manually tested thoroughly. All existing unit tests pass. # Issues #134
@fabb - hello! Thanks again for the suggestions, we've gone ahead and readied a PR to address this issue here: #149 This resolution will be part of the next minor version update of the SDK which should be coming soon. Once it's released we will update this issue and close it. Also, wanted to repost this reply here in case anyone misses it on the PR: @Develliot - We discussed this approach internally and due to the nature of the change, we will consider the change as a new feature request as it requires refactoring many components. Our suggestion is to add it to our feature request board here - we will prioritize it based on the number of votes it gets! Best, cc: @zashraf1985 |
@fabb - just released a new patch yesterday that should resolve this. Thanks again for reporting the issue! cc: @zashraf1985 |
Nice, thanks! |
There are a few issues that make it impossible for the application to still work if optimizely fails to initialize:
optimizely.createInstance
fromoptimizely-sdk
can returnnull
https://github.com/optimizely/javascript-sdk/blob/v4.7.0/packages/optimizely-sdk/lib/index.browser.ts#L136
createInstance
in thereact-sdk
does not handle thenull
case and then just throws in the constructor when accessingthis._client.onReady
https://github.com/optimizely/react-sdk/blob/2.7.0/src/client.ts#L195-L201
OptimizelyProvider
because it needs anOptimizelyReactSDKClient
instancehttps://github.com/optimizely/react-sdk/blob/2.7.0/src/Provider.tsx#L27
OptimizelyProvider
is not an option because the optimizely hooks fromreact-sdk
throw when there is nooptimizely
instance provided via theOptimizelyProvider
https://github.com/optimizely/react-sdk/blob/2.7.0/src/hooks.ts#L332-L336
We currently see in production ~70.000 failed optimizely initializations / hour. All happening in Firefox, and we need to investigate more, but the main issue is that failing optimizely makes the application unusable, which is not a graceful fallback.
I have different ideas to solve this:
react-sdk
allows to renderOptimizelyProvider
withoptimizely={null}
and the optimizely hooks are modified to not throw anymore when nooptimizely
instance is available. Applications will need to catch the thrown error from the initialcreateInstance
call and as a fallback usenull
. The hooks then fall back to the same values they would when optimizely is not yet ready.react-sdk
does not throw increateInstance
whenoptimizely
isnull
and rather wraps all accesses tothis._client
with anull
check.react-sdk
provides aDummyReactClient
that implements all the functions fromReactSDKClient
with no functionality. Applications will need to catch the thrown error from the initialcreateInstance
call and as a fallback use theDummyReactClient
and provide it toOptimizelyProvider
.I would vote for option 1. because it still allows applications to detect when
createInstance
fails and allows for the maximum flexibility. Also it's backwards-compatible.The text was updated successfully, but these errors were encountered: