phlex-reactive 0.16.9 → 0.16.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -449,7 +449,9 @@ export function __resetReactiveKeepForTest() {
449
449
  // a morph that changes the src lets the browser try again. A morph that keeps
450
450
  // the src puts the state back. An error with no src is not an error (a
451
451
  // removed or emptied src). One document listener, capture phase (neither
452
- // event bubbles); nothing per root at connect.
452
+ // event bubbles); nothing per root at connect. An img that finished before it
453
+ // was in the document (before registration, or while Turbo held it imported
454
+ // but not yet inserted, issue #452) is read off the element instead.
453
455
  const IMAGE_FALLBACK = "data-reactive-image-fallback"
454
456
  const IMAGE_STATE = "data-reactive-image-state"
455
457
  // Marks the `hidden` the fallback set, so a reset removes only that one. On
@@ -580,13 +582,36 @@ export function registerReactiveImageFallback() {
580
582
  if (wire) setImageState(img, wire, value)
581
583
  })
582
584
  },
585
+ // Content Turbo inserts was imported into the document first, a few awaits
586
+ // earlier (issue #452): an img loads from then on, so one whose request
587
+ // ended in between fired its event detached, unheard. Read it off the
588
+ // element once the content is in: the frame, the page, a stream's render.
589
+ "turbo:frame-render": (event) => scanImageFallbacks(event.target),
590
+ "turbo:render": () => scanImageFallbacks(document),
591
+ "turbo:before-stream-render": (event) => {
592
+ const detail = event.detail
593
+ const render = detail?.render
594
+ const stream = detail?.newStream ?? event.target
595
+ if (typeof render !== "function" || !stream?.querySelector?.("template")?.content?.querySelector?.(`img[${IMAGE_FALLBACK}]`)) return
596
+ // The whole document: a replace's target is gone by then.
597
+ detail.render = async (el) => {
598
+ await render(el)
599
+ scanImageFallbacks(document)
600
+ }
601
+ },
583
602
  }
584
603
  for (const [name, fn] of Object.entries(imageFallbackListeners)) document.addEventListener(name, fn, true)
585
- // An image that finished before this ran (a cached 404) fired its event
586
- // unheard: read it off the element once. naturalWidth 0 is also a loaded
587
- // SVG with only a viewBox, so decode() decides (it rejects a broken image).
588
- for (const img of document.querySelectorAll?.(`img[${IMAGE_FALLBACK}]`) ?? []) {
589
- if (!img.complete || !hasImageSrc(img)) continue
604
+ // An image that finished before this ran (a cached 404) fired its event unheard.
605
+ scanImageFallbacks(document)
606
+ }
607
+
608
+ // Read the result of every marked img in `root` that has no state yet and has
609
+ // finished; one still loading fires its own event, connected. naturalWidth 0
610
+ // is also a loaded SVG with only a viewBox, so decode() decides (it rejects a
611
+ // broken image, and fetches nothing).
612
+ function scanImageFallbacks(root) {
613
+ for (const img of root?.querySelectorAll?.(`img[${IMAGE_FALLBACK}]`) ?? []) {
614
+ if (img.getAttribute(IMAGE_STATE) != null || !img.complete || !hasImageSrc(img)) continue
590
615
  // A decode() that settles after the src changed decoded the OLD one: ignore it.
591
616
  const was = `${img.getAttribute("src")} ${img.getAttribute("srcset")}`
592
617
  const mark = (value) => `${img.getAttribute("src")} ${img.getAttribute("srcset")}` === was && setImageState(img, imageFallback(img) ?? {}, value)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Reactive
5
- VERSION = "0.16.9"
5
+ VERSION = "0.16.10"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-reactive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.9
4
+ version: 0.16.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson