-
Notifications
You must be signed in to change notification settings - Fork 35
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
media-condition/mediaquery changes should only affect images in document #257
Comments
Note that in some scenarios that may not be the desired behavior: e.g. authors may want to remove the element from the DOM only to bring it back into the DOM later one, with the right resource already loaded. Also, in the current situation, authors can remove the HTMLImageElement from the inside of HTMLPictureElement to make sure viewport changes don't trigger a resource load. With that said, I'm not against such a spec change, just saying that people may get surprised either way we pick. |
Also, seems like the spec has no guidance regarding viewport changes and their impact on triggering resource loading: #157 |
FYI - the use case where this is happening is a single page application where the user is moving from a list view to a detail view that displays a responsive image. I remove the detail view after the user returns to the list view. It was at this time that I noticed that the picture element in the view I removed kept firing when I resized the page. My main concern is that the browser was not releasing the memory of the detail views as the user navigated back and forth between the list and detail views. |
Removing an element from the document (or inserting it to the document) doesn't change the viewport. It's also not a "relevant mutation" (unless you remove/insert Moving an element from one document to another is a "relevant mutation" (last bullet point). |
@philipjmurphy The memory concern is probably a viable one, but can probably be addressed by removing the |
To be honest, I don't really understand @zcorpan comment. I know that removing an image or a picture image construct from the document is not a relevant mutation. However - if an img or picture/img construct is removed from the document and even if there is no reference from JS land to this DOM object or its ancestors - Chrome still references it and still runs the source selection algorithm for it, if the viewport changes. This is obviously bad for network and memory. But it also introduces a totally new concept of how dom nodes have to be removed. Do you really suggest, that any JS script in the world, which worked element agnostic to change the content of a document has to include now some exceptional JS to treat responsive images differently? If this issue isn't fixed, every script, which uses the following code: function replaceHTML(dom, newContent){
dom.innerHTML = newContent;
} has to change to something like this: var cleanImg = function(img){
img.removeAttribute('srcset');
img.removeAttribute('sizes');
if(img.parentNode){
img.parentNode.removeChild(img);
}
};
function replaceHTML(dom, newContent){
Array.prototype.slice.call(dom.querySelector('img[srcset], picture > img')).forEach(cleanImg);
dom.innerHTML = newContent;
} I can try to submit a patch for jQuery's $.cleanData method, but I'm pretty sure they will think I'm nuts. |
@zcorpan's suggestion on IRC to use weak references to the keep track of nodes for viewport notifications purposes can probably do the trick. |
Oh I probably misunderstood what was meant by "viewport changes". I thought you meant that removing an element from the document would associate the |
|
The HTML spec sometimes has requirements about garbage collection where it is not obvious, e.g. WebSocket. We can certainly do it here also. We can also tweak the rules so that it becomes possible to garbage collect |
Correct me if I'm wrong. I thought, that added event listeners aren't taken into account by garbage collectors (only in old IE, if there is a circular reference). Therefore if there is a listener added to the element and there is no (other) reference, the element will be garbage collected. If it wouldn't be garbage collected, I'm fine with both ways you described above. But if it would be garbage collected I don't see a reason why an event listener should still count for source selection to be run. |
On the one hand, maybe the author just took the image out of the document in order to later add it in a different location, expecting it to keep itself up-to-date with the current viewport dimensions? On the other hand, if we'd rely only on weakrefs and GC in order to avoid downloads here, we will probably end up with a racy experience, where the resources are sometimes downloaded and sometimes aren't. (I'm guessing we do not want to force GC in the middle of JS execution) So, aborting early for the env change case (as well as weakrefs) probably makes the most sense. |
Possibly event listeners are not relevant for |
I haven't specified anything specific about GC, but technically it's not necessary. I think it's more useful to spend the time writing test cases. |
Currently if a
picture
/img
is removed from DOM, viewport changes are triggering downloads for these elements. See also corresponding chrome bug ticket.In case of a SPA or AJAX driven website, this yields to a) a lot of memory consumption and b) a lot of network traffic for images, that are removed long time ago.
The spec should be more clear, that if an images is created or a relevant mutation-change happend the resource selection should start (queued) based on the current viewport/media-condition/mediaquery matches, but in case of a mediaquery change on the other hand should only affect images inside the document.
The text was updated successfully, but these errors were encountered: