A/B Testing Middleware
Hello everyone,
I'm wanting to do some A/B Testing on Vercel. The guide is for NextJS (https://vercel.com/blog/ab-testing-with-nextjs-and-vercel#experimenting-at-the-edge).
So I'm looking to convert this into Nuxt, the only thing I can't seem to find any information on is how to set a cookie in a Nuxt Middleware. I've attempted to use
useCookie()
.Solution:Jump to solution
Okay so I've found a solution:
/middleware/ab-test.global.ts
```js
const COOKIE_NAME = "ab_variant";
const THRESHOLD = 0.5;...4 Replies
It seems there is
/middleware
and /server/middleware
I'm not entirely sure of the difference but I've come up:
Then:
Which works, okay, but on the initial load when it sets the cookie I'm getting a Hydration text content mismatch
refreshing after that works as expected.Solution
Okay so I've found a solution:
/middleware/ab-test.global.ts
Then composables/useVariant.ts
I'm unsure whether this is the best solution, but it seems to be working and SSR friendly in my tests.lgtm π
Pretty simple in the end and is much less code that Next. It actually makes plenty of sense but I had it in my head that useCookie was not accessible inside
defineNuxtRouteMiddleware
, I limited myself from the very beginning π
turns out I was just missing the .global