AB test Middleware for Nextjs
This is a Node.js module available through the npm registry. Installation is done using the npm install command:
$ npm install nextjs-ab-test-middlewarevar activateAB = require("nextjs-ab-test-middleware");var express = require("express"); const next = require("next"); var activateAB = require("nextjs-ab-test-middleware"); const experiment = [ { name: "experiment-1", variants: [ { id: "a", weight: 50, }, { id: "b", weight: 50, }, ], customFilter(req, res) { return req.originalUrl.indexOf("US") !== -1; }, }, { name: "experiment-2", variants(req, res) { if (req.originalUrl.indexOf("US") !== -1) { return [ { id: "a", weight: 50, }, { id: "b", weight: 50, }, ]; } return [ { id: "a", weight: 100, }, { id: "b", weight: 0, }, ]; }, }, ]; const app = next({ dev }); app .prepare() .then(() => { const server = express(); server.use((req, res, next) => activateAB(req, res, next, experiment)); server.use((req, res, next) => { next(); }); server.listen(3000, (err) => { if (err) throw err; }); }) .catch((ex) => { console.error(ex.stack); // eslint-disable-line no-console process.exit(1); });