Skip to content
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

Prioritize loading poster image of video LCP elements #1498

Open
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Tag visitor that optimizes VIDEO tags:
* - Adds preload links for poster images if in a breakpoint group's LCP.
*
* @package image-prioritizer
*
* @since n.e.x.t
*/

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Image Prioritizer: Image_Prioritizer_Video_Tag_Visitor class
*
* @since n.e.x.t
*
* @access private
*/
final class Image_Prioritizer_Video_Tag_Visitor extends Image_Prioritizer_Tag_Visitor {

/**
* Video tag.
*
* @var string
*/
const VIDEO = 'VIDEO';

/**
* Poster attribute.
*
* @var string
*/
const POSTER = 'poster';

/**
* Visits a tag.
*
* @param OD_Tag_Visitor_Context $context Tag visitor context.
*
* @return bool Whether the visitor visited the tag.
*/
public function __invoke( OD_Tag_Visitor_Context $context ): bool {
$processor = $context->processor;
if ( self::VIDEO !== $processor->get_tag() ) {
return false;
}

// Skip empty poster attributes and data: URLs.
$poster = trim( (string) $processor->get_attribute( self::POSTER ) );
if ( '' === $poster || $this->is_data_url( $poster ) ) {
return false;
}

$xpath = $processor->get_xpath();

// If this element is the LCP (for a breakpoint group), add a preload link for it.
foreach ( $context->url_metrics_group_collection->get_groups_by_lcp_element( $xpath ) as $group ) {
$link_attributes = array_merge(
array(
'rel' => 'preload',
'fetchpriority' => 'high',
'as' => 'image',
),
array_filter(
array(
'href' => (string) $processor->get_attribute( self::POSTER ),
),
static function ( string $value ): bool {
return '' !== $value;
}
)
);

$crossorigin = $processor->get_attribute( 'crossorigin' );
if ( is_string( $crossorigin ) ) {
$link_attributes['crossorigin'] = 'use-credentials' === $crossorigin ? 'use-credentials' : 'anonymous';
}

$link_attributes['media'] = 'screen';

$context->link_collection->add_link(
$link_attributes,
$group->get_minimum_viewport_width(),
$group->get_maximum_viewport_width()
);
}

return true;
}
}
3 changes: 3 additions & 0 deletions plugins/image-prioritizer/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $regis

$bg_image_visitor = new Image_Prioritizer_Background_Image_Styled_Tag_Visitor();
$registry->register( 'bg-image-tags', $bg_image_visitor );

$video_visitor = new Image_Prioritizer_Video_Tag_Visitor();
$registry->register( 'video-tags', $video_visitor );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call this something like image-prioritizer-video-poster-preloading. The names I had chosen previously are too generic.

}
1 change: 1 addition & 0 deletions plugins/image-prioritizer/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ static function ( string $version ): void {

require_once __DIR__ . '/class-image-prioritizer-tag-visitor.php';
require_once __DIR__ . '/class-image-prioritizer-img-tag-visitor.php';
require_once __DIR__ . '/class-image-prioritizer-video-tag-visitor.php';
require_once __DIR__ . '/class-image-prioritizer-background-image-styled-tag-visitor.php';
require_once __DIR__ . '/helper.php';
require_once __DIR__ . '/hooks.php';
Expand Down