From 08566492db455043bca889f1f0fb293fb72c34d0 Mon Sep 17 00:00:00 2001 From: ekremney Date: Mon, 2 Sep 2024 11:51:08 +0200 Subject: [PATCH] fix(cwv): error handling --- src/cwv/handler.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/cwv/handler.js b/src/cwv/handler.js index 78d255b3..9576befe 100644 --- a/src/cwv/handler.js +++ b/src/cwv/handler.js @@ -13,6 +13,7 @@ import RUMAPIClient from '@adobe/spacecat-shared-rum-api-client'; 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 @@ -20,13 +21,28 @@ const INTERVAL = 7; // days export async function CWVRunner(auditUrl, context, site) { const rumAPIClient = RUMAPIClient.createFrom(context); const domainkey = await getRUMDomainkey(site.getBaseURL(), context); - const options = { + let options = { domain: auditUrl, domainkey, interval: INTERVAL, granularity: 'hourly', }; - const cwvData = await rumAPIClient.query('cwv', options); + 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 auditResult = { cwv: cwvData.filter((data) => data.pageviews >= DAILY_THRESHOLD * INTERVAL), auditContext: {