https://www.growthbook.io/ logo
#ask-questions
Title
# ask-questions
h

high-answer-43473

02/01/2024, 5:23 PM
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

fresh-football-47124

02/01/2024, 5:23 PM
Hi Josh
it still largely works - are you doing any edge worker stuff?
h

high-answer-43473

02/01/2024, 5:35 PM
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

fresh-football-47124

02/01/2024, 6:17 PM
@future-teacher-7046 thoughts?
f

future-teacher-7046

02/01/2024, 6:49 PM
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

high-answer-43473

02/01/2024, 7:36 PM
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?
3 Views