Skip to content

Commit

Permalink
feat: cwv audits (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney authored Nov 24, 2023
1 parent 16af6d6 commit 6cf6045
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/cwv/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -48,6 +49,8 @@ function processRUMResponse(respJson) {
.map((row) => ({
url: row.url,
pageviews: row.pageviews,
avgcls: row.avgcls,
avginp: row.avginp,
avglcp: row.avglcp,
}));
}
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions test/audits/cwv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Index Tests', () => {
type: 'cwv',
url: 'adobe.com',
auditContext: {
key: 'value',
finalUrl: 'adobe.com',
},
};
context = {
Expand Down Expand Up @@ -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);

Expand All @@ -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');
});
});
6 changes: 6 additions & 0 deletions test/rum-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
];
Expand Down

0 comments on commit 6cf6045

Please sign in to comment.