Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Randomize the urls when we run Lighthouse #25

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 6 additions & 16 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import BrowserRunner from "./BrowserRunner.js"
import LighthouseRunner from "./LighthouseRunner.js"
import Datadog from "./DatadogClient.js"
import {v1, v2} from '@datadog/datadog-api-client'
import fs from 'node:fs'
import os from 'node:os'
import type { Flags, Result, Config } from 'lighthouse'
import lhConfig from '@config/lh-config.js';
Expand Down Expand Up @@ -65,19 +64,10 @@ function retrieveDataPointsForAudits(results: Result, audits: string[]) {
/**
* Adds `rand={123..}` param to the urls to ensure we never hit a CDN cache.
*/
function addRandomParamToUrl(inspectList: Record<string, string[]>): Record<string, string[]> {
const updatedInspectList: Record<string, string[]> = structuredClone(inspectList);

for (const pageType in updatedInspectList) {
const urls = updatedInspectList[pageType];

updatedInspectList[pageType] = urls.map(url => {
const urlObject = new URL(url);
urlObject.searchParams.append('rand', Math.floor(Math.random() * 1000000).toString());
return urlObject.toString();
});
}
return updatedInspectList;
function addRandomParamToUrl(url: string): string {
const urlObject = new URL(url);
urlObject.searchParams.append('rand', Math.floor(Math.random() * 1000000).toString());
return urlObject.toString();
}

async function sendMetricsToDatadog(metricName: string, dataPoints: v2.MetricPoint[], tags: Record<string, string>, metadata?: v1.MetricMetadata) {
Expand All @@ -100,8 +90,9 @@ async function captureLighthouseMetrics(pageType: string, url: string, audits: s

const formFactor = config.settings?.formFactor || 'mobile'
console.log(`Running Lighthouse for ${url} with form factor: ${formFactor}`);
const randUrl = addRandomParamToUrl(url);
const page = await browserRunner.start()
const results = await lighthouseRunner.run(url, {...options}, config, page)
const results = await lighthouseRunner.run(randUrl, {...options}, config, page)

await browserRunner.stop()

Expand Down Expand Up @@ -134,7 +125,6 @@ async function captureLighthouseMetrics(pageType: string, url: string, audits: s
}
(async() => {
let inspectList: Record<string, string[]> = URLS;
inspectList = addRandomParamToUrl(inspectList);

const audits = lhConfig.settings.onlyAudits || []

Expand Down
Loading