Skip to content

Commit

Permalink
fix(cwv): audit url handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ekremney committed Sep 2, 2024
1 parent 0856649 commit 33793d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 22 deletions.
33 changes: 13 additions & 20 deletions src/cwv/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,30 @@
*/

import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client';
import URI from 'urijs';
import { hasText } from '@adobe/spacecat-shared-utils';
import { getRUMDomainkey } from '../support/utils.js';
import { AuditBuilder } from '../common/audit-builder.js';
import { hasNonWWWSubdomain } from '../experimentation/handler.js';

const DAILY_THRESHOLD = 1000;
const INTERVAL = 7; // days

export function getAuditUrl(baseURL) {
const uri = new URI(baseURL);
return hasText(uri.subdomain()) ? baseURL.replace(/https?:\/\//, '') : baseURL.replace(/https?:\/\//, 'www.');
}

export async function CWVRunner(auditUrl, context, site) {
const rumAPIClient = RUMAPIClient.createFrom(context);
const domainkey = await getRUMDomainkey(site.getBaseURL(), context);
let options = {
domain: auditUrl,
const finalUrl = getAuditUrl(auditUrl);
const options = {
domain: finalUrl,
domainkey,
interval: INTERVAL,
granularity: 'hourly',
};
let cwvData;

try {
cwvData = await rumAPIClient.query('cwv', options);
/* c8 ignore start */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (e) {
if (!hasNonWWWSubdomain(auditUrl) && !auditUrl.toLowerCase().startsWith('www')) {
options = {
...options,
domain: `www.${auditUrl}`,
};
cwvData = await rumAPIClient.query('cwv', options);
}
}
/* c8 ignore stop */
const cwvData = await rumAPIClient.query('cwv', options);
const auditResult = {
cwv: cwvData.filter((data) => data.pageviews >= DAILY_THRESHOLD * INTERVAL),
auditContext: {
Expand All @@ -52,10 +44,11 @@ export async function CWVRunner(auditUrl, context, site) {

return {
auditResult,
fullAuditRef: auditUrl,
fullAuditRef: finalUrl,
};
}

export default new AuditBuilder()
.withUrlResolver((site) => site.getBaseURL())
.withRunner(CWVRunner)
.build();
13 changes: 11 additions & 2 deletions test/audits/cwv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import sinon from 'sinon';
import sinonChai from 'sinon-chai';
import nock from 'nock';
import { createSite } from '@adobe/spacecat-shared-data-access/src/models/site.js';
import { CWVRunner } from '../../src/cwv/handler.js';
import { CWVRunner, getAuditUrl } from '../../src/cwv/handler.js';
import { rumData } from '../fixtures/rum-data.js';

use(sinonChai);
Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Index Tests', () => {
});

it('cwv audit runs rum api client cwv query', async () => {
const result = await CWVRunner('www.spacecat.com', context, site);
const result = await CWVRunner('https://spacecat.com', context, site);
expect(result).to.deep.equal({
auditResult: {
cwv: rumData.filter((data) => data.pageviews >= 7000),
Expand All @@ -70,4 +70,13 @@ describe('Index Tests', () => {
fullAuditRef: auditUrl,
});
});

it('audit url calculated correctly', async () => {
expect(getAuditUrl('http://spacecat.com')).to.equal('www.spacecat.com');
expect(getAuditUrl('https://spacecat.com')).to.equal('www.spacecat.com');
expect(getAuditUrl('http://www.spacecat.com')).to.equal('www.spacecat.com');
expect(getAuditUrl('https://www.spacecat.com')).to.equal('www.spacecat.com');
expect(getAuditUrl('http://blog.spacecat.com')).to.equal('blog.spacecat.com');
expect(getAuditUrl('https://blog.spacecat.com')).to.equal('blog.spacecat.com');
});
});

0 comments on commit 33793d3

Please sign in to comment.