@@ -105,6 +105,54 @@ Refer to [Image Prioritizer](https://github.com/WordPress/performance/tree/trunk
105
105
[ Embed Optimizer] ( https://github.com/WordPress/performance/tree/trunk/plugins/embed-optimizer ) for additional
106
106
examples of how tag visitors are used.
107
107
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
+
108
156
### Action: ` od_url_metric_stored ` (argument: ` OD_URL_Metric_Store_Request_Context ` )
109
157
110
158
Fires whenever a URL Metric was successfully stored.
0 commit comments