diff --git a/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php b/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php index ea057ed92d..4eebd203d6 100644 --- a/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php +++ b/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php @@ -78,6 +78,10 @@ public function __invoke( OD_HTML_Tag_Walker $walker ): bool { } elseif ( ! $is_visible && 'lazy' !== $loading ) { $walker->set_attribute( 'loading', 'lazy' ); } + // If the element is not visible in the initial viewport, set fetchpriority to low. + if ( ! $is_visible ) { + $walker->set_attribute( 'fetchpriority', 'low' ); + } } // TODO: If an image is visible in one breakpoint but not another, add loading=lazy AND add a regular-priority preload link with media queries (unless LCP in which case it should already have a fetchpriority=high link) so that the image won't be eagerly-loaded for viewports on which it is not shown. diff --git a/plugins/image-prioritizer/tests/test-helper.php b/plugins/image-prioritizer/tests/test-helper.php index 12a3207fc3..6849c93422 100644 --- a/plugins/image-prioritizer/tests/test-helper.php +++ b/plugins/image-prioritizer/tests/test-helper.php @@ -287,9 +287,9 @@ public function data_provider_test_filter_tag_walker_visitors(): array {

Pretend this is a super long paragraph that pushes the next image mostly out of the initial viewport.

Bar

Now the following image is definitely outside the initial viewport.

- Baz - Qux - Quux + Baz + Qux + Quux ',