Server-side Rendering Integration With Flagship [SSR]

This guide explains you how to use Flagship with nextJS using SSR mode.


Next.js is used for this example

For this example, we have used the Next.js framework.
If you need more examples, please contact us at [email protected]

How it works

In SSR, content is rendered server side on every page request during which we will fetch all flags targeting the visitor and passe them (flags) through a props to the __app.js component to feed the FlagshipProvider component.


  • Once your app is loaded on a browser, you can use Flagship SDK like in client side rendering mode


Please refer to this article link to set up a NextJS project.

Once your project is set up, you need to install and initialize the Flagship React SDK.

yarn add

We will customize the next component App to initialize the Flagship SDK.


import { FlagshipProvider } from ''
import '../styles/globals.css'

function MyApp({ Component, pageProps }) {
  const { initialVisitorData, initialFlagsData } = pageProps
  return (
      visitorData={initialVisitorData || {}}
      <Component {...pageProps} />


export default MyApp

We have 2 props in MyApp component, initialVisitorData and initialFlagsData. The first one contains visitor data and the second contains the FlagsData fetched from getServerSideProps at request of page to feed FlagshipProvider.

Here is the index page:


import {
} from "";
import styles from "../styles/Home.module.css";

export default function Home() {
  const fs = useFlagship();

  //get flag
  const myFlag = useFsFlag("my_flag_key", "default-value");

  const onSendHitClick = () => {
      type: HitType.EVENT,
      category: EventCategory.ACTION_TRACKING,
      action: "click button",

  return (
    <div className={styles.container}>
      <main className={styles.main}>
        <h1>Next ServerSide Rendering Integration With Flagship [SSR]</h1>
        <p>flag key: my_flag_key</p>
        <p>flag value: {myFlag.getValue()}</p>
          style={{ width: 100, height: 50 }}
          onClick={() => {
          Send hits

// This function runs only on the server side
export async function getServerSideProps(context) {
  const { res, req } = context;

  //Start the Flagship SDK
  const flagship = Flagship.start(
      fetchNow: false,

  //Get visitorId from cookies if exists
  const fs_visitorID_cookie = req.cookies["fs_visitorID_cookie"];

  const initialVisitorData = {
    id: fs_visitorID_cookie, // if not exists the SDK will generate one
    context: {
      any: "value",

  // Create a new visitor
  const visitor = flagship.newVisitor({
    context: initialVisitorData.context,

  // //Fetch flags
  await visitor.fetchFlags();

  //set cookie fs_visitorID_cookie
  res.setHeader("Set-Cookie", `fs_visitorID_cookie=${visitor.visitorId}`);

  // Pass data to the page via props
  return {
    props: {
      initialFlagsData: visitor.getFlagsDataArray(),
      initialVisitorData: {
        id: visitor.visitorId,

On the getServerSideProps of the index component page, we :

  • fetch the flags for the targeted visitor
  • pass the fetched flags to the page via props

Any feedback?
Want another tutorial?
Contact us

See full code here : github