-
Notifications
You must be signed in to change notification settings - Fork 25
Ability to detect whether an element is rendered. #112
Comments
/cc @chrishtr |
To answer my own question about frames: I prefer that we limit this to the document level so that the value would not cross frames. |
We had a use case for this that came up while prototyping this. When performing an interaction we'll collect metrics after the final paint. During interactions we will watch for DOM mutations and after the final paint we will walk over the rendered images and make sure they are visible amongst other checks (like making sure it's downloaded). Without knowing if the element is in an activated subtree we query the bounding client react which forces activation of the element. Normally since we just painted the layout information should be current. But this is no longer true if you're within a tree that isn't activated. If we can query if the element is not activated then we can ignore these subtrees and avoiding activating them. The other use case that's worth exploring is how frameworks like React could benefit from this. Imagine a frameworks knows that an element is not activated and that it's stale. It can check and defer the rendering of stale elements, from VDOM->DOM) to get the paint out sooner. This use case is more complex so perhaps it warrants a separate deep dive. |
@bgirard, a question: is it just images in the viewport that you want to check for download+render? If so, why does a regular IntersectionObserver on them not work? Also, does your current code walk the entire DOM after DOM mutations looking for such images? @vmpstr and I brainstormed a possible API change to IntersectionObserver where you could observe |
The maintainer will see if we can move entirely to IntersectionObserver. Right now it's half It might be worthwhile having good WPT coverage around the interaction of this spec and IntersectionObserver. What are the performance implications of calling |
querySelectorAll will force-compute style for any invisible subtrees. The performance of selectors themselves is almost certainly not going to be a big part of the cost, unless there are many matches. |
I take back my previous comment, it was just incorrect. querySelectorAll does not force-compute styles. |
Another use case: determining whether a
In the old API, hidden-matchable elements could change state due to automatic UA activation. The new API doesn't have this, and is instead developer controlled. Since the developer is always aware of what happened to change subtree-visibility, it seems we don't need a new event. (*) They also observed that this caused a site compatibility bug for them when removing the previous |
Update: the previous use case about detecting subtree skipping turned out to be only for a specific AMP component, and was able to be achieved in a different way. The use case @bgirard mentioned was able to be solved with IntersectionObserver. The React use case mentioned (skip Virtual DOM (VDOM) rendering for subtrees that are not on screen) is still quite interesting, because React sites (and those from other frameworks that do client-side hydration) still suffer from the cost of rendering VDOM into DOM, even if that DOM ends up off-screen and has One question that might come up is whether the observer really needs to be in sync with the browser rendering state, or if an IntersectionObserver with generous margins would suffice. It may even be better to use a larger margin for VDOM rendering, because gives more time for the VDOM rendering to occur and therefore reduce checkerboarding / flashing of non-rendered content. Finally, a special case that is very important is the first render. If a React site is loaded that has all content under It seems the solution is to expose a special event to script just for this situation, either via IntersectionObserver or a one-off event, such as *(Note that this event can be polyfilled via ResizeObserver, so I think would not be introducing a new unachievable footgun.) |
@vmpstr raised a very good point, which is that, since This seems to be a good enough motivation for exposing an event |
I created a preact-based clone of Una's demo that combines |
New use case: detecting whether an iframe embedded in a cross-origin, possibly not trusted, context is hidden. The iframe would reasonably want to know whether it is hidden, so that it can throttle work. |
@chrishtr would the Page Visibility API not suffice there? |
@developit yes, if we decided that iframes received page visibilty lifecycle events when hidden by |
This came up several times during discussions:
It would be nice to know if an element is rendered from script. In one context, it may act as a signal whether to do any script side updates to the element. In another similar context, it may act as a signal to stop or start doing resources fetches for a component.
We should add a way to detect from script whether an element is rendered. Some questions to consider:
The text was updated successfully, but these errors were encountered: