Skip to content

Commit 16bde04

Browse files
committed
Add middleware to extract AB tests from query params in dev server
1 parent 16e3781 commit 16bde04

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { Handler } from 'express';
2+
import { validateAsFEArticle } from '../../../src/model/validate';
3+
4+
export const addQueryParamsToABTests: Handler = async (req, res, next) => {
5+
try {
6+
const frontendData = validateAsFEArticle(req.body);
7+
8+
const { config } = frontendData;
9+
10+
const queryParamsAb = req.query;
11+
12+
const filteredQuery: Record<string, string> = {};
13+
for (const [key, value] of Object.entries(queryParamsAb)) {
14+
if (typeof value == 'string' && key.startsWith('ab-')) {
15+
const testId = key.replace(/^ab-/, '');
16+
filteredQuery[testId] = value;
17+
}
18+
}
19+
20+
config.serverSideABTests = {
21+
...config.serverSideABTests,
22+
...filteredQuery,
23+
};
24+
req.body = frontendData;
25+
} catch (error) {
26+
return next();
27+
}
28+
next();
29+
};

dotcom-rendering/src/server/server.dev.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
handleFootballMatchPage,
2121
handleFootballTablesPage,
2222
} from './handler.sportDataPage.web';
23+
import { addQueryParamsToABTests } from './lib/add-queryparams-to-abtests';
2324
import { getContentFromURLMiddleware } from './lib/get-content-from-url';
2425

2526
/** article URLs contain a part that looks like “2022/nov/25” */
@@ -89,6 +90,7 @@ const renderer = Router();
8990
// populates req.body with the content data from a production
9091
// URL if req.params.url is present
9192
renderer.use(getContentFromURLMiddleware);
93+
renderer.use(addQueryParamsToABTests);
9294
renderer.get('/Article/*url', handleArticle);
9395
renderer.get('/Interactive/*url', handleInteractive);
9496
renderer.get('/Blocks/*url', handleBlocks);

0 commit comments

Comments
 (0)