Skip to content

remove dependency on imagesloaded - #1306

Merged
thet merged 15 commits into
masterfrom
thet/rm-imagesloaded
Nov 28, 2026
Merged

thet merged 15 commits into
masterfrom
thet/rm-imagesloaded

Conversation

@thet

@thet thet commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

Remove the imagesloaded dependency

pat-carousel, pat-equaliser, pat-masonry and pat-scroll now use native browser APIs instead of the imagesloaded library. Two small helpers in core/dom replace it:

  • dom.on_media_loaded(el, callback, { signal }) calls back whenever an image inside el has 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 inside el have loaded or failed. Lazy images are not waited for.

Both accept an AbortSignal to stop listening or waiting.

Changes in behaviour

  • pat-masonry
    • Re-lays out whenever an image or video loads, including ones added later.
    • masonry-ready is still set once all images have loaded, but images with loading="lazy" are no longer waited for, since they may only load when scrolled into view. imagesloaded used to force them to download.
    • Set width and height on lazy images to avoid layout jumps; the documentation now says so.
  • pat-carousel
    • Starts immediately instead of waiting for images, and recalculates its dimensions when images or videos load.
    • Until then, a carousel with images that have no dimensions can briefly show the wrong size.
  • pat-equaliser equalises immediately on initialization and again when images or videos load, when the window is resized and when the DOM changes.
  • pat-scroll (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. with preload="none"), the page would never scroll.

Breaking change

pat-equaliser is now a class-based pattern (BasePattern):

  • The module's default export is the pattern class.
  • equaliser._update(container) is replaced by the instance method update().
  • No other code we know of uses it.

Fixes

  • pat-equaliser: never re-equalised on DOM changes. The MutationObserver callback received the list of mutations instead of the container, so it did nothing. It now ignores only equaliser's own changes (the height style, the temporary overflow styles of the grow transition and the equalised class), which avoids an endless update loop.
  • pat-equaliser: leaked after removal. It is now destroyed automatically when its element is removed. Its listeners are removed and its MutationObserver is disconnected. Previously, the window resize listener was never removed and kept updating containers that were gone.
  • pat-masonry: layoutComplete listener piling up. A new listener was added on every layout() call.
  • pat-scroll: error after the element was removed. It no longer tries to scroll after its element was removed. Previously, a trigger: auto element 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 MutationObserver on document.body is never disconnected, and slick's window listeners are never removed. Fixing these needs a migration to BasePattern, which is planned for a separate PR.

🤖 Generated with Claude Code

@thet
thet requested a review from petschki November 26, 2026 11:14

@petschki petschki left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 load from 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 resize listener 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.

thet added 15 commits November 28, 2026 03:34
…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>
@thet
thet force-pushed the thet/rm-imagesloaded branch from e3a1f45 to bdb44e6 Compare November 28, 2026 04:05
@thet

thet commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

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 height style and the equalised class. A class or style that other code sets on a column (your pat-collapsible example) triggers a new equalisation again.

While doing this I found that jQuery's animate, used by transition: grow, also sets overflow: hidden on the columns while it runs. If only height were ignored, the animation would trigger itself again. So the temporary overflow styles are ignored too. Tests cover other code changing a column's class or style, and confirm that the grow animation doesn't loop.

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 window load. It waits for all images in the document that haven't loaded yet, like imagesloaded(document.body) did. Lazy images are the exception, because they may only load when scrolled into view.

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:

  • Normal images: the grid stays hidden until all of them have loaded, as before.
  • Lazy images with width/height: the final layout appears straight away and the images fill their reserved spaces. No jumps.
  • Lazy images without dimensions: the items appear straight away, collapsed, and rearrange as the images arrive.

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 width and height on lazy images.

The smaller points:

  • The listener also catching load from iframes, scripts and stylesheets: there's a new helper, dom.on_media_loaded. It reacts only to images loading or failing, and to videos loading their metadata (a video without fixed dimensions changes size when that arrives). Audio, iframes, scripts, stylesheets and <source> elements are ignored. Masonry, equaliser and carousel use it. A second helper, dom.images_loaded, returns a Promise and is used by masonry (for masonry-ready) and by pat-scroll. Both helpers accept an AbortSignal.
  • Listeners never removed: after rebasing on master, pat-equaliser is now a BasePattern ("breaking(pat-equaliser): Migrate to a class based pattern."). It's destroyed automatically when its element is removed: the resize listener goes, the image and video listeners go, and the MutationObserver is disconnected straight away rather than at the next change. pat-scroll also stops listening and waiting on destroy. Previously, a trigger: auto element removed before its images loaded still tried to scroll and threw an error.
  • pat-masonry and pat-carousel are still old-style patterns. Their listeners sit on their own element, so they're garbage-collected with it. The older leaks (masonry's observer watching the whole page, slick's window listeners) need a migration to BasePattern, which I'd do in a separate PR.
  • The carousel showing wrong dimensions before its images load: that's still the case. It's the tradeoff of starting slick right away. It recalculates as soon as images or videos arrive.
  • The duplicated autoformat commit: gone with the rebase on master after feat(pat-validation): Support definition of minimum or maximum number of selections. #1249 was merged.
  • The PR description: I'll add a summary of the behaviour changes for the release notes.

@thet
thet merged commit 04ba362 into master Nov 28, 2026
1 check passed
@thet
thet deleted the thet/rm-imagesloaded branch November 28, 2026 04:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants