npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@newskit-render/feature-flags

v1.7.0

Published

A package for utilizing feature flags for newskit-render consumers, based on optimizely's capabilities.

Downloads

833

Readme

@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() 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:

pages/index.ts

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,
     getFeatureFlags()
  ])

  const featureFlags = featureFlagsResult.value;

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

The package also provides a helper function that conbines getFeatureFlags, createFeatureFlagsInstance in one.

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

export async function getServerSideProps(context) {
  const [resultFromOtherQueries, featureFlags] = await Promise.allSettled([
     // other queries,
     initAndGetFeatureFlag(process.env.OPTIMIZELY_SDK_KEY)
  ])

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

samplePage/index.tsx

import React from 'react'
import { FeatureFlag } from '@newskit-render/feature-flags'

const SectionPage: React.FC<{
  page: Page
  // pageprops...
  featureFlags?: FeatureFlag[]
}> = ({ page, pageprops..., featureFlags }) => {

  return(
      <Layout>
        {featureFlags && featureFlags['test_flag'] && <p>FEATURE FLAG IS HEREEE</p>}
        <Cell>
          // ...content
        </Cell>
      </Layout>
 )}

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.

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

createFeatureFlagsInstance

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