-
Notifications
You must be signed in to change notification settings - Fork 153
Add native loading=lazy support for video elements #2450
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
39f797e
668f515
771be4f
e871d03
d8e6794
167c0da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,38 +1,50 @@ | ||
| const lazyVideoObserver = new IntersectionObserver( | ||
| ( entries ) => { | ||
| for ( const entry of entries ) { | ||
| if ( entry.isIntersecting ) { | ||
| const video = /** @type {HTMLVideoElement} */ entry.target; | ||
| const restoreVideo = ( /** @type {HTMLVideoElement} */ video ) => { | ||
| const poster = video.getAttribute( 'data-original-poster' ); | ||
| if ( poster ) { | ||
| video.setAttribute( 'poster', poster ); | ||
| } | ||
|
|
||
| const poster = video.getAttribute( 'data-original-poster' ); | ||
| if ( poster ) { | ||
| video.setAttribute( 'poster', poster ); | ||
| } | ||
| if ( video.hasAttribute( 'data-original-autoplay' ) ) { | ||
| video.setAttribute( 'autoplay', 'autoplay' ); | ||
| } | ||
|
|
||
| if ( video.hasAttribute( 'data-original-autoplay' ) ) { | ||
| video.setAttribute( 'autoplay', 'autoplay' ); | ||
| } | ||
| const preload = video.getAttribute( 'data-original-preload' ); | ||
| if ( preload ) { | ||
| if ( 'default' === preload ) { | ||
| video.removeAttribute( 'preload' ); | ||
| } else { | ||
| video.setAttribute( 'preload', preload ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const preload = video.getAttribute( 'data-original-preload' ); | ||
| if ( preload ) { | ||
| if ( 'default' === preload ) { | ||
| video.removeAttribute( 'preload' ); | ||
| } else { | ||
| video.setAttribute( 'preload', preload ); | ||
| } | ||
| } | ||
| const videos = document.querySelectorAll( 'video.od-lazy-video' ); | ||
|
|
||
| lazyVideoObserver.unobserve( video ); | ||
| // When the browser natively supports lazy-loading on video, restore the original | ||
| // attributes immediately and rely on loading="lazy" to defer the video load. | ||
| if ( 'loading' in HTMLMediaElement.prototype ) { | ||
| for ( const video of videos ) { | ||
| restoreVideo( /** @type {HTMLVideoElement} */ ( video ) ); | ||
| } | ||
| } else { | ||
| const lazyVideoObserver = new IntersectionObserver( | ||
| ( entries ) => { | ||
| for ( const entry of entries ) { | ||
| if ( entry.isIntersecting ) { | ||
| restoreVideo( | ||
| /** @type {HTMLVideoElement} */ ( entry.target ) | ||
| ); | ||
| lazyVideoObserver.unobserve( entry.target ); | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| rootMargin: '100% 0% 100% 0%', | ||
| threshold: 0, | ||
| } | ||
| }, | ||
| { | ||
| rootMargin: '100% 0% 100% 0%', | ||
| threshold: 0, | ||
| } | ||
| ); | ||
| ); | ||
|
|
||
| const videos = document.querySelectorAll( 'video.od-lazy-video' ); | ||
| for ( const video of videos ) { | ||
| lazyVideoObserver.observe( video ); | ||
| for ( const video of videos ) { | ||
| lazyVideoObserver.observe( video ); | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>...</title> | ||
| <meta name="generator" content="optimization-detective 0.0.0"> | ||
| <meta name="generator" content="image-prioritizer 0.0.0"> | ||
| </head> | ||
| <body> | ||
| <div id="page"> | ||
| <video class="desktop" width="1200" height="500" src="https://example.com/header.mp4" preload="auto" autoplay></video> | ||
| </div> | ||
| </body> | ||
| </html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
| return static function ( Test_Image_Prioritizer_Helper $test_case ): void { | ||
| $breakpoint_max_widths = array( 480, 600, 782 ); | ||
|
|
||
| add_filter( | ||
| 'od_breakpoint_max_widths', | ||
| static function () use ( $breakpoint_max_widths ) { | ||
| return $breakpoint_max_widths; | ||
| } | ||
| ); | ||
|
|
||
| foreach ( array_merge( $breakpoint_max_widths, array( 1000 ) ) as $viewport_width ) { | ||
| $test_case->store_url_metric( | ||
| od_get_url_metrics_slug( od_get_normalized_query_vars() ), | ||
| $test_case->get_sample_url_metric( | ||
| array( | ||
| 'viewport_width' => $viewport_width, | ||
| 'elements' => array( | ||
| array( | ||
| 'isLCP' => false, | ||
| 'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[1][self::VIDEO]', | ||
| 'boundingClientRect' => $test_case->get_sample_dom_rect(), | ||
| 'intersectionRatio' => 0.0, | ||
| ), | ||
| ), | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>...</title> | ||
| <meta name="generator" content="optimization-detective 0.0.0"> | ||
| <meta name="generator" content="image-prioritizer 0.0.0"> | ||
| </head> | ||
| <body> | ||
| <div id="page"> | ||
| <video class="desktop" poster="https://example.com/poster.jpg" width="1200" height="500" src="https://example.com/header.mp4" loading="lazy"></video> | ||
| </div> | ||
| </body> | ||
| </html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
| return static function ( Test_Image_Prioritizer_Helper $test_case ): void { | ||
| $breakpoint_max_widths = array( 480, 600, 782 ); | ||
|
|
||
| add_filter( | ||
| 'od_breakpoint_max_widths', | ||
| static function () use ( $breakpoint_max_widths ) { | ||
| return $breakpoint_max_widths; | ||
| } | ||
| ); | ||
|
|
||
| foreach ( array_merge( $breakpoint_max_widths, array( 1000 ) ) as $viewport_width ) { | ||
| $test_case->store_url_metric( | ||
| od_get_url_metrics_slug( od_get_normalized_query_vars() ), | ||
| $test_case->get_sample_url_metric( | ||
| array( | ||
| 'viewport_width' => $viewport_width, | ||
| 'elements' => array( | ||
| array( | ||
| 'isLCP' => false, | ||
| 'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[1][self::VIDEO]', | ||
| 'boundingClientRect' => $test_case->get_sample_dom_rect(), | ||
| 'intersectionRatio' => 0.5, | ||
| ), | ||
| ), | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>...</title> | ||
| <meta name="generator" content="optimization-detective 0.0.0"> | ||
| <meta name="generator" content="image-prioritizer 0.0.0"> | ||
| </head> | ||
| <body> | ||
| <div id="page"> | ||
| <video class="desktop" poster="https://example.com/poster.jpg" width="1200" height="500" src="https://example.com/header.mp4" loading="lazy"></video> | ||
| </div> | ||
| </body> | ||
| </html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
| return static function ( Test_Image_Prioritizer_Helper $test_case ): void { | ||
| $breakpoint_max_widths = array( 480, 600, 782 ); | ||
|
|
||
| add_filter( | ||
| 'od_breakpoint_max_widths', | ||
| static function () use ( $breakpoint_max_widths ) { | ||
| return $breakpoint_max_widths; | ||
| } | ||
| ); | ||
|
|
||
| foreach ( array_merge( $breakpoint_max_widths, array( 1000 ) ) as $viewport_width ) { | ||
| $test_case->store_url_metric( | ||
| od_get_url_metrics_slug( od_get_normalized_query_vars() ), | ||
| $test_case->get_sample_url_metric( | ||
| array( | ||
| 'viewport_width' => $viewport_width, | ||
| 'elements' => array( | ||
| array( | ||
| 'isLCP' => true, | ||
| 'xpath' => '/HTML/BODY/DIV[@id=\'page\']/*[1][self::VIDEO]', | ||
| 'boundingClientRect' => $test_case->get_sample_dom_rect(), | ||
| 'intersectionRatio' => 1.0, | ||
| ), | ||
| ), | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something that just came to mind: What about sites that are configured with infinite scroll? The newly-inserted content could come in with the
autoplayandpreloadattributes removed.Perhaps this is unlikely because it would mean Image Prioritizer would have to be running on the Ajax response.
Also, worst case is it means the video wouldn't get preloaded, meaning the user would have to click it to play. Not the end of the world. Since it is implemented in Chrome now, appears to be implemented behind a flag in Firefox and the Safari implementation is also moving along, I expect we can soon eliminate the JS here entirely.