Skip to content

Commit c66d923

Browse files
committed
Add docs for new actions
1 parent 0b8a184 commit c66d923

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

plugins/optimization-detective/docs/hooks.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,54 @@ Refer to [Image Prioritizer](https://github.com/WordPress/performance/tree/trunk
105105
[Embed Optimizer](https://github.com/WordPress/performance/tree/trunk/plugins/embed-optimizer) for additional
106106
examples of how tag visitors are used.
107107

108+
### Action: `od_start_template_optimization` (argument: `OD_Template_Optimization_Context`)
109+
110+
Fires before Optimization Detective starts iterating over the document in the output buffer.
111+
112+
This is before any of the registered tag visitors have been invoked.
113+
114+
It is important to note that this action fires _after_ the entire template has been rendered into the output buffer. In
115+
other words, it will fire after the `wp_footer` action.
116+
117+
This action runs before any of the registered tag visitors have been invoked in the current response. It is useful for
118+
an extension to gather the required information from the currently-stored URL Metrics for tag visitors to later leverage.
119+
See [example](https://github.com/WordPress/performance/pull/1921) from the Image Prioritizer plugin where it can be used
120+
to determine what the common external LCP background-image is for each viewport group up front so that this doesn't have
121+
to be computed when a tag visitor is invoked.
122+
123+
This action can be used if a site wants to prevent storing a response in the page cache until it has collected URL Metrics
124+
from both mobile and desktop:
125+
126+
```php
127+
add_action(
128+
'od_start_template_optimization',
129+
static function ( OD_Template_Optimization_Context $context ) {
130+
if (
131+
$context->url_metric_group_collection->get_first_group()->count() === 0
132+
||
133+
$context->url_metric_group_collection->get_last_group()->count() === 0
134+
) {
135+
header( 'Cache-Control: private' );
136+
}
137+
}
138+
);
139+
```
140+
141+
This could just as well be done at `od_finish_template_optimization` since the headers are not sent until after that
142+
action completes and the output buffer is returned.
143+
144+
### Action: `od_finish_template_optimization` (argument: `OD_Template_Optimization_Context`)
145+
146+
Fires after Optimization Detective has finished iterating over the document in the output buffer.
147+
148+
This is after all the registered tag visitors have been invoked.
149+
150+
This action runs after all the tags in a document have been visited and so no additional tag visitor will be invoked.
151+
This action has limited usefulness at the moment, but see [#1931](https://github.com/WordPress/performance/issues/1931)
152+
which will introduce methods on `OD_Template_Optimization_Context` to insert HTML into the document. This will allow,
153+
for example, tag visitors to gather the styles for tags encountered on the page and to print them all in one single
154+
`STYLE` tag.
155+
108156
### Action: `od_url_metric_stored` (argument: `OD_URL_Metric_Store_Request_Context`)
109157

110158
Fires whenever a URL Metric was successfully stored.

plugins/optimization-detective/optimization.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,9 @@ function od_optimize_template_output_buffer( string $buffer ): string {
286286
);
287287

288288
/**
289-
* Fires before Optimization Detective starts optimizing the template.
289+
* Fires before Optimization Detective starts iterating over the document in the output buffer.
290+
*
291+
* This is before any of the registered tag visitors have been invoked.
290292
*
291293
* @since n.e.x.t
292294
*
@@ -355,7 +357,9 @@ function od_optimize_template_output_buffer( string $buffer ): string {
355357
}
356358

357359
/**
358-
* Fires after Optimization Detective finishes optimizing the template.
360+
* Fires after Optimization Detective has finished iterating over the document in the output buffer.
361+
*
362+
* This is after all the registered tag visitors have been invoked.
359363
*
360364
* @since n.e.x.t
361365
*

0 commit comments

Comments
 (0)