Image: Address onLoad not being called for SSR images #3612
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
<Image />
accepts anonLoad
callback which ends up being attached to the nativeimg
element'sload
event handler and is triggered once the image resource has finished loading.The problem with this approach is that the
onLoad
handler does not get triggered if the image resource has already finished loading before the<Image />
component is rendered. The most common scenario of this happening is server-side rendering since the image request will be kicked off via the server-rendered HTML and, very likely, be completed by the time client hydration happens.Change
This PR introduces a fix for this by attached a ref callback to the native
img
callback which will do the following:_fixCompletedOnLoad
is true, the img complete attribute is true, and theonLoad
callback has not yet been triggeredonLoad
will a fake load Event. Because the load event has already happened, we need to construct our own.Why is this in an experiment
I discovered this issue when debugging an impression drop with my Masonry V2 experiment. I found that, because my experiment resulted in the initial server rendered Pins not being unmounted/re-mounted, the image onLoad callbacks were never being triggered and consequently, we were not logging impressions.
I'm planning to test this change across both control and enabled groups of the Masonry V2 experiment to validate this and in parallel, will chat with metrics quality about rolling this out since it could have impression impact.