-
Notifications
You must be signed in to change notification settings - Fork 104
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
Mark existing URL Metrics as stale when a new tag visitor is registered #1705
Mark existing URL Metrics as stale when a new tag visitor is registered #1705
Conversation
…on` instead of a global variable
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Code in question: performance/tests/class-optimization-detective-test-helpers.php Lines 64 to 116 in 69eca9a
@westonruter, while working on this PR, I came across this code and had a quick question. On line 93, shouldn’t it use Just thought I’d flag it while I noticed it—thanks! |
You're right! That seems to be an oversight in the tests. |
@@ -46,6 +46,11 @@ function od_register_endpoint(): void { | |||
'required' => true, | |||
'pattern' => '^[0-9a-f]{32}$', | |||
), | |||
'eTag' => array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See other comment about calling this rather current_etag
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 371e998.
@@ -539,6 +541,7 @@ export default async function detect( { | |||
|
|||
const url = new URL( restApiEndpoint ); | |||
url.searchParams.set( 'slug', urlMetricSlug ); | |||
url.searchParams.set( 'eTag', currentETag ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a bit confusing that this is here rather than (also?) added as urlMetric.eTag
(or urlMetric.etag
). In the end they will need to be identical of course, so it does make sense. Just an observation. Maybe this should be called current_etag
to better disambiguate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed the arg in 371e998.
@@ -208,6 +210,12 @@ public static function get_json_schema(): array { | |||
'required' => true, | |||
'readonly' => true, // Omit from REST API. | |||
), | |||
'eTag' => array( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should make this just 'etag'
. In HTTP it is "ETag", and a lower case "eTag" feels like it would maybe be something else, like an "electronic tag" or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. Done in 08f8f8a.
* | ||
* @param string $slug Slug (hash of normalized query vars). | ||
* @param string $etag ETag. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider $current_etag
instead.
@@ -173,12 +173,13 @@ function od_get_url_metrics_storage_hmac( string $slug, string $url, ?int $cache | |||
* | |||
* @param string $hmac HMAC. | |||
* @param string $slug Slug (hash of normalized query vars). | |||
* @param string $etag ETag. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto on $current_etag
.
@@ -182,6 +188,7 @@ function od_handle_rest_request( WP_REST_Request $request ) { | |||
// Now supply the readonly args which were omitted from the REST API params due to being `readonly`. | |||
'timestamp' => microtime( true ), | |||
'uuid' => wp_generate_uuid4(), | |||
'eTag' => $request->get_param( 'eTag' ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is good. It makes sense that the ETag wouldn't be provided with the URL Metric object in the JSON body since it's not something that the client collects from the document. In this way it is similar to the uuid
and url
, although in theory the url
and etag
could be provided with in the urlMetric
object even though they will just be ignored.
@@ -216,10 +206,23 @@ function od_optimize_template_output_buffer( string $buffer ): string { | |||
*/ | |||
do_action( 'od_register_tag_visitors', $tag_visitor_registry ); | |||
|
|||
$visitors = iterator_to_array( $tag_visitor_registry ); | |||
$current_etag = implode( ',', array_keys( $visitors ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An important point here: similar to the slug
, i think the etag actually should be hashed:
$current_etag = implode( ',', array_keys( $visitors ) ); | |
$current_etag = md5( implode( ',', array_keys( $visitors ) ) ); |
The key reason here is that when expanding the scope here to address the parent issue #1466, there will be much more data to include in the ETag. So I suggest that there be a new function created, like od_compute_current_etag()
which takes $tag_visitor_registry
as an argument. For now it could look like:
function od_compute_current_etag( OD_Tag_Visitor_Registry $tag_visitor_registry ) {
$data = array(
'tag_visitors' => array_keys( iterator_to_array( $tag_visitor_registry ) ),
);
return md5( serialize( $data ) );
}
Then later when working on #1466 the $data
can be expanded to include things like the IDs and post_modified
times for the posts in the loop, what the current queried object is, and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 8fe38f7.
…with od_get_url_metrics_slug()
Co-authored-by: Shyamsundar Gadde <shyamgadde1602@gmail.com>
…nd to improve testability
@ShyamGadde I believe I've worked out a solution for the testing problem by introducing a filter for allowing extensions to customize the parameters that go into constructing an ETag. For #1466 this will be useful in case sites want to fine-tune what internal state of a page goes into constructing an ETag. |
plugins/optimization-detective/tests/test-cases/many-images.php
Outdated
Show resolved
Hide resolved
Signing off for the day. |
Co-authored-by: Weston Ruter <westonruter@google.com>
…ction constructor Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
Signed-off-by: Shyamsundar Gadde <shyamsundar.gadde@rtcamp.com>
… effects are needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whew! This was more involved than I expected. Great work!
Summary
Fixes #1424
Relevant technical choices
$current_etag
) onOD_URL_Metric_Group_Collection
.od_get_current_url_metrics_etag()
that generates the ETag for the current environment.od_current_url_metrics_etag_data
filter to allow sites to customize the internal page state used for computing the ETag. This will feed into Internal state of the page (e.g. queried posts or queried object) should factor into whether a stored URL metric is fresh #1466.