Install Middleware

How it Works

Intelligems uses Next.js middleware to split and assign visitors into a Test Group for each active Test and adds this User Context to browser cookies.

  • If using the Route Hashing Method, this user context is passed to page props directly through a URL hash.

  • If using the Cookies Method, this user context is saved as cookies and read at response time.

Add Intelligems To Middleware

Example Configuration

import { IgMiddlewareHandler } from '@intelligems/headless/next'
import type { IgMiddlewareConfig } from '@intelligems/headless/next'

const igConfig: IgMiddlewareConfig = {
  settings: {
    enabled: true
    orgId: process.env.NEXT_PUBLIC_INTELLIGEMS_ORG_ID!,
    cacheIntervalMinutes: 5,
  },
  routes: {
    forceSkipRoutes: [
      '/api',
      '/_next',
      '/orders',
      '/profile',
      '/wishlist',
      '/favicon',
    ],
    getServerSidePropsRoutes: ['/', '/search'],
    getStaticPropsRoutes: ['/product/:path'],
  },
}

export async function middleware(req: NextRequest) {
  const igMiddlewareHandler = new IgMiddlewareHandler(req, igConfig)

  const res = NextResponse.next()
  await igMiddlewareHandler.setContext(res)
  return res
}