Skip to content

Commit

Permalink
Rename 'page metrics' to 'URL metrics'
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 15, 2023
1 parent 95d9406 commit 9876aa4
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 161 deletions.
12 changes: 6 additions & 6 deletions modules/images/image-loading-optimization/detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ function ilo_print_detection_script() {
}

$query_vars = ilo_get_normalized_query_vars();
$slug = ilo_get_page_metrics_slug( $query_vars );
$slug = ilo_get_url_metrics_slug( $query_vars );
$microtime = microtime( true );

// Abort if we already have all the sample size we need for all breakpoints.
$needed_minimum_viewport_widths = ilo_get_needed_minimum_viewport_widths_now_for_slug( $slug );
if ( ! ilo_needs_page_metric_for_breakpoint( $needed_minimum_viewport_widths ) ) {
if ( ! ilo_needs_url_metric_for_breakpoint( $needed_minimum_viewport_widths ) ) {
return;
}

Expand All @@ -50,12 +50,12 @@ function ilo_print_detection_script() {
'serveTime' => $microtime * 1000, // In milliseconds for comparison with `Date.now()` in JavaScript.
'detectionTimeWindow' => $detection_time_window,
'isDebug' => WP_DEBUG,
'restApiEndpoint' => rest_url( ILO_REST_API_NAMESPACE . ILO_PAGE_METRICS_ROUTE ),
'restApiEndpoint' => rest_url( ILO_REST_API_NAMESPACE . ILO_URL_METRICS_ROUTE ),
'restApiNonce' => wp_create_nonce( 'wp_rest' ),
'pageMetricsSlug' => $slug,
'pageMetricsNonce' => ilo_get_page_metrics_storage_nonce( $slug ),
'urlMetricsSlug' => $slug,
'urlMetricsNonce' => ilo_get_url_metrics_storage_nonce( $slug ),
'neededMinimumViewportWidths' => $needed_minimum_viewport_widths,
'storageLockTTL' => ilo_get_page_metric_storage_lock_ttl(),
'storageLockTTL' => ilo_get_url_metric_storage_lock_ttl(),
);
wp_print_inline_script_tag(
sprintf(
Expand Down
36 changes: 18 additions & 18 deletions modules/images/image-loading-optimization/detection/detect.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function error( ...message ) {
*/

/**
* @typedef {Object} PageMetrics
* @typedef {Object} URLMetrics
* @property {string} url - URL of the page.
* @property {Object} viewport - Viewport.
* @property {number} viewport.width - Viewport width.
Expand Down Expand Up @@ -137,11 +137,11 @@ function getBreadcrumbs( leafElement ) {
}

/**
* Checks whether the page metric(s) for the provided viewport width is needed.
* Checks whether the URL metric(s) for the provided viewport width is needed.
*
* @param {number} viewportWidth - Current viewport width.
* @param {Array<number, boolean>[]} neededMinimumViewportWidths - Needed minimum viewport widths, in ascending order.
* @return {boolean} Whether page metrics are needed.
* @return {boolean} Whether URL metrics are needed.
*/
function isViewportNeeded( viewportWidth, neededMinimumViewportWidths ) {
let lastWasNeeded = false;
Expand Down Expand Up @@ -176,26 +176,26 @@ function getCurrentTime() {
* @param {boolean} args.isDebug Whether to show debug messages.
* @param {string} args.restApiEndpoint URL for where to send the detection data.
* @param {string} args.restApiNonce Nonce for writing to the REST API.
* @param {string} args.pageMetricsSlug Slug for page metrics.
* @param {string} args.pageMetricsNonce Nonce for page metrics storage.
* @param {Array<number, boolean>[]} args.neededMinimumViewportWidths Needed minimum viewport widths for page metrics.
* @param {number} args.storageLockTTL The TTL (in seconds) for the page metric storage lock.
* @param {string} args.urlMetricsSlug Slug for URL metrics.
* @param {string} args.urlMetricsNonce Nonce for URL metrics storage.
* @param {Array<number, boolean>[]} args.neededMinimumViewportWidths Needed minimum viewport widths for URL metrics.
* @param {number} args.storageLockTTL The TTL (in seconds) for the URL metric storage lock.
*/
export default async function detect( {
serveTime,
detectionTimeWindow,
isDebug,
restApiEndpoint,
restApiNonce,
pageMetricsSlug,
pageMetricsNonce,
urlMetricsSlug,
urlMetricsNonce,
neededMinimumViewportWidths,
storageLockTTL,
} ) {
const currentTime = getCurrentTime();

// As an alternative to this, the ilo_print_detection_script() function can short-circuit if the
// ilo_is_page_metric_storage_locked() function returns true. However, the downside with that is page caching could
// ilo_is_url_metric_storage_locked() function returns true. However, the downside with that is page caching could
// result in metrics being missed being gathered when a user navigates around a site and primes the page cache.
if ( isStorageLocked( currentTime, storageLockTTL ) ) {
if ( isDebug ) {
Expand Down Expand Up @@ -227,7 +227,7 @@ export default async function detect( {

if ( ! isViewportNeeded( win.innerWidth, neededMinimumViewportWidths ) ) {
if ( isDebug ) {
log( 'No need for page metrics from the current viewport.' );
log( 'No need for URL metrics from the current viewport.' );
}
return;
}
Expand Down Expand Up @@ -354,11 +354,11 @@ export default async function detect( {
log( 'Detection is stopping.' );
}

/** @type {PageMetrics} */
const pageMetrics = {
/** @type {URLMetrics} */
const urlMetrics = {
url: win.location.href,
slug: pageMetricsSlug,
nonce: pageMetricsNonce,
slug: urlMetricsSlug,
nonce: urlMetricsNonce,
viewport: {
width: win.innerWidth,
height: win.innerHeight,
Expand Down Expand Up @@ -396,11 +396,11 @@ export default async function detect( {
boundingClientRect: elementIntersection.boundingClientRect,
};

pageMetrics.elements.push( elementMetrics );
urlMetrics.elements.push( elementMetrics );
}

if ( isDebug ) {
log( 'Page metrics:', pageMetrics );
log( 'URL metrics:', urlMetrics );
}

// TODO: Wait until idle? Yield to main?
Expand All @@ -411,7 +411,7 @@ export default async function detect( {
'Content-Type': 'application/json',
'X-WP-Nonce': restApiNonce,
},
body: JSON.stringify( pageMetrics ),
body: JSON.stringify( urlMetrics ),
} );

if ( response.status === 200 ) {
Expand Down
Loading

0 comments on commit 9876aa4

Please sign in to comment.