Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@optimizely/js-web-sdk
Advanced tools
Readme
track
calls that happen before the datafile is downloadednpm install @optimizely/js-web-sdk
// ES Modules
import * as optimizelySDK from '@optimizely/js-web-sdk'
// CommonJS
const optimizelySDK = require('@optimizely/js-web-sdk')
<!-- UMD script, assigns to window.jsWebSdk -->
<script src="https://unpkg.com/@optimizely/js-web-sdk/dist/js-web-sdk.browser.umd.min.js"></script>
This is the ideal case and prevents a lot of timing issues and complexity, however we realize not all customers will have the ability to this.
import * as optimizelySDK from '@optimizely/js-web-sdk'
const optimizely = optimizelySDK.createInstance({
datafile: window.datafile,
})
// all calls can happen immediately after (sync)
optimizely.activate('my-exp', 'user1')
By providing the sdkKey
option to createInstance
the SDK will automatically fetch the datafile. If a cached datafile exists it will use the cached version. Decisions made after the fresh datafile has loaded will use the new datafile.
Asnyc load and wait until datafile is loaded
import * as optimizelySDK from '@optimizely/js-web-sdk'
const optimizely = optimizelySDK.createInstance({
sdkKey: 'GaXr9RoDhRcqXJm3ruskRa',
})
// At this point optimizely can be used, on first page load the datafile will not be fetched and methods will no-op
// On second page load it will use the cached datafile immediately
//
initApp()
optimizely.onReady()
to block renderingBy using await optimizely.onReady()
you can gaurantee code wont be run until the datafile is downloaded
import * as optimizelySDK from '@optimizely/js-web-sdk'
const optimizely = optimizelySDK.createInstance({
sdkKey: 'GaXr9RoDhRcqXJm3ruskRa',
})
await optimizely.onReady()
// at this point datafile is gauranteed to be loaded
initApp()
However, the above example isn't great because Optimizely could time out due to network connectivity issues. By passing a timeout
to optimizely.onReady()
we can gaurantee that Optimizely won't block the page for more than X milliseconds.
import * as optimizelySDK from '@optimizely/js-web-sdk'
const optimizely = optimizelySDK.createInstance({
sdkKey: 'GaXr9RoDhRcqXJm3ruskRa',
})
// Dont wait more than 200ms, if there is a cached datafile this will immediately resolve
await optimizely.onReady({ timeout: 200 })
// you can also use the Promise API
optimizely.onReady({ timeout: 200 }).then(() => {
initApp()
})
It's worth noting that optimizely.onReady
can be called as many times, once the datafile has downloaded this will always return a resolved promise. This is a powerful mechanism to build UI components, as a UI component can be configured to block up to X milliseconds waiting for Optimizely to load, while other parts of the UI are unaffected.
By default loading the datafile by URL will store the contents of the datafile in localStorage
, on second page load we are guaranteed to have synchronous access to the datafile.
The underlying DatafileManager will also make a background request to get an updated datafile, however that will not be registered until the next instantiation of Optimizely via optimizely.createInstance
which is usually the next page load.
When using optimizely async the user will only have to pay the loading cost once on first page load, subsequent page loads are always synchronous
This SDK can be used stand alone to bolster the current javascript-sdk with things like automatic datafile loading and caching. It can also be used with the ReactSDK to simplify Feature Flagging and AB Testing in React.
First-party code (under lib/ and dist/) is copyright Optimizely, Inc. and contributors, licensed under Apache 2.0.
Prod dependencies are as follows:
{
"json-schema@0.2.3": {
"licenses": [
"AFLv2.1",
"BSD"
],
"publisher": "Kris Zyp",
"repository": "https://github.com/kriszyp/json-schema"
},
"lodash@4.17.11": {
"licenses": "MIT",
"publisher": "John-David Dalton",
"repository": "https://github.com/lodash/lodash"
},
"murmurhash@0.0.2": {
"licenses": "MIT*",
"repository": "https://github.com/perezd/node-murmurhash"
},
"sprintf-js@1.1.1": {
"licenses": "BSD-3-Clause",
"publisher": "Alexandru Mărășteanu",
"repository": "https://github.com/alexei/sprintf.js"
},
"uuid@3.3.2": {
"licenses": "MIT",
"repository": "https://github.com/kelektiv/node-uuid"
}
}
To regenerate this, run the following command:
npx license-checker --production --json | jq 'map_values({ licenses, publisher, repository }) | del(.[][] | nulls)'
Please see CONTRIBUTING
FAQs
Browser wrapper for Optimizely's Javascript SDK
The npm package @optimizely/js-web-sdk receives a total of 146 weekly downloads. As such, @optimizely/js-web-sdk popularity was classified as not popular.
We found that @optimizely/js-web-sdk demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.