Repository navigation
remove dependency on imagesloaded - #1306
Conversation
petschki
left a comment
There was a problem hiding this comment.
I like this: one dependency less, and images which are added later are handled too. The two fixes on the way are real (the MutationObserver callback in pat-equaliser, the layoutComplete listener in pat-masonry).
I see no blocker. But there are three changes in behaviour which you should decide on. Points 1 and 3 and the smaller points are from Jest tests and reading the code on this branch. Point 2 I tested in a browser (Chromium 156).
1. pat-equaliser now reacts to mutations in the whole document
The observer is on document.body. Until now its callback did nothing, so this is new behaviour. What I measured:
| Change | Updates |
|---|---|
| attribute on an element outside the container | 1 |
| class on a nested element in the container | 1 |
| class on a direct child of the container | 0 |
| mutation outside every 30 ms for one second | 0 meanwhile, 1 after it |
The debounce keeps the cost small and there is no endless loop. But the filter against the own loop ignores every class and style change on a direct child, also those from other code. When one of the equalised columns is opened by a class on itself, e.g. with pat-collapsible, the heights are not equalised again; when something deeper in it changes, they are.
I would only ignore what the equaliser does itself: height in style and the class equalised.
2. pat-scroll does not wait for images which are loaded later
imagesloaded(document.body) waited for all images which were in the document when the pattern was initialized. Now the pattern scrolls at once when document.readyState is already complete.
I tested this in a browser: a page which is loaded, then a pat-inject link loads a fragment with a pat-scroll with trigger: auto, an image of 1500px height which the server delivers after 3 seconds, and the target below it.
| Scrolls after | Target after the image is loaded | |
|---|---|---|
| master | about 3.1 s (after the image) | 16px below the top, visible |
| this branch | about 0.1 s (at once) | 1516px below the top, outside of the window |
The image has no width and height attributes; with them the target would not move.
On the first page load it now waits for all resources, so also for iframes and stylesheets.
3. pat-masonry does not wait for lazy images
masonry-ready is set when all images without loading="lazy" are done. That is documented and makes sense to me. The class is not removed again after that; images which are added later only get a quick layout.
Smaller points
- pat-carousel is set up before the images are loaded. Slick recalculates with
setPosition, but until then the carousel can be visible with wrong dimensions. - The capturing listener also gets
loadfrom iframes, scripts and stylesheets in the container. That only leads to an update which is not needed. - The listeners are never removed, and neither is the
resizelistener of pat-equaliser. The observer of pat-equaliser only disconnects at the next mutation after its container was removed. That was not better before. - The first commit "pat-validation: Autoformat." is the same as in #1249. Whichever is merged first takes it along.
- The description of the PR is empty. The commit messages explain everything well; one sentence on the three changes above would help for the release notes.
Side note: Mockup does not import imagesloaded and has none of the four patterns in its bundle.
…agesloaded. With `trigger: auto`, scroll after the page including all images has been loaded. If the page is already loaded, scroll immediately. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
… changes. - Equalise immediately on initialization and re-equalise whenever an image within the container has been loaded or failed loading. This uses a capturing load/error event listener, which also handles images added later. The imagesloaded library is not used anymore. - Fix the MutationObserver callback, which got the list of mutation records passed instead of the container and therefore never re-equalised. - Ignore the style and class changes the equaliser does on the children itself, which would otherwise lead to an endless update loop. - Stop observing when the container was removed from the DOM and do not run pending updates on removed containers. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
…listener. - Re-layout whenever an image within the container has been loaded or failed loading. This uses a capturing load/error event listener, which also handles images added later. When all images are loaded, do a full layout and set the `masonry-ready` class, like before. - Do not wait for images with `loading="lazy"`, which might only be loaded when scrolled into view. imagesloaded did force loading these. - Fix the `layoutComplete` event listener being added again on each `layout` call. - Add real tests. The previous test had its only assertion commented out. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
…ages with imagesloaded. Set up the carousel immediately and recalculate its dimensions whenever an image within has been loaded or failed loading. This uses a capturing load/error event listener, which also handles images added later. Slick itself also recalculates on the window load event. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
It is not used anymore. pat-carousel, pat-equaliser, pat-masonry and pat-scroll now use native browser APIs to react on loaded images. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Register the window load and the click event listeners as pattern instance scoped listeners, which are removed when the pattern is destroyed. Before, with `trigger: auto` a pat-scroll element which was removed before the page was loaded still tried to scroll on load and threw an error. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
pat-equaliser is now based on BasePattern and gets destroyed automatically when its element is removed from the document: - The window resize and the image load/error listeners are pattern instance scoped and removed on destroy. Before, the resize listener was never removed and kept updating removed elements. - The MutationObserver is disconnected on destroy. This replaces the manual checks for the element still being connected. - Pending debounced updates are not run anymore after destroy. Breaking change for JavaScript users: the default export is now the pattern class. The `equaliser._update(container)` function was replaced by the `update()` method of the pattern instance. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Before, every class and style change on a direct child of the container was ignored, also those done by other code. E.g. when a column was opened by a class on itself, the heights were not equalised again. Now only the changes the equaliser does itself are ignored: the `height` style, the `overflow` styles which are set temporarily by the `grow` transition and the `equalised` class. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
With `trigger: auto`, wait for all images in the document which are not loaded yet, like imagesloaded did before - instead of waiting for the window load event. Before, a pat-scroll which was injected after the page was loaded scrolled immediately. Images above the target which were loaded later moved the target out of view. This also does not wait anymore for other resources like iframes and stylesheets on the first page load. Images with `loading="lazy"` are not waited for, as they might only be loaded when scrolled into view. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
- `on_media_loaded(el, callback, { signal })`: Call back whenever an element
within a container has been loaded which might have changed its size:
images which have been loaded or failed loading and videos which have
loaded their metadata. Uses capturing event listeners, which also catch
elements added later. Other elements like iframes, scripts, stylesheets
or audio elements are ignored.
- `images_loaded(el, { signal })`: Return a Promise which is resolved when
all images within an element are loaded or failed loading. Lazy loading
images are not waited for, as they might only be loaded when scrolled into
view.
Both accept an AbortSignal to stop listening or waiting.
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
- Re-layout only when images have been loaded or failed loading and not anymore when other elements like iframes, scripts or stylesheets have been loaded. - Also re-layout when a video has loaded its metadata, which can change its dimensions. - Use images_loaded to set the `masonry-ready` class when all images are loaded, which replaces the custom check on each load event. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
- Re-equalise only when images have been loaded or failed loading and not anymore when other elements like iframes, scripts or stylesheets have been loaded. - Also re-equalise when a video has loaded its metadata, which can change its dimensions. The listeners are removed on destroy via an AbortController. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
- Recalculate the dimensions only when images have been loaded or failed loading and not anymore when other elements like iframes, scripts or stylesheets have been loaded. - Also recalculate when a video has loaded its metadata, which can change its dimensions. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Wait for the images with images_loaded and stop waiting when the pattern is destroyed via an AbortController. Videos are not waited for. Unlike the other patterns, pat-scroll would not scroll at all if a video never loads its metadata, e.g. with `preload="none"`. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
e3a1f45 to
bdb44e6
Compare
|
AJ Answer: Thanks for the thorough review and the browser measurements! I addressed all three points and most of the smaller ones: 1. pat-equaliser reacting to mutations in the whole document. Fixed in "fix(pat-equaliser): Only ignore the own changes on the children." The observer now only ignores the changes equaliser makes itself: the While doing this I found that jQuery's 2. pat-scroll not waiting for images that load later. Fixed in "fix(pat-scroll): Wait for images which are loaded later." It no longer waits for I repeated your test in Firefox: an injected fragment, a 1500px image without dimensions delivered after 3s, and the target below it. The page now scrolls at about 3.1s, after the image has arrived, and the target ends up 8px from the top. Your side note is fixed too: on the first page load it no longer waits for iframes and stylesheets. 3. pat-masonry not waiting for lazy images. We're keeping that, after trying it out on a demo page where every image arrived 2s late:
The alternative would be to wait for lazy images as well. Off-screen lazy images may never load, though, so the grid could stay invisible indefinitely. The masonry documentation now recommends setting The smaller points:
|
Remove the
imagesloadeddependencypat-carousel, pat-equaliser, pat-masonry and pat-scroll now use native browser APIs instead of the
imagesloadedlibrary. Two small helpers incore/domreplace it:dom.on_media_loaded(el, callback, { signal })calls back whenever an image insideelhas loaded or failed, or a video has loaded its metadata (which includes its dimensions). Images and videos added later are covered too. Audio, iframes, scripts, stylesheets and<source>elements are ignored.dom.images_loaded(el, { signal })returns a Promise that resolves once all images insideelhave loaded or failed. Lazy images are not waited for.Both accept an
AbortSignalto stop listening or waiting.Changes in behaviour
masonry-readyis still set once all images have loaded, but images withloading="lazy"are no longer waited for, since they may only load when scrolled into view.imagesloadedused to force them to download.widthandheighton lazy images to avoid layout jumps; the documentation now says so.trigger: auto) waits for all images in the document that haven't loaded yet, except lazy ones, as before. Videos are not waited for: if one never loads its metadata (e.g. withpreload="none"), the page would never scroll.Breaking change
pat-equaliser is now a class-based pattern (
BasePattern):equaliser._update(container)is replaced by the instance methodupdate().Fixes
MutationObservercallback received the list of mutations instead of the container, so it did nothing. It now ignores only equaliser's own changes (theheightstyle, the temporaryoverflowstyles of thegrowtransition and theequalisedclass), which avoids an endless update loop.MutationObserveris disconnected. Previously, the window resize listener was never removed and kept updating containers that were gone.layoutCompletelistener piling up. A new listener was added on everylayout()call.trigger: autoelement removed before its images had loaded threw an error.Not part of this PR
pat-masonry and pat-carousel are still old-style patterns. They have older leaks: masonry's
MutationObserverondocument.bodyis never disconnected, and slick's window listeners are never removed. Fixing these needs a migration toBasePattern, which is planned for a separate PR.🤖 Generated with Claude Code