Skip to content

Commit 38a4c6b

Browse files
committed
Add tests and fix bug with adding links at finish action
1 parent 0d8cb65 commit 38a4c6b

File tree

12 files changed

+113
-20
lines changed

12 files changed

+113
-20
lines changed

plugins/optimization-detective/class-od-template-optimization-context.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
* @since n.e.x.t
1919
*
2020
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
21-
* @property-read OD_Tag_Visitor_Registry $tag_visitor_registry Tag visitor registry.
2221
* @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection.
2322
* @property-read array<string, mixed> $normalized_query_vars Normalized query vars.
2423
* @property-read non-empty-string $url_metrics_slug Slug for the od_url_metrics post.

plugins/optimization-detective/optimization.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,6 @@ function od_optimize_template_output_buffer( string $buffer ): string {
349349
$visited_tag_state->reset();
350350
} while ( $processor->next_tag( array( 'tag_closers' => 'skip' ) ) );
351351

352-
// Send any preload links in a Link response header and in a LINK tag injected at the end of the HEAD.
353-
if ( count( $link_collection ) > 0 ) {
354-
$response_header_links = $link_collection->get_response_header();
355-
if ( ! is_null( $response_header_links ) && ! headers_sent() ) {
356-
header( $response_header_links, false );
357-
}
358-
$processor->append_head_html( $link_collection->get_html() );
359-
}
360-
361352
// Inject detection script.
362353
// TODO: When optimizing above, if we find that there is a stored LCP element but it fails to match, it should perhaps set $needs_detection to true and send the request with an override nonce. However, this would require backtracking and adding the data-od-xpath attributes.
363354
if ( $needs_detection ) {
@@ -373,5 +364,15 @@ function od_optimize_template_output_buffer( string $buffer ): string {
373364
*/
374365
do_action( 'od_finish_template_optimization', $template_optimization_context );
375366

367+
// Send any preload links in a Link response header and in a LINK tag injected at the end of the HEAD.
368+
// Additional links may have been added at the od_finish_template_optimization action, so this must come after.
369+
if ( count( $link_collection ) > 0 ) {
370+
$response_header_links = $link_collection->get_response_header();
371+
if ( ! is_null( $response_header_links ) && ! headers_sent() ) {
372+
header( $response_header_links, false );
373+
}
374+
$processor->append_head_html( $link_collection->get_html() );
375+
}
376+
376377
return $processor->get_updated_html();
377378
}

plugins/optimization-detective/tests/test-cases/admin-bar/expected.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/tests/test-cases/complete-url-metrics/expected.html

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/tests/test-cases/many-images/expected.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/tests/test-cases/no-url-metrics/expected.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/tests/test-cases/noscript/expected.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/tests/test-cases/preload-link/expected.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/tests/test-cases/tag-track-opt-in/expected.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

plugins/optimization-detective/tests/test-cases/video/expected.html

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)