
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
gatsby-plugin-launchdarkly
Advanced tools
A simple plugin that integrates LaunchDarkly into your Gatsby site. This will allow you to use feature flags to rollout new features on your site.
A simple plugin that integrates LaunchDarkly into your Gatsby site. This will allow you to use feature flags to rollout new features on your site.
Add plugin to your Gatsby site:
npm install gatsby-plugin-launchdarkly
Then in your gatsby-config.js
:
// gatsby-config.js
...
plugins: [
...
{
resolve: 'gatsby-plugin-launchdarkly',
options: {
clientSideID: '<your-launchdarkly-project-client-side-id>',
options: {
// any LaunchDarkly options you may want to implement
bootstrap: 'localStorage', // caches flag values in localStorage
},
},
},
...
]
...
This plugin uses LaunchDarkly's React SDK. The SDK requires a client-side ID which you can retrieve from your LaunchDarkly Project settings page. This client-side ID needs to be stored in your gatsby-config.js.
Behind the scenes, this plugin uses the React SDK's withLDProvider
function to initialize the client. Read the documentation on Initializing the React SDK
to understand other configuration options you can provide.
To learn more about the configuration options available in the plugin's options
property, read the documentation on configuration in the JavaScript SDK.
To use a LaunchDarkly feature flag in your component, first import the LaunchDarklyContext
. This plugin uses React Context to make the LaunchDarkly SDK
available to your Gatbsy components.
import { useFlags } from 'gatsby-plugin-launchdarkly'
Then within your component, you can do the following:
// In a functional component...
const Header = ({ siteTitle }) => {
// The following contains all of the client-side flags. Flag names are
// automatically converted to snake-case which will allow you to pull out
// one or more flags directly through destructuring.
const flags = useFlags()
return (
<header
style={{
background: flags.someNewFeature ? 'green' : 'gray'
}}
>
...
Note that the LaunchDarkly SDK will automatically convert flag names to snake-case.
In addition to the useFlags
hook, the useLDClient
hook gives you direct access to the LaunchDarkly client:
import React from 'react';
import { useFlags, useLDClient } from 'gatsby-plugin-launchdarkly';
const HooksDemo = () => {
const { someNewFeature } = useFlags();
const ldClient = useLDClient();
const onLoginSuccessful = () => ldClient.identify({ kind: 'user', key: 'user-key-123abc' });
return (
<div>{someNewFeature ? 'Flag on' : 'Flag off'}</div>
);
};
export default HooksDemo;
If you're using class components, you can use the withLDConsumer
higher-order
component to do this instead:
import { withLDConsumer } from 'gatsby-plugin-launchdarkly'
// In your class component...
class MyComponent extends React.Component {
render() {
// Wrapping your class component with the withLDConsumer HOC injects the
// flags and ldClient props into your component
const { flags, ldClient } = this.props;
return
<header
style={{
background: flags.someNewFeature ? 'green' : 'gray'
}}
...
>
}
}
export default withLDConsumer()(MyComponent)
The withLDConsumer
HOC injects the flags
and ldClient
as props to your
class component.
This plugin assumes that the end user viewing your site is anonymous, which is likely the case for most Gatsby sites. In this situation, the LaunchDarkly SDK uniquely tracks each end user and remembers what variation of each flag was served to them. This is transparent and you don't need to do anything else to make it work this way.
If you have a logged-in end user, and can identify that end user to LaunchDarkly and then target that end user for a feature. To do this, access the LDClient
object directly:
import React from 'react';
import { useFlags, useLDClient } from 'gatsby-plugin-launchdarkly';
const HooksDemo = () => {
const { someNewFeature } = useFlags();
const ldClient = useLDClient();
// Calling `identify` will cause the flags to be re-evaluated for the
// new end user that's logged in. Changes in flag values will stream in and
// could cause your component to re-render.
const onLoginSuccessful = (user) => ldClient.identify({
kind: 'user',
key: user.id,
firstName: user.firstName,
lastName: user.lastName,
anonymous: false,
});
return (
<div>{someNewFeature ? 'Flag on' : 'Flag off'}</div>
);
};
export default HooksDemo;
To learn more about changing the user context, read the identify
documentation for the JavaScript SDK.
We encourage pull requests and other contributions from the community. Check out our contributing guidelines for instructions on how to contribute to this plugin.
[1.0.0] - 2023-06-30
The latest version of this SDK supports LaunchDarkly's new custom contexts feature. Contexts are an evolution of a previously-existing concept, "users." For more information please read the JavaScript SDK's latest release notes.
For detailed information about this version, please refer to the list below. For information on how to upgrade from the previous version, please read the react migration guide.
context
configuration option has been added.user
configuration option has been deprecated. Please use context
instead.FAQs
A simple plugin that integrates LaunchDarkly into your Gatsby site. This will allow you to use feature flags to rollout new features on your site.
The npm package gatsby-plugin-launchdarkly receives a total of 1,856 weekly downloads. As such, gatsby-plugin-launchdarkly popularity was classified as popular.
We found that gatsby-plugin-launchdarkly demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers 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.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.