Optimizely object is not valid. Failing isFeatureEnabled

asa*_*cal 5 optimizely

Trying to use the React and JavaScript SDKs for Optimizely, but getting the following error in the console:

OPTIMIZELY: Optimizely object is not valid. Failing isFeatureEnabled.
Run Code Online (Sandbox Code Playgroud)

More info about my setup below:

Installed via Yarn: yarn add @optimizely/react-sdk

Import statement in the app container:

import {
  createInstance
} from '@optimizely/react-sdk'
Run Code Online (Sandbox Code Playgroud)

Logic in render function:

const optimizely = createInstance({
  sdkKey: '<SDK_KEY>',
})

const enabled = optimizely.isFeatureEnabled('example_feature', 'user123');
Run Code Online (Sandbox Code Playgroud)

I get this error in the Chrome console:

OPTIMIZELY: Optimizely object is not valid. Failing isFeatureEnabled.
Run Code Online (Sandbox Code Playgroud)

asa*_*cal 9

The Optimizely object will log that error when you call isFeatureEnabled before the SDK has successfully loaded your project's datafile. This can happen for a number of reasons outlined below. Looking at the code example provided in the question, it looks like reason #4 is the most likely cause of the error, but here are all of them:

1. Bad SDK key

If you pass in a bad SDK Key to createInstance, the SDK will not successfully load the datafile and you will get this error.

const optimizely = createInstance({
  sdkKey: 'invalid-sdk-key'
})
Run Code Online (Sandbox Code Playgroud)

2. Malformed datafile

If you are passing in the datafile directly to createInstance, but pass in an object that isn't the proper datafile format, you will get this error:

const optimizely = createInstance({
  datafile: { wrong: 'format' }
})
Run Code Online (Sandbox Code Playgroud)

3. Inaccessible datafile

Make sure you can access the url of your datafile in a web browser: https://cdn.optimizely.com/datafiles/<Your_SDK_Key>.json. If you get an AccessDenied (403) or Not Found (404) error and your account is new, make sure you create something in the Optimizely UI so that Optimizely is triggered to create and upload a proper datafile.

If in the console of your running application you see a 403 or 404 for the request to the datafile, ensure there are no ad-blockers, firewalls, or proxies preventing the SDK from requesting the datafile on Optimizely's CDN from the SDK.

4. Not waiting for Optimizely SDK to be ready

Even if you have the right SDK Key and the SDK can access Optimizely's CDN. If you don't give the SDK enough time for the datafile request to finish, you will be trying to use the SDK before it's ready.

在 JavaScript SDK 中,可以使用以下onReady方法解决:

const optimizely = createInstance({
  sdkKey: 'valid-sdk-key',
});

optimizely.onReady().then(() => {
  // optimizely is ready to use, with datafile downloaded from the Optimizely CDN
});
Run Code Online (Sandbox Code Playgroud)

如果使用<OptimizelyFeature>React SDK 的<OptimizelyFeature>组件,则组件将自动等待,直到<OptimizelyProvider>成功加载数据文件,然后再评估isFeatureEnabled.

  • 如果有广告拦截器,您需要将数据文件托管在您自己的服务器上,并使用“urlTemplate”参数告诉 SDK 从您的服务器而不是从 Optimizely 的服务器下载数据文件:https://docs.developers.optimizely .com/full-stack/docs/initialize-sdk-javascript (2认同)