Consider whether additional disambiguating attributes should be added as XPath predicates #1889
Labels
Needs Discussion
Anything that needs a discussion/agreement
[Plugin] Optimization Detective
Issues for the Optimization Detective plugin
First of all, see the plugin's documentation about the XPath format.
A typical XPath generated for a tag looks like the following:
Note that
/HTML
and/HTML/BODY
lack any predicates because there is no possibility for ambiguity in an HTML document at these levels. When it comes to descendants of theBODY
, there is the possibility of ambiguity since there can be any number of children of any type. In the case of direct descendants of theBODY
, the predicate consists of theid
,role
, orclass
attribute since atwp_body_open
any number of tags may be output which causes the node index to change, resulting in/HTML/BODY/DIV[@class='wp-site-blocks']
. Below this level, however, there is more consistency. So to ensure that anIMG
captured during detection remains the sameIMG
and isn't moved around in the document due to a paragraph being added before, for example, nodes further below are selected specifying the node index with the tag name being another predicate, like*[2][self::IMG]
. This XPath will stop matching a givenIMG
if a preceding tag is added or removed. This helps ensure that optimizations don't get incorrectly applied.Nevertheless, there are a couple cases I'm aware of where this breaks down.
IMG
to a page and it is the LCP element, and then detection occurs. If afterward, thisIMG
is replaced with a tiny image, the result can be that theIMG
may erroneously getfetchpriority=high
added to it. (Aside: One option would be to not do any optimization if there are stale URL Metrics according to the ETag, since a post update to change the image would cause the ETag to change.)This means that an XPath which identified one
IMG
as LCP for one page load may incorrectly try to optimize theIMG
in the same document position as LCP for the next page load.This issue is something I also encountered as a possibility while working on the Intrinsic Dimensions plugin. If there is a Gallery of images which lack dimensions, for example, and all the images are presented in a random order, then the intrinsic dimensions captured for one
IMG
may incorrectly be applied to a differentIMG
which appears in the same document position with the next page load. To address this, I explored introducing a "srcHash
" which takes thesrc
andsrcset
for an image and turns it into an MD5 hash. This hash is then stored in the URL Metric with the element along with the intrinsicwidth
andheight
. When the tag visitor optimizes anIMG
without dimensions, it checks to see if the currentsrcHash
matches thesrcHash
stored in the URL Metric. If not, it aborts.One way to deal with all of these issues would be to include the
src
(andsrcset
?) of theIMG
in its XPath. This would prevent optimizations from erroneously applying to wrong element. (This is also relevant toVIDEO
which can also have asrc
, but which also can haveSOURCE
elements instead.) For images, an alternative would be to include theclass
name in the XPath instead.My suspicion is that the above scenarios are edge cases that most of the time wouldn't cause an issue. I'm not sure they are significant enough to warrant safeguards, but I wanted to raise the scenario for consideration.
The text was updated successfully, but these errors were encountered: