next-unleash
The goal of this module is to make it easy to use Unleash with next.js.
The main motivation is to make sure toggles are available both during Server-Side-Rendering (SSR) and during local page transitions.
The general idea is that Unleash Node.js SDK is used at the server side, and that all toggles are propagated as part of SSR.
This project is considered a POC. APIs and components might change
Not solved yet:
- Client side update of toggles. We should use SSR or similar to update the client toggles when changes are detected. Currently these changes are only picket up during reload (SSR).
- A wrapper component. It would provably be easier to use if there where some kind of react component you could use to wrap your components.
- Figure out if there are ways to configure the middleware with next.js so that users don't have to introduce custom server.
How to use
Step 1: Express middleware setup
First you will need to configure a custom-server with next.js in order to make the toggles available during SSR.
//server.js const express = ;const next = ; const port = || 3000;const dev = processenvNODE_ENV !== 'production';const app = ;const initialize isEnabled = ;const handle = app; ; app
Step 2: Wrap App with the Unleash provider
You will need to wrap your App
component with the unleash-provider.
//_app.js ;;;; ;; ; { superprops; thispageContext = ; } { // Remove the server-side injected CSS. const jssStyles = document; if jssStyles && jssStylesparentNode jssStylesparentNode; } { const Component pageProps = thisprops; return <Container> <Head> <title>My app</title> </Head> <Component pageContext=thispageContext ...pageProps /> </Container> ; } // Wrap the entire app with the unleash providerMyApp;
Step 3: Access toggles in component
When you want to access Unleash feature toggles in a component you can wrap the component using withUnleash
:
; ;;; Component { const toggles = thisprops; return <div> toggledemo ? <p>Enabled</p> : <p>disabled</p> </div> ; } MyComponentpropTypes = toggles: PropTypesobjectisRequired; MyComponent;