Debugging nextjs middleware functions on deployed site

I’m trying to implement A/B Testing on a Nextjs site using middleware functions.

The issue I’m facing is that it seems like the middleware function doesnt run on every request to the given page (on the deployed site). e.g. I visit the URL, the middleware sets the cookie and rewrites the page (good), then I delete the cookie, and repeat the process but the cookie is no longer being set (bad).

Now I’ve tried to isolate the problem with a reproduction abtest-middleware.netlify.app.
Works perfectly, everything as expected, cookies get written after a clear and refresh (giving a 50/50 chance of seeing a test layout or original).
GitHub repo is here.

So my question is how can I debug this issue? Everything works fine locally using netlify dev, everything works fine on the reproduction, but the deployed site has issues. Is there a way to view logs from the middleware to figure out where things might be going wrong?

Cheers
Jim

Ok I’ve been able to reproduce the issue with dynamic routing. Will make a Github report now I’ve isolated the issue.

[Bug]: middleware on dynamic routes (deployed) · Issue #1168 · netlify/next-runtime · GitHub