DEV Community

A/B Testing with the new Next.js 12 Middleware

Jeremy Dorn on October 26, 2021

Vercel recently released Next.js 12 which adds a number of exciting performance improvements as well as a new beta feature - Middleware. Middlewar...
Collapse
 
amantel profile image
Amantel • Edited

Hi!
Great guide, thanks.

This line

  if (growthbook.feature("new-homepage").on) {
Enter fullscreen mode Exit fullscreen mode

Probably is better written as

if (growthbook.feature("new-homepage").isOn) {
Enter fullscreen mode Exit fullscreen mode

Original code work, but it's kinda confusing that sdk docs differ from the guide.

Collapse
 
jdorn profile image
Jeremy Dorn

isOn is just a helper method and is used like this to make the code a little shorter:

if (growthbook.isOn("new-homepage")) {
Enter fullscreen mode Exit fullscreen mode
Collapse
 
afzalh profile image
Afzal Hossain

Worked on first try but with one issue. Can't maintain sticky session when using with netlify. visitor_id/id is not sent to growthbook and it's kept locally and when we have multiple instance if the middleware one isn't aware of the other and the A/B testing version initially served don't stick and randomly switch to different version for the same user (assuming when the middleware instance changes).
Thinking about workarounds (e.g separate cookie for each feature) but then I probably can't use most of the SDK features anymore :(
Anyone has a nice solution to this issue?

Collapse
 
collinc777 profile image
Collin Caram

How can we keep the middleware fast and avoid making a call to the growthbook endpoint? Any way we can have that information cached locally?

Collapse
 
jdorn profile image
Jeremy Dorn

I don't believe Next.js provides any sort of persistent storage or cache for middleware yet. But, I do think you can do some in-memory caching. Basically, if you do the fetch outside of the exported middleware function and store the response in a variable, it should only run once at startup and be re-used across invocations. I haven't tested this with Vercel, but that's how Cloudflare workers and Lambda does it at least.

I can do some tests and update the article if works and ends up being better for performance.

Collapse
 
collinc777 profile image
Collin Caram

Awesome, yeah the problems that would be nice if solved are:

  1. how can we maintain optimal performance while doing a/b testing?
  2. how can we update a/b testing config in growthbook, and have that be reflected in the application?
Thread Thread
 
jdorn profile image
Jeremy Dorn

@collinc777 I updated the example code in the article to use in-memory caching. So now, the first request will wait for the API response, but subsequent ones will return immediately. And it will refresh in the background after 5 seconds to keep the features up-to-date.

Still some improvements to make this truly production ready, like setting a time-out for the API fetch, but it's much closer now. Thanks for the idea!

Thread Thread
 
collinc777 profile image
Collin Caram

Dang that was fast. Thanks @jdorn! I appreciate you being so responsive!

Collapse
 
valento profile image
valentin mundrov

I understand the Next 12.2.0 middleware runs on edge-runtime and won't let us fetch data without proxy (Prisma). Edge-runtime requires req to be passed in <1.5sec. or it times out. So, is fetching in middleware actually possible? Appreciate your effort!

Collapse
 
monarch profile image
Prajjwal

Cool! and so quick post!!

Collapse
 
iamludal profile image
Ludal πŸš€ • Edited

Wow, I didn't even know this could be possible with Next.js! I'm so impressed by their updates. Thanks for sharing. 🀝

Collapse
 
davidornelas11 profile image
david ornelas

Do you have a repo somewhere? I cant seem to get growthbook to work

Collapse
 
jdorn profile image
Jeremy Dorn

Hi David. What problems are you having?

I just found and fixed a couple typos in the middleware code in the article. It was using /homepage_new instead of /new_homepage and I forgot an async in the function signature. It's possible those were causing issues for you.

Collapse
 
davidornelas11 profile image
david ornelas • Edited

I have a cloud account on growthbook.io and it cant seem to connect to it to check whether to roll out a feature. Everything just says "unknownsource" on it

Collapse
 
christopher2k profile image
Christopher N. KATOYI

This is huge. Thanks for sharing

Collapse
 
anishde12020 profile image
Anish De

Great article, keep it up!!!

Collapse
 
kobetwo profile image
KobeTwo

Is it neccessary to block the variant pages from direct access so that a customer is not able to access them via URL?