diff --git a/example-manifests/measure-webpage.yml b/example-manifests/measure-webpage.yml index b5eb302..352a465 100644 --- a/example-manifests/measure-webpage.yml +++ b/example-manifests/measure-webpage.yml @@ -12,8 +12,9 @@ initialize: method: WebpageImpact path: '@tngtech/if-webpage-plugins' config: - scrollToBottom: true url: https://www.tngtech.com + scrollToBottom: true + computeReloadRati: true 'co2js': method: Co2js path: '@tngtech/if-webpage-plugins' diff --git a/src/__tests__/unit/lib/webpage-impact/index.test.ts b/src/__tests__/unit/lib/webpage-impact/index.test.ts index 5c96128..7c44e83 100644 --- a/src/__tests__/unit/lib/webpage-impact/index.test.ts +++ b/src/__tests__/unit/lib/webpage-impact/index.test.ts @@ -91,7 +91,7 @@ describe('lib/webpage-impact', () => { jest.spyOn(Date, 'now').mockImplementation(() => mockTimestamp); const webpageImpact = WebpageImpact( - {url: 'http://localhost:3000'}, + {url: 'http://localhost:3000', computeReloadRatio: true}, {}, {} ); diff --git a/src/lib/webpage-impact/README.md b/src/lib/webpage-impact/README.md index 702f58c..808f9fc 100644 --- a/src/lib/webpage-impact/README.md +++ b/src/lib/webpage-impact/README.md @@ -16,6 +16,7 @@ The `WebpageImpact` plugin measures the weight of a webpage in bytes and the wei The follwing config parameters are optional: +- `computeReloadRatio`: If true, a heuristic value for the `dataReloadRatio` is computed, which can be used as input for the co2js plugin. Any value for the `dataReloadRatio` that is provided as input will be overwritten. The data reload ratio expresses how much data has to be reloaded if the web page is visited a second time. - `mobileDevice:`: You can pick a mobile device to emulate. Must be one of puppeteer's known devices: https://pptr.dev/api/puppeteer.knowndevices - `emulateNetworkConditions`: You can pick one of puppeteer's predefined network conditions: https://pptr.dev/api/puppeteer.predefinednetworkconditions - `scrollToBottom`: If true, emulates a user scrolling to the bottom of the page (which loads all content that isn't loaded on initial load). If false, the page is not scrolled. Default: false. @@ -28,8 +29,7 @@ The follwing config parameters are optional: - `network/data/bytes`: page weight in bytes - `network/data/resources/bytes`: resource weights by category in bytes -- `dataReloadRatio`: an estimate of the percentage of data that is downloaded by return visitors (can be fed into the CO2.JS plugin) - if `options.dataReloadRatio` is already provided as an input, the plugin won't calculate it +- `dataReloadRatio`: If `computeReloadRatio` is true: estimate of the amount of data that is reloaded on return visits (Can be fed into the co2js plugin.) - `timestamp`: set to the time of the plugin execution - `duration`: set to 0 (because the request time does not seem of particular interest here to the author) @@ -72,6 +72,7 @@ initialize: path: '@tngtech/if-webpage-plugins' config: url: 'https://tngtech.com' + computeReloadRatio: true tree: children: child: diff --git a/src/lib/webpage-impact/index.ts b/src/lib/webpage-impact/index.ts index 32b13e7..6cc18c0 100644 --- a/src/lib/webpage-impact/index.ts +++ b/src/lib/webpage-impact/index.ts @@ -100,14 +100,8 @@ export const WebpageImpact = PluginFactory({ inputs.map(async input => { const startTime = Date.now(); - const computeReloadRatio = !input?.options?.dataReloadRatio; - const {pageWeight, resourceTypeWeights, dataReloadRatio} = - await measurePageImpactMetrics( - config.url, - computeReloadRatio, - config - ); + await measurePageImpactMetrics(config.url, config); const durationInSeconds = (Date.now() - startTime) / 1000; @@ -118,7 +112,7 @@ export const WebpageImpact = PluginFactory({ url: config.url, 'network/data/bytes': pageWeight, 'network/data/resources/bytes': resourceTypeWeights, - ...(dataReloadRatio + ...(config.computeReloadRatio && dataReloadRatio ? { options: { ...input.options, @@ -135,7 +129,6 @@ export const WebpageImpact = PluginFactory({ const WebpageImpactUtils = () => { const measurePageImpactMetrics = async ( url: string, - computeReloadRatio: boolean, config?: ConfigParams ) => { const requestHandler = (interceptedRequest: HTTPRequest) => { @@ -186,7 +179,7 @@ const WebpageImpactUtils = () => { }); let reloadedResources: Resource[] | undefined; - if (computeReloadRatio) { + if (config?.computeReloadRatio) { reloadedResources = await loadPageResources(page, url, { reload: true, cacheEnabled: true, @@ -377,6 +370,7 @@ const WebpageImpactUtils = () => { } const optionalConfigs = z.object({ + computeReloadRatio: z.boolean().optional(), timeout: z.number().gte(0).optional(), mobileDevice: z.string().optional(), emulateNetworkConditions: z.string().optional(), @@ -390,11 +384,6 @@ const WebpageImpactUtils = () => { .optional(), }) .optional(), - options: z - .object({ - dataReloadRatio: z.number().optional(), - }) - .optional(), }); const configSchema = z