If the script is enqueued / printed in another location, it may be that some images do not get transformed so that they don't get a proper src attribute, causing them to not be loaded at all.
Possible Solution
By wrapping the script to run on DOMContentLoaded, it is ensured it catches all images initially included in the markup.
Issue Overview
If the script is enqueued / printed in another location, it may be that some images do not get transformed so that they don't get a proper
src
attribute, causing them to not be loaded at all.Possible Solution
By wrapping the script to run on
DOMContentLoaded
, it is ensured it catches all images initially included in the markup.Potentially related (Autoptimize):