LaunchDarkly引导:需要(JS)属性分配



我正在设置LaunchDarkly来控制我的第一个功能标志及其在服务器上的正常工作;客户端。现在我正在尝试LaunchDarkly Bootstrap方法(从下面给定的链接(,并在我的代码下面尝试,但它不接受双大括号,我不知道如何使用引导方法获得标志值,所以我的代码哪里出了问题?。有人能帮我举个例子吗?

链接,

https://docs.launchdarkly.com/docs/js-sdk-reference#section-bootstrapping

使用Bootstrap选项初始化客户端,如下所示,

client = LDClient.initialize(sdkKey, userContext.user, options = {
bootstrap: {
{{ ldclient.all_flags(userContext.user) }}
}
});

我的函数是获取标志值,

isFeatureEnabled: function (featureFlag, properties) {
console.log("Before Variation");
//we shall update the custom properties into user context.
if (properties) {
for (var k in properties) {
userContext.user.custom[k] = properties[k];
}
}
//later make the identity call to update the user details.
client.identify(userContext.user, null, function () { /*rules updated*/
console.log("New user's flags available");
//validate the feature flag
var showFeature = client.variation(featureFlag);
if (!showFeature) {
window.in8.platform.showUnauthorized('');
}
console.log("after Variation");
});
}

完全公开,我叫John,是LaunchDarkly支持团队的一员。我很乐意帮你解决这个问题

首先,您使用的似乎是引导示例的旧版本。新示例修复了拼写错误,并使用了新的all_flags_state方法。

我认为这里有两个主要问题。主要问题是如何从后端到前端引导标志变体,以及在使用引导时如何适当利用LaunchDarkly。我将首先解决如何从后端引导标志变体的问题。

LaunchDarkly文档中的示例利用模板将引导的值包括在前端。模板化是一种将程序生成的内容包含在静态源文件或文本文件中的策略。模板化通常在编译或部署代码时使用,或者在运行时向客户端提供内容时使用。这样做是为了在最终版本中呈现当时才可用的信息。

不同的模板语言有不同的行为方式,但一般来说,您在源文件或文本文件中包含令牌,这些令牌指示模板呈现器用您提供的数据替换该令牌。

在文档中,它提到这个例子是使用Ruby进行模板化的,但这个例子使用的是Mustache渲染,Mustache有很多不同的语言。模板化是一种将程序生成的内容包含在静态源文件或文本文件中的策略。这通常在编译或部署代码时使用,或者在运行时向客户端提供内容时使用。这样做是为了在最终版本中呈现当时才可用的信息。

该示例可能不起作用,这取决于您使用的后端语言和框架。根据与您的问题相关的标签,我可以放心地假设您正在使用.NET为后端供电,而后端没有指定的模板语言。不过,有很多开源解决方案。

在下面的示例中,我将使用https://github.com/rexm/Handlebars.Net以将a用户引导的标志值呈现到CCD_ 1变量中。我将借用handlebars repo中的示例以及LaunchDarkly的hello-bootstraphello-dotnetrepo中提供的代码,它们在这里可用:https://github.com/launchdarkly/hello-bootstrap&https://github.com/launchdarkly/hello-dotnet

string source =
@"
<html>
<head>
<script src=""https://app.launchdarkly.com/snippet/ldclient.min.js""></script>
<script>
window.ldBootstrap={{ldBootstrap}};
window.ldClientsideId=""{{ldClientsideId}}"";
window.ldUser={{ldUser}};
</script>
</head>
<body>
<h1>LaunchDarkly server-side bootstrap example</h1>
<ul>
<li><code>normal client</code>: <span class=""normal"">initializing…</span></li>
<li><code>bootstrapped client</code>: <span class=""bootstrap"">initializing…</span></li>
</ul>
<script>
var user = window.ldUser;
console.log(`Clients initialized`);
var client = LDClient.initialize(window.ldClientsideId, user);
var bootstrapClient = LDClient.initialize(window.ldClientsideId, user, {
bootstrap: window.ldBootstrap
});
client.on('ready', handleUpdateNormalClient);
client.on('change', handleUpdateNormalClient);
bootstrapClient.on('ready', handleUpdateBootstrapClient);
bootstrapClient.on('change', handleUpdateBootstrapClient);
function handleUpdateNormalClient(){
console.log(`Normal SDK updated`);
render('.normal', client);
}
function handleUpdateBootstrapClient(){
console.log(`Bootstrapped SDK updated`);
render('.bootstrap', bootstrapClient);
}
function render(selector, targetClient) {
document.querySelector(selector).innerHTML = JSON.stringify(targetClient.allFlags(user), null, 2);
}
</script>
</body>
</html>";
var template = Handlebars.Compile(source);
Configuration ldConfig = LaunchDarkly.Client.Configuration.Default("YOUR_SDK_KEY");
LdClient client = new LdClient(ldConfig);
User user = User.WithKey("bob@example.com")
.AndFirstName("Bob")
.AndLastName("Loblaw")
.AndCustomAttribute("groups", "beta_testers");
var data = new {
ldBootstrap: JsonConvert.SerializeObject(client.AllFlagsState(user)),
ldUser = JsonConvert.SerializeObject(user),
ldClientsideId = "YOUR_CLIENT_SIDE_ID"
};
var result = template(data);

您可以使用这个示例,并对其进行调整,以便在向用户提供页面时呈现静态源代码。

第二个问题是如何使用SDK。我看到您每次在评估用户之前都会调用identify。每次调用标识时,SDK都需要重新初始化。这意味着,即使在引导您的初始变体之后,您也会通过调用identify来强制SDK重新初始化,从而消除引导的所有好处。作为一种解决方案,请检测用户对象是否已更改。如果有,则调用identify。否则,不要调用identify以便SDK使用缓存的用户属性。

如果你想更深入地了解这一点,并为我们提供更多的包装源,你可以联系我们support@launchdarkly.com

最新更新