Naive Puppets Marching

    TypeScript icon, indicating that this package has built-in type declarations

    0.23.0 • Public • Published


    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() 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. To do so you will need to initialize the package by calling createFeatureFlagsInstance with the sdkKey from optimizely. 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. Optionally the config can be switched to suit your need. More on this can be found here.

    An example implementation would be as follows:


    import {getFeatureFlags, createFeatureFlagsInstance} from '@newskit-render/feature-flags';
    export async function getServerSideProps(context) {
      // code
      createFeatureFlagsInstance({ optimizelyConfig: { sdkKey: process.env.OPTIMIZELY_SDK_KEY } })
      // code
      const [resultFromOtherQueries, featureFlagsResult] = await Promise.allSettled([
         // other queries,
      const featureFlags = featureFlagsResult.value;
      return {
        props: {
          // other data


    import React from 'react'
    import { FeatureFlag } from '@newskit-render/feature-flags'
    const SectionPage: React.FC<{
      page: Page
      // pageprops...
      featureFlags?: FeatureFlag[]
    }> = ({ page, pageprops..., featureFlags }) => {
            {featureFlags && featureFlags['test_flag'] && <p>FEATURE FLAG IS HEREEE</p>}
              // ...content
    export default SamplePage

    hooks in getInitialProps

    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.


    import { getFeatureFlags, FeatureFlagsContextProvider, FeatureFlag, createFeatureFlagsInstance } from '@newskit-render/feature-flags';
    function MyApp({ featureFlags }: {featureFlags: FeatureFlag[]}) {
      return (
        <FeatureFlagsContextProvider context={ featureFlags } >
          <App />
    MyApp.getInitialProps = async () => {
        createFeatureFlagsInstance({ optimizelyConfig: { sdkKey: process.env.OPTIMIZELY_SDK_KEY } })
      const featureFlags = await getFeatureFlags();
      return { featureFlags }
    export default MyApp


    import { useFeatureFlagsContext } from '@newskit-render/feature-flags';
    const Header: React.FC<{ user: UserData }> = ({ user }) => {
      const featureFlags = useFeatureFlagsContext();
      return (
              <Cell xs={12}>
                  // ...nav buttons
                  {featureFlags && featureFlags['test_flag'] && <p>FEATURE FLAGG</p>}
                  // ...nav buttons
    export default Header


    createFeatureFlagsInstance takes config object as parameter. The config object consists of optimizelyConfig, userId, defaultFeatureFlags, logLevel. The only requirement for the feature flags to be instantiated is passing optimizely sdk key to optimizelyConfig. Further, the whole config can be changed to suit your needs. userId serves as optimizely user identity. 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




    npm i @newskit-render/feature-flags

    DownloadsWeekly Downloads






    Unpacked Size

    66.7 kB

    Total Files


    Last publish


    • newskit