Skip to content

Implement HTML video and audio element lazy-loading via the loading attribute - #58220

Closed
credod wants to merge 2 commits into
WebKit:mainfrom
Squarespace:eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute
Closed

credod wants to merge 2 commits into
WebKit:mainfrom
Squarespace:eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute

Conversation

@credod

@credod credod commented Feb 9, 2026 •

Copy link
Copy Markdown
Contributor
Implement HTML video and audio element lazy-loading via the loading attribute
https://bugs.webkit.org/show_bug.cgi?id=303995
rdar://problem/166791801

Reviewed by NOBODY (OOPS!).

Implement proposed loading attribute for video and audio elements. 
This patch implements a proposed loading attribute for video and audio elements, enabling lazy loading of media sources and video poster images, and deferring autoplay.

This change is proposed to be added to the HTML spec in the following PR: 
whatwg/html#11980

This patch is passing video and audio WPT tests from the following PR: 
web-platform-tests/wpt#57051

* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml:
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/dom/Document.cpp: (WebCore::Document::lazyLoadMediaObserver):
* Source/WebCore/dom/Document.h:
* Source/WebCore/html/HTMLMediaElement.cpp: (WebCore::HTMLMediaElement::didMoveToNewDocument): (WebCore::HTMLMediaElement::attributeChanged):
(WebCore::HTMLMediaElement::didFinishInsertingNode): (WebCore::HTMLMediaElement::removedFromAncestor):
(WebCore::HTMLMediaElement::hasLazyLoadableAttributeValue): (WebCore::HTMLMediaElement::isLazyLoadable const): (WebCore::HTMLMediaElement::loadDeferredMedia):
(WebCore::HTMLMediaElement::resumeLazyLoadingIfNeeded): (WebCore::HTMLMediaElement::loading const):
(WebCore::HTMLMediaElement::setLoading):
(WebCore::HTMLMediaElement::play):
(WebCore::HTMLMediaElement::sourceWasAdded):
(WebCore::HTMLMediaElement::setShouldDelayLoadEvent):
* Source/WebCore/html/HTMLMediaElement.h:
* Source/WebCore/html/HTMLMediaElement.idl:
* Source/WebCore/html/HTMLVideoElement.cpp: (WebCore::HTMLVideoElement::rendererIsNeeded):
(WebCore::HTMLVideoElement::supportsFullscreen const): (WebCore::HTMLVideoElement::hasAvailableVideoFrame const): (WebCore::HTMLVideoElement::webkitEnterFullscreen): (WebCore::HTMLVideoElement::loadDeferredMedia):
* Source/WebCore/html/HTMLVideoElement.h:
* Source/WebCore/html/LazyLoadMediaObserver.cpp: Added. (WebCore::LazyLoadMediaObserver::observe):
(WebCore::LazyLoadMediaObserver::unobserve):
(WebCore::LazyLoadMediaObserver::intersectionObserver): (WebCore::LazyLoadMediaObserver::isObserved const):
* Source/WebCore/html/LazyLoadMediaObserver.h: Added.
* Source/WebCore/html/parser/HTMLParserOptions.cpp:
* Source/WebCore/html/parser/HTMLPreloadScanner.cpp: (WebCore::TokenPreloadScanner::StartTagScanner::processAttribute):
* Source/WebCore/html/shadow/DataListButtonElement.cpp:
* Source/WebCore/html/shadow/SpinButtonElement.cpp:
* Source/WebCore/loader/ImageLoader.cpp: (WebCore::ImageLoader::updateFromElement):
(WebCore::ImageLoader::didUpdateCachedImage):
(WebCore::ImageLoader::notifyFinished):
(WebCore::ImageLoader::updatedHasPendingEvent):
(WebCore::ImageLoader::decode):

We manually opened this PR on behalf of the Squarespace org after having some issues opening it in this way using git-webkit pull-request. Please let us know if there's a better way to open this PR. Thank you!

cc: @jernoble @jensimmons

e5fe50d

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ❌ 🛠 ios ❌ 🛠 mac ❌ 🛠 wpe ❌ 🛠 win
✅ 🧪 bindings ❌ 🛠 ios-sim ❌ 🛠 mac-AS-debug ❌ 🧪 wpe-wk2 ❌ 🧪 win-tests
✅ 🧪 webkitperl ❌ 🧪 ios-wk2 ❌ 🧪 api-mac ❌ 🧪 api-wpe
❌ 🧪 ios-wk2-wpt ❌ 🧪 api-mac-debug ❌ 🛠 gtk3-libwebrtc
✅ 🛠 🧪 jsc ❌ 🧪 api-ios ❌ 🧪 mac-wk1 ❌ 🛠 gtk
✅ 🛠 🧪 jsc-debug-arm64 ❌ 🛠 ios-safer-cpp ❌ 🧪 mac-wk2 ❌ 🧪 gtk-wk2
❌ 🛠 vision ❌ 🧪 mac-AS-debug-wk2 ❌ 🧪 api-gtk
❌ 🛠 vision-sim ❌ 🧪 mac-wk2-stress   🛠 playstation
⏳ 🧪 vision-wk2 ❌ 🧪 mac-intel-wk2 ✅ 🛠 jsc-armv7
❌ 🛠 tv ❌ 🛠 mac-safer-cpp ✅ 🧪 jsc-armv7-tests
❌ 🛠 tv-sim
❌ 🛠 watch
❌ 🛠 watch-sim

@credod
credod requested review from cdumez and rniwa as code owners February 9, 2026 19:01
@webkit-early-warning-system

Copy link
Copy Markdown
Collaborator

Starting EWS tests for 4f260a6. Live statuses available at the PR page, #58220

@Ahmad-S792

Copy link
Copy Markdown
Contributor

@credod - thanks for PR and if you need any help, please join Slack channel and you can ask for input via #help or #dev channels. https://webkit.org/getting-started/#staying-in-touch - Happy to answer any questions as well.

@nt1m
nt1m requested a review from a team February 9, 2026 19:11
@scottjehl

Copy link
Copy Markdown

Hi there! Some notes on this PR:

  • It includes a couple files that appear to be necessary for the build, but are unrelated to the feature (/shadow directory). It seems this is common to other PRs but we wanted to mention it anyway.
  • We initially tried to send this PR via the git-webkit pull-request workflow, but ran into issues attempting to send it from our organization rather than a personal account. Please let us know if we need to adjust things.

Thank you for your consideration!

cc:
@jensimmons and @jernoble

@brentfulgham

Copy link
Copy Markdown
Contributor

Would you mind including the Radar # in your ChangeLog header? in a line underneath the Bugzilla?

rdar://problem/166791801

@brentfulgham

Copy link
Copy Markdown
Contributor

Thank you for contributing this change? It's great to see work from other contributors!

Comment thread Source/WebCore/dom/Document.h Resolved
Comment thread Source/WebCore/loader/ImageLoader.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we not need these Resource Loader options when we are dealing with a Media Element?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe HTMLImageElement and HTMLMediaElement should both be instances of a LazyLoadable concept so we can deal with a single code path in these various cases below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Do we not need these Resource Loader options when we are dealing with a Media Element?

Good question. I don't think we need to concern ourselves with these attributes in this case. Based on the HTML spec, referrerpolicy and fetchpriority are not available as attributes the Media Element. The changes in this file ensure that the Media Element is making use of ImageLoader for lazy-loading a video poster (if it exists). When we specify <video src="/media.mp4" poster="/image.jpg">, we don't have the ability to specify attributes specific to the poster image. We can only control whether or not we render it.

Maybe HTMLImageElement and HTMLMediaElement should both be instances of a LazyLoadable concept so we can deal with a single code path in these various cases below.

This is a good point. LazyLoadImageObserver and LazyLoadMediaObserver are almost identical so I think we could share an observer between them. However, there is also a LazyLoadFrameObserver (used for <iframe>). I was a little hesitant to update the observer for all these elements because it feels slightly out of scope for this work, and I was following what had been done previously, but I'm happy to consolidate if we like that better!

Not sure if this matters, but the Chromium patch adds a new observer file (like I did here) while the Firefox patch has a shared observer for Media, Image, and Iframe elements

LazyLoadImageObserver::observe(protect(element()));
if (m_lazyImageLoadState == LazyImageLoadState::Deferred) {
#if ENABLE(VIDEO)
if (is<HTMLMediaElement>(element()))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's unfortunate that we have to do this type casting a second time.

}

bool HTMLMediaElement::isLazyLoadable() const
{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This code seems pretty costly to run frequently. It seems like this would ideally be cached and invalidated when the frame is detached, or attributes are modified.

I wonder if Image Lazy Load works the same way?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This function is very similar to the HTMLImageElement equivalent (this one includes a check for document.paginated()). In the image lazy load implementation, isLazyLoadable() is called in one place here (updateFromElement() function).

For the HTMLMediaElement, this is only potentially called during certain events: attributeChanged(), didFinishInsertingNode(), sourceWasAdded(), didMoveToNewDocument(), and ImageLoader::updateFromElement() (this last one is for video posters). During each of these events, we need to decide whether we need to lazy load or not but I'm not sure how frequent this would actually happen in practice. Since we only call this function during these events, would caching this value be necessary?

@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

Safer C++ Build #79268 (4f260a6)

❌ Found 2 failing files with 12 issues. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Feb 12, 2026
@credod

credod commented Feb 12, 2026

Copy link
Copy Markdown
Contributor Author

WPT tests for this work have landed with a tentative status!

@credod
credod force-pushed the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch 2 times, most recently from 6daee36 to 38b43c9 Compare February 12, 2026 18:58
@credod
credod force-pushed the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch from 38b43c9 to 42b1154 Compare February 12, 2026 20:25
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

Safer C++ Build #80213 (42b1154)

❌ Found 2 failing files with 12 issues. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@credod
credod force-pushed the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch from 42b1154 to f701cd9 Compare February 13, 2026 19:22
@credod
credod force-pushed the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch from f701cd9 to e362004 Compare February 17, 2026 19:50
@credod
credod force-pushed the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch from e362004 to 1f0548a Compare February 17, 2026 20:42
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

Safer C++ Build #81199 (1f0548a)

❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@credod
credod force-pushed the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch from 1f0548a to 4fa7bcd Compare February 18, 2026 15:33
…ttribute

https://bugs.webkit.org/show_bug.cgi?id=303995

Reviewed by NOBODY (OOPS!).

Implement proposed loading attribute for video and audio elements.
This patch implements a proposed loading attribute for video and audio elements, enabling lazy loading of media sources and video poster images, and deferring autoplay.

This change is proposed to be added to the HTML spec in the following PR:
whatwg/html#11980

This patch is passing video and audio WPT tests from the following PR:
web-platform-tests/wpt#57051

* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml:
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/dom/Document.cpp:
(WebCore::Document::lazyLoadMediaObserver):
* Source/WebCore/dom/Document.h:
* Source/WebCore/html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::didMoveToNewDocument):
(WebCore::HTMLMediaElement::attributeChanged):
(WebCore::HTMLMediaElement::didFinishInsertingNode):
(WebCore::HTMLMediaElement::removedFromAncestor):
(WebCore::HTMLMediaElement::hasLazyLoadableAttributeValue):
(WebCore::HTMLMediaElement::isLazyLoadable const):
(WebCore::HTMLMediaElement::loadDeferredMedia):
(WebCore::HTMLMediaElement::resumeLazyLoadingIfNeeded):
(WebCore::HTMLMediaElement::loading const):
(WebCore::HTMLMediaElement::setLoading):
(WebCore::HTMLMediaElement::play):
(WebCore::HTMLMediaElement::sourceWasAdded):
(WebCore::HTMLMediaElement::setShouldDelayLoadEvent):
* Source/WebCore/html/HTMLMediaElement.h:
* Source/WebCore/html/HTMLMediaElement.idl:
* Source/WebCore/html/HTMLVideoElement.cpp:
(WebCore::HTMLVideoElement::rendererIsNeeded):
(WebCore::HTMLVideoElement::supportsFullscreen const):
(WebCore::HTMLVideoElement::hasAvailableVideoFrame const):
(WebCore::HTMLVideoElement::webkitEnterFullscreen):
(WebCore::HTMLVideoElement::loadDeferredMedia):
* Source/WebCore/html/HTMLVideoElement.h:
* Source/WebCore/html/LazyLoadMediaObserver.cpp: Added.
(WebCore::LazyLoadMediaObserver::observe):
(WebCore::LazyLoadMediaObserver::unobserve):
(WebCore::LazyLoadMediaObserver::intersectionObserver):
(WebCore::LazyLoadMediaObserver::isObserved const):
* Source/WebCore/html/LazyLoadMediaObserver.h: Added.
* Source/WebCore/html/parser/HTMLParserOptions.cpp:
* Source/WebCore/html/parser/HTMLPreloadScanner.cpp:
(WebCore::TokenPreloadScanner::StartTagScanner::processAttribute):
* Source/WebCore/html/shadow/DataListButtonElement.cpp:
* Source/WebCore/html/shadow/SpinButtonElement.cpp:
* Source/WebCore/loader/ImageLoader.cpp:
(WebCore::ImageLoader::updateFromElement):
(WebCore::ImageLoader::didUpdateCachedImage):
(WebCore::ImageLoader::notifyFinished):
(WebCore::ImageLoader::updatedHasPendingEvent):
(WebCore::ImageLoader::decode):
@credod
credod force-pushed the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch from 4fa7bcd to e5fe50d Compare February 24, 2026 17:47
ASSERT_WITH_SECURITY_IMPLICATION(&document() == &newDocument);

// Handle lazy loading observer transfer between documents
oldDocument.lazyLoadMediaObserver().unobserve(*this, oldDocument);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems like it will allocate the LazyLoadMediaObserver even in cases where there there are no lazy loads and even in the case that lazyMediaLoadingEnabled() is false.

It seems like you could get around this by adding Document::lazyLoadMediaObserverIfExists() which returns nullptr if one hasn't been created yet. (see ensureIntersectionObserverData()/intersectionObserverDataIfExists() on Document for an example of this pattern).

class Document;
class Element;

class LazyLoadMediaObserver {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How distinct is this from LazyLoadImageObserver? Do we need to have both? Could LazyLoadImageObserver be renamed to encompass both images and media?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And to encompass LazyLoadModelObserver as well, as it also seems to be pretty much identical other than the bit in the intersection observer callback invoke. But those could combined together.

        for (auto& entry : entries) {
            if (RefPtr element = dynamicDowncast<HTMLImageElement>(entry->target())) {
                if (entry->isIntersecting()) {
                    element->loadDeferredImage();
                    element->document().lazyLoadImageObserver().unobserve(*element, element->document());
                }
            } else if (RefPtr element = dynamicDowncast<HTMLMediaElement>(entry->target())) {
                if (entry->isIntersecting())
                    element->loadDeferredMedia();
            } else if (RefPtr element = dynamicDowncast<HTMLModelElement>(entry->target()))
                element->viewportIntersectionChanged(entry->isIntersecting());
        }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Though I would probably add new a lazy load specific entry point for each (I just picked a verbose name, but that doesn't have to be it):

         for (auto& entry : entries) {
             if (RefPtr element = dynamicDowncast<HTMLImageElement>(entry->target()))
                 element->lazyLoadIntersectionCallbackInvoked(entry->isIntersecting());
             else if (RefPtr element = dynamicDowncast<HTMLMediaElement>(entry->target()))
                 element->lazyLoadIntersectionCallbackInvoked(entry->isIntersecting());
             else if (RefPtr element = dynamicDowncast<HTMLModelElement>(entry->target()))
                 element->lazyLoadIntersectionCallbackInvoked(entry->isIntersecting());
         }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hey @weinig, thanks for the suggestion! This felt like a bit of a separate task so I've opened a separate PR here. Would love to get your review on that!

@weinig weinig left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the contribution.

One thing this will need to move forward is testing.

@Ahmad-S792

Copy link
Copy Markdown
Contributor

@credod - I am syncing the-audio-element tests here - #59348 and you can run them once they are merged after rebasing on top of it and using run-webkit-tests imported/w3c/web-platform-tests/html/semantics/embedded-content/the-audio-element --reset-results to generate up-to-date results on your branch and see progressions.

@credod

credod commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@Ahmad-S792 thanks for that info. Just want to make sure that you're aware that the WPT tests we created for this work have been merged already?

I'm not super familiar with how things are done in this repo, but is it intentional that you're copying over the WPT tests into this repo in #59348? Is the expectation for me to open up a similar PR to bring in the tests for the video element?

@Ahmad-S792

Ahmad-S792 commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

@Ahmad-S792 thanks for that info. Just want to make sure that you're aware that the WPT tests we created for this work have been merged already?

I'm not super familiar with how things are done in this repo, but is it intentional that you're copying over the WPT tests into this repo in #59348? Is the expectation for me to open up a similar PR to bring in the tests for the video element?

Yes - I am syncing those WPT tests now into WebKit repo. Usually, the expectation is that the person do separate PR to sync tests with failure or skip expectations and then do PR to fix the bug to show progressions. If there are only few tests then it is fine to sync tests with implementation PR but in this case, if you do, it would become quite big for review. Hence, I am doing it separately, if you want to try you can do for the-video-element but if you want me to do it, happy to help out.

@scottjehl

Copy link
Copy Markdown

This review has been really great so far, thanks!

As an aside, we're still awaiting a Webkit standards position on the feature here, which would help move the spec proposal along. Thanks! WebKit/standards-positions#586

@credod

credod commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Yes - I am syncing those WPT tests now into WebKit repo. Usually, the expectation is that the person do separate PR to sync tests with failure or skip expectations and then do PR to fix the bug to show progressions. If there are only few tests then it is fine to sync tests with implementation PR but in this case, if you do, it would become quite big for review. Hence, I am doing it separately, if you want to try you can do for the-video-element but if you want me to do it, happy to help out.

Awesome, thanks @Ahmad-S792. I've opened a PR for syncing the-video-element WPT tests here

@scottjehl

Copy link
Copy Markdown

Update: @annevk and Igalia proposed a positive standards position for Webkit on this feature and spec proposal (which is now in Stage 3 at WHATWG) WebKit/standards-positions#586

It'll close as positive on March 19.

The Spec PR was approved yesterday as well: whatwg/html#11980

We'd love to get a status update on this patch and if there's anything for us to revise at this time. Thanks!

@Ahmad-S792

Copy link
Copy Markdown
Contributor

@scottjehl - I think we might need to update the-video-element tests sync PR and merge it and I noticed that there are now more added test cases, I am happy to sync, then this PR needs to be rebased and show progression / unskip previously timing out / failing tests to show progression and have green bots (compiling + passing tests without regression). Meanwhile if someone want to start looking at code, they will do in paralle.

@annevk

annevk commented Mar 14, 2026

Copy link
Copy Markdown
Contributor

As far as I can tell #59807 needs to land first, the tests need to land, then this needs to be rebased on top (and on top of several other changes, such as the change from checkedX to protect(x)). It's a lot easier to get review when everything is green and the test coverage is good so I recommend focusing on these tasks. Hope that helps! (And of course, thanks for contributing this to WebKit and the web platform!)

@credod credod closed this Oct 7, 2026
@credod
credod deleted the eng/Implement-HTML-video-and-audio-element-lazy-loading-via-the-loading-attribute branch October 7, 2026 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merging-blocked Applied to prevent a change from being merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants