next-intl rewrite url in middleware including lang code

101 views Asked by At

I am working on integrating experimentation into a next.js project making use of next-intl. I am looking to rewrite the URL if the user is within a bucket. Is there a way rewrite the url with next-intl middleware?

For example if a user visits /experiment I want to rewrite the URL to whatever bucket they fall into, e.g. /experiment/control or /experiment/test as an example. This also needs to work with /es/experiement to /es/experiment/control or /es/experiment/test.

I've tried rewriting based off of this official example from Next.js Statsig template, but obviously this doesn't account for the next-intl middleware setup. https://github.com/vercel/examples/blob/main/edge-middleware/ab-testing-statsig/middleware.ts

I've also tried a recommendation from https://stackoverflow.com/a/77194110/22770962

if (url.pathname === "/experiment") {
  const url = new URL(`/experiment/${bucket}`, request.url);
  response.headers.set("x-middleware-rewrite", url.toString());
}

But this 404s as well

0

There are 0 answers