DEV Community

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

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!