1.11.0 • Published 8 months ago

@newskit-render/feature-flags v1.11.0

Weekly downloads
-
License
UNLICENSED
Repository
-
Last release
8 months ago

@newskit-render/feature-flags

A package for adding feature flags to projects generated by newskit-render.

How to use:

yarn add @newskit-render/feature-flags or npm install --save @newskit-render/feature-flags

There are two ways that feature flags can be utilized.

getFeatureFlags() in getServerSideProps

The most performant way to use feature flags is to call getFeatureFlags() or getFeatureFlagsByKeys() in getServerSideProps and then use them throughout the page. This way the implementation will only be available for the current page and and wouldn't effect the rest of the site. Best practice is to store your sdk key in environment variables, the examples below utilize that method. For local use, you cadd the Optimizely SDK key from your project in your .env.local file like so OPTIMIZELY_SDK_KEY="123". In case you don't have project or access to the Optimizely platform please contact the Experimentation team. An example implementation would be as follows:

pages/index.ts

import { getFeatureFlags } from '@newskit-render/feature-flags';

export async function getServerSideProps(context) {
  const featureFlags = await getAllFeatureFlags(config)

  return {
    props: {
      // other data
      featureFlags
    },
  }
}

The package also provides a function that is going to return only selected flags

import { getFeatureFlagsByKeys } from '@newskit-render/feature-flags';

export async function getServerSideProps(context) {
  const featureFlags = getFeatureFlagsByKeys(['flag_1', 'flag_2'], config)

  return {
    props: {
      // other data
      featureFlags
    },
  }
}

Alternatively, feature flags can be applied on the whole app. To do so, you'll need to instantiate the package in getInitialProps of the main app, then call getFeatureFlags and pass the result to the whole app. By calling the useFeatureFlagsContext hook, the list of featureFlags can be accessed from any point of the site. Below we explore a solution, where we use it in the header of the app.

pages/_app.tsx

import { getFeatureFlags, FeatureFlagsContextProvider, FeatureFlag, createFeatureFlagsInstance } from '@newskit-render/feature-flags';

function MyApp({ featureFlags }: {featureFlags: FeatureFlag[]}) {
  return (
    <FeatureFlagsContextProvider context={ featureFlags } >
      <App />
    </FeatureFlagsContextProvider>
  )
}
MyApp.getInitialProps = async () => {
    createFeatureFlagsInstance({ optimizelyConfig: { sdkKey: process.env.OPTIMIZELY_SDK_KEY } })
  const featureFlags = await getFeatureFlags();
  return { featureFlags }
}

export default MyApp

header/index.tsx

import { useFeatureFlagsContext } from '@newskit-render/feature-flags';

const Header: React.FC<{ user: UserData }> = ({ user }) => {
  const featureFlags = useFeatureFlagsContext();

  return (
    <>
      <StyledHeader>
        <MainGrid>
          <Cell xs={12}>
              // ...nav buttons
              {featureFlags && featureFlags['test_flag'] && <p>FEATURE FLAGG</p>}
              // ...nav buttons
              </Stack>
            </Stack>
          </Cell>
        </MainGrid>
      </StyledHeader>
    </>
  )
}

export default Header

config argument

The config object is optional. It can be added as an argument to both getFeatureFlags() and getFeatureFlagsByKeys() functions. By default the package is going to get the OPTIMIZELY_SDK_KEY directly from the environment variables but the key can be overwritten with a key added in the config.

The configurations that can be passed to Optimizely in the config object is:

  sdkConfig?: {
    datafileOptions?: DatafileOptions;
    eventBatchSize?: number;
    eventFlushInterval?: number;
    eventMaxQueueSize?: number;
    sdkKey?: string;
    odpOptions?: OdpOptions;
    persistentCacheProvider?: PersistentCacheProvider;
  }
  logLevel?: 'critical' | 'error' | 'warning' | 'debug' | 'info'
  userData?: {
    userId?: string
    attributes?: {
      [name: string]: UserAttributeValue
    }
  }
  defaultFeatureFlags?: Flags
  includeFlagVariables?: boolean
  decideOptions?: OptimizelyDecideOption[]

userId serves as optimizely user identity. attributes is a map of custom key-value pairs specifying attributes for the user that are used for audience targeting. Detailed information can be found in the Optimizely docs defaultFeatureFlags are used in the event that optimizely doesn't load up and initial values are required. logLevel serves to configure the optimizely logger if you wish to use it. It accepts critical, error, warning, debug or info decideOptions is an array of OptimizelyDecideOption enums. Detailed information can be found in the Optimizely docs includeFlagVariables with default of false can be used when you need additional flag information like variationKey, enabled and variables.

1.11.0

8 months ago

0.0.0-13f98ebc0

8 months ago

1.11.0-alpha.0

8 months ago

0.0.0-8b1c9a58f

10 months ago

0.0.0-7aee3c884

9 months ago

0.0.0-6b630b3db

10 months ago

0.0.0-f90a1dfc4

10 months ago

0.0.0-cffdc2dcc

10 months ago

0.0.0-b7fba40c6

10 months ago

1.9.1-alpha.0

11 months ago

0.0.0-440086b4

11 months ago

0.0.0-e16014a0

12 months ago

0.0.0-0bbd8005

11 months ago

0.0.0-42901200

11 months ago

0.0.0-e67c7c42

11 months ago

1.10.0-alpha.0

11 months ago

0.0.0-1cbb2a4f

11 months ago

1.8.3-alpha.0

12 months ago

0.0.0-d55b7d88

12 months ago

1.9.1

11 months ago

1.9.0

12 months ago

1.8.2-alpha.0

12 months ago

0.0.0-2b93091d

11 months ago

1.10.0

11 months ago

0.0.0-cc29eac2

11 months ago

1.8.1

1 year ago

1.8.0

1 year ago

1.8.1-alpha.0

1 year ago

1.8.1-alpha.1

12 months ago

1.9.0-alpha.0

12 months ago

0.0.0-767f0ff7

12 months ago

0.0.0-c58b97af

11 months ago

0.0.0-1fc471ad

12 months ago

1.8.0-alpha.0

1 year ago

0.0.0-014af081

11 months ago

1.8.3

12 months ago

1.6.0

2 years ago

1.6.0-alpha.1

2 years ago

1.6.0-alpha.0

2 years ago

1.7.0-alpha.0

2 years ago

1.7.0

2 years ago

1.5.0

2 years ago

1.5.0-alpha.0

2 years ago

1.4.11-alpha.0

2 years ago

1.4.11

2 years ago

1.4.0-alpha.0

3 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.2-alpha.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.4.5-alpha.0

3 years ago

1.4.3-alpha.0

3 years ago

1.4.9-alpha.0

2 years ago

1.4.7-alpha.0

2 years ago

1.4.1-alpha.0

3 years ago

1.4.6-alpha.0

3 years ago

1.4.4-alpha.0

3 years ago

1.4.10-alpha.0

2 years ago

1.4.9

2 years ago

1.4.10

2 years ago

1.4.7

2 years ago

1.4.8-alpha.1

2 years ago

1.4.8-alpha.0

2 years ago

1.4.8-alpha.2

2 years ago

1.3.0-alpha.0

3 years ago

1.3.0

3 years ago

1.2.0

3 years ago

1.0.0

3 years ago

1.0.0-alpha.3

3 years ago

1.0.0-alpha.2

3 years ago

1.0.0-alpha.1

3 years ago

1.0.0-alpha.0

3 years ago

1.1.1-alpha.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.2.0-alpha.0

3 years ago

1.1.0-alpha.0

3 years ago

0.25.1-alpha.0

3 years ago

0.25.1-alpha.1

3 years ago

0.24.1-alpha.0

3 years ago

0.26.0-alpha.0

3 years ago

0.26.0-alpha.1

3 years ago

0.26.0-alpha.2

3 years ago

0.25.1

3 years ago

0.25.0

3 years ago

0.25.0-alpha.0

3 years ago

0.26.0

3 years ago

0.24.1

3 years ago

0.23.0-alpha.0

3 years ago

0.23.0-alpha.1

3 years ago

0.24.0-alpha.0

3 years ago

0.24.0

3 years ago

0.23.0

3 years ago

0.17.0-alpha.0

3 years ago

0.21.0

3 years ago

0.19.0-alpha.0

3 years ago

0.20.0

3 years ago

0.19.0-alpha.1

3 years ago

0.22.0-alpha.0

3 years ago

0.16.0

3 years ago

0.15.1

3 years ago

0.20.0-alpha.1

3 years ago

0.18.0

3 years ago

0.21.0-alpha.0

3 years ago

0.22.0

3 years ago

0.12.0

3 years ago

0.13.0

3 years ago

0.14.0

3 years ago

0.15.0

3 years ago

0.11.0

3 years ago

0.10.0

3 years ago

0.10.1

3 years ago

0.9.0

3 years ago

0.8.1

3 years ago

0.10.2

3 years ago

0.8.0

3 years ago

0.5.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.3.0

4 years ago

0.1.2

4 years ago

0.2.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.4.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago