Hello, I’m hoping to setup some A/B tests on our N...
# ask-questions
h
Hello, I’m hoping to setup some A/B tests on our NextJS application and found this article from @future-teacher-7046 from about 2 years ago https://dev.to/jdorn/ab-testing-with-the-new-nextjs-12-middleware-54j6 Any updated recommendations on how to setup a test like this?
f
Hi Josh
it still largely works - are you doing any edge worker stuff?
h
Thanks for the response @fresh-football-47124, we are not!
It seems like instead of administering your own “stale time” via the
getFeatures
function
Copy code
if (Date.now() - lastFetch > 5000) {
maybe we can do a the same using
options.next.revalidate
on NextJS
fetch
? https://nextjs.org/docs/app/api-reference/functions/fetch
f
@future-teacher-7046 thoughts?
f
That article is a little out of date, although the core concepts still work. The latest Javascript SDK has support for built-in fetching and caching. https://docs.growthbook.io/lib/js#built-in-fetching-and-caching We use localStorage by default as a caching layer, which won't work in middleware, but there are 2 ways around this: 1. Specify a
localStorage
polyfill possibly using something like Edge Config 2. Specify a
fetch
polyfill that adds the Next.js-specific revalidate option to every request The Node.js instructions show examples of setting these polyfills, but they aren't specific to Next.js middleware, so will likely need some tweaking: https://docs.growthbook.io/lib/js#nodejs
h
Got it, it seems like
instrumentation.ts
would probably be the place to set those polyfills https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation?