@samitouri / QOS-React / commits / 7f217d1d88

[Fiber] use srcset to trigger load even on img mount (#30351)

In https://github.com/facebook/react/pull/23316 we fixed a bug where onload events were missed if they happened too early. This update adds support for srcset to retrigger the load event. Firefox unfortunately does not trigger a load even when you assign srcset so this won't work in every browser when you use srcset without src however it does close a gap in chrome at least

Josh Story committed Jul 25, 2024 at 15:46 UTC 7f217d1d88d3b628d97a714ce1573526080af47d
1 file changed +11
packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js
+11
@@ -692,8 +692,19 @@ export function commitMount(
692 }
693 return;
694 case 'img': {
695 + // The technique here is to assign the src or srcSet property to cause the browser
696 + // to issue a new load event. If it hasn't loaded yet it'll fire whenever the load actually completes.
697 + // If it has already loaded we missed it so the second load will still be the first one that executes
698 + // any associated onLoad props.
699 + // Even if we have srcSet we prefer to reassign src. The reason is that Firefox does not trigger a new
700 + // load event when only srcSet is assigned. Chrome will trigger a load event if either is assigned so we
701 + // only need to assign one. And Safari just never triggers a new load event which means this technique
702 + // is already a noop regardless of which properties are assigned. We should revisit if browsers update
703 + // this heuristic in the future.
704 if ((newProps: any).src) {
705 ((domElement: any): HTMLImageElement).src = (newProps: any).src;
706 + } else if ((newProps: any).srcSet) {
707 + ((domElement: any): HTMLImageElement).srcset = (newProps: any).srcSet;
708 }
709 return;
710 }