Skip to content

Freshness TTL should be able to be disabled in favor of using ETag alone to determine whether URL Metric is stale #1914

Description

@westonruter

Feature Description

This comes out of #1913 where my original solution was to fix test_get_lcp_element_when_group_half_stale was to provide a freshness TTL of -1 to disable it from consideration:

diff --git a/plugins/optimization-detective/tests/test-class-od-url-metrics-group.php b/plugins/optimization-detective/tests/test-class-od-url-metrics-group.php
index 209bb2b26..f1bd26548 100644
--- a/plugins/optimization-detective/tests/test-class-od-url-metrics-group.php
+++ b/plugins/optimization-detective/tests/test-class-od-url-metrics-group.php
@@ -469,7 +469,7 @@ class Test_OD_URL_Metric_Group extends WP_UnitTestCase {
 			$current_etag,
 			array( 480, 600, 782 ),
 			3,
-			WEEK_IN_SECONDS
+			-1
 		);
 
 		$this->assertFalse( $collection->is_every_group_complete() );

This, however, fails because the constructor requires a positive integer:

// Set freshness TTL.
if ( $freshness_ttl < 0 ) {
throw new InvalidArgumentException(
esc_html(
sprintf(
/* translators: %d is the invalid sample size */
__( 'Freshness TTL must be at least zero, but provided: %d', 'optimization-detective' ),
$freshness_ttl
)
)
);
}

Nevertheless, now that there is an ETag which is taken into account for considering whether a URL Metric is fresh (#1466), it would make sense to provide an option for a site to disregard the "freshness TTL" altogether when determining whether a URL Metric is stale:

// The URL Metric is too old to be fresh.
if ( $current_time > $url_metric->get_timestamp() + $this->freshness_ttl ) {
return false;
}

In other words, it should be possible for a site to do this:

add_filter( 'od_url_metric_freshness_ttl', function () { return -1; } );

But this is currently also blocked from being allowed:

$freshness_ttl = (int) apply_filters( 'od_url_metric_freshness_ttl', WEEK_IN_SECONDS );
if ( $freshness_ttl < 0 ) {
_doing_it_wrong(
__FUNCTION__,
esc_html(
sprintf(
/* translators: %s is the TTL freshness */
__( 'Freshness TTL must be at least zero, but saw "%s".', 'optimization-detective' ),
$freshness_ttl
)
),
''
);
$freshness_ttl = 0;
}

Allowing the freshness TTL to be disabled will allow URL Metrics to remain fresh indefinitely as long as the ETags do not change, that is, if the state of the site does not change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fields

    No fields configured for Enhancement.

    Projects

    Status
    Done 😃

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions