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.
@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!
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?
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!
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.
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
I'm a young black developer and I'm in love with tech. I love to discuss about tech with all kind of people. I'm also trying to teach, to give back what the tech community gave to me !
Hi!
Great guide, thanks.
This line
Probably is better written as
Original code work, but it's kinda confusing that sdk docs differ from the guide.
isOnis just a helper method and is used like this to make the code a little shorter: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?
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
middlewarefunction 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.
Awesome, yeah the problems that would be nice if solved are:
@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!
Dang that was fast. Thanks @jdorn! I appreciate you being so responsive!
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?
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!
Cool! and so quick post!!
Do you have a repo somewhere? I cant seem to get growthbook to work
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_newinstead of/new_homepageand I forgot anasyncin the function signature. It's possible those were causing issues for you.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
This is huge. Thanks for sharing
Great article, keep it up!!!
Wow, I didn't even know this could be possible with Next.js! I'm so impressed by their updates. Thanks for sharing. π€
Is it neccessary to block the variant pages from direct access so that a customer is not able to access them via URL?