From f38ff78ba04af2d4b32204295fe489eb9ecdbb73 Mon Sep 17 00:00:00 2001 From: ekdogan Date: Fri, 24 Nov 2023 14:57:57 +0100 Subject: [PATCH] feat: cwv audits --- src/cwv/handler.js | 6 +++++- test/audits/cwv.test.js | 6 +++--- test/rum-data.js | 6 ++++++ 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/cwv/handler.js b/src/cwv/handler.js index 4ad653f1..e308fa89 100644 --- a/src/cwv/handler.js +++ b/src/cwv/handler.js @@ -34,7 +34,8 @@ const PAGEVIEW_THRESHOLD = 7000; export async function getRUMUrl(url) { const urlWithScheme = url.startsWith('http') ? url : `https://${url}`; const resp = await fetch(urlWithScheme); - return resp.url.split('://')[1]; + const finalUrl = resp.url.split('://')[1]; + return finalUrl.endsWith('/') ? finalUrl.slice(0, -1) : /* c8 ignore next */ finalUrl; } function filterRUMData(data) { @@ -48,6 +49,8 @@ function processRUMResponse(respJson) { .map((row) => ({ url: row.url, pageviews: row.pageviews, + avgcls: row.avgcls, + avginp: row.avginp, avglcp: row.avglcp, })); } @@ -62,6 +65,7 @@ export default async function auditCWV(message, context) { log.info(`Received audit req for domain: ${url}`); const finalUrl = await getRUMUrl(url); + auditContext.finalUrl = finalUrl; const params = { ...DEFAULT_PARAMS, diff --git a/test/audits/cwv.test.js b/test/audits/cwv.test.js index ef17a25a..351db037 100644 --- a/test/audits/cwv.test.js +++ b/test/audits/cwv.test.js @@ -36,7 +36,7 @@ describe('Index Tests', () => { type: 'cwv', url: 'adobe.com', auditContext: { - key: 'value', + finalUrl: 'adobe.com', }, }; context = { @@ -70,7 +70,7 @@ describe('Index Tests', () => { .query({ ...DEFAULT_PARAMS, domainkey: context.env.RUM_DOMAIN_KEY, - url: 'adobe.com/', + url: 'adobe.com', }) .reply(200, rumData); @@ -93,6 +93,6 @@ describe('Index Tests', () => { .reply(200); const finalUrl = await getRUMUrl('http://space.cat'); - expect(finalUrl).to.eql('space.cat/'); + expect(finalUrl).to.eql('space.cat'); }); }); diff --git a/test/rum-data.js b/test/rum-data.js index 29038322..cfa20df1 100644 --- a/test/rum-data.js +++ b/test/rum-data.js @@ -14,16 +14,22 @@ export const expectedAuditResult = [ { url: 'https://www.adobe.com/', pageviews: '36064271', + avgcls: 0.148, + avginp: 65, avglcp: 5239, }, { url: 'https://www.adobe.com/express/feature/image/remove-background', pageviews: '2737380', + avgcls: 0.129, + avginp: 95, avglcp: 1423, }, { url: 'https://www.adobe.com/express/', pageviews: '2703493', + avgcls: 0.028, + avginp: 79, avglcp: 1869, }, ];