-
-
Notifications
You must be signed in to change notification settings - Fork 720
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
Fallback Support for JavaScript #8009
Comments
Hi @exhuma, thanks for raising this and proposing a solution. The Frontend SDKs works slightly different in Unleash, as they by default only knows about enabled feature flags for the provided context. There are two main reasons for this:
Because of this the frontend SDK will not be able to distinguish between "flag disabled" and "flag missing", which is the purpose of the fallback-value supported by server-side SDKs. What we have done for the worst-case scenario you describe is the bootstrap support. This way to can bootstrap the SDK that will take precedence in the case the SDK is not able to connect back to the Unleash API: Does the fallback mechanism solve your needs? |
I think this solves it partially. I tested two situations:
It would be nice to have an "unknown" state to let the application decide what to do in that case. I understand the reasoning why the front-end SDK only sees a "true" and "false". Especially for applications with a large number of feature flags. One way (but inefficient) to support the "unknown" state would be to sync all feature-flags on application startup (f.ex. when I've been thinking about a more efficient solution leveraging the polling process but this only sees information whenever a flag changes. For an established/live application there will be no changes and the polling always returns a I don't see a better solution than the one you currently implemented. The bootstrap is good for an unreachable unleash instance but it's not usable for feature-flags that are not yet known . I understand that, because of these constraints having an "unknown" state is currently not possible. If you like you can close this issue. |
thanks for the update. I will leave it open for a bit. This is an interesting use-case, but it does entail that we re-architect a bit on what flags are shared with frontend SDKs. One high level thought would be to mark frontend-flags when you configure them (or possibly at the project level). However, this would require deep analysis to not break existing usage of Unleash. One alternative for you, that might work right now:
By combining these two you could build your own fallback function like this: function isEnabled(name, fallback) {
if(client.getAllToggles().map(t => t.name).includes(name)) {
return client.isEnabled(name);
} else {
return fallback;
}
} |
I saw the Another solution could be to have something similar to the "bootstrap" option that - when set - will do an initial "pull" of all the known feature-flags. This could be a fairly simple payload that is sufficient for the This could be "false" by default to keep the same behaviour as we have now. This could then be used as follows: const client = new UnleashClient({fetchKnownFlags: true});
client.isEnabled('my-unknown-flag') // -> undefined
client.isEnabled('my-known-flag') // -> false While I'm not a huge fan of abusing if (client.isEnabled('my-unknown-feature')) { ... } When using it without the option the result would be: const client = new UnleashClient({fetchKnownFlags: false});
client.isEnabled('my-unknown-flag') // -> false
client.isEnabled('my-known-flag') // -> false Having the const client = new UnleashClient({fetchKnownFlags: true});
client.isEnabled('my-unknown-flag') ?? false // -> false
client.isEnabled('my-unknown-flag') ?? true // -> true That way, it would be up to the user to enable that initial "download" of flags or not. If you never need to cover that case, you can leave it off for startup speedup. It might make sense to periodically refresh that list if that feature is requested from the client. This could be included in the proxy response whenever the list changes. Additionally/Alternatively, the client can update that list whenever it sees a new feature for the first time. For example, assume that on startup the flags |
Describe the feature request
Add support for "fallback" values to the JS API
Background
The documentation of the Python SDK mentions fallback values. This support is not present in the current JS SDK
Currently the
isEnabled
method cannot return a stable default value as recommended in your best practices. Some features may make sense to be "enabled by default" while others may need to be "disabled by default".If the state cannot be retrieved for any reason, the current JS-SDK only ever returns
false
.In a worst-case scenario this would leave application devoid of much functionality whenever unleash is unreachable or when features are not found for whatever reason.
Solution suggestions
Provide the same solution as provided in the Python SDK.
For example:
Alternatively, a static value would work too but this would break consistency with the Python SDK:
The text was updated successfully, but these errors were encountered: