Skip to content

Commit ec1e35c

Browse files
Marcosldclaude
andauthored
fix(Image): handle SSR image already complete before inline script runs (#1536)
## Problem The inline `<script>` rendered after `<img>` in SSR attached a `load` listener to set the image opacity to `1`. When the image finished loading before that script was parsed, the listener never fired and the image stayed at `opacity: 0`, producing flaky snapshot diffs in the SSR acceptance test. ## Fix Check `img.complete` first and set `opacity = "1"` synchronously in that case; fall back to the `load` listener otherwise. Ref: WEB-2429 Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent bc3574f commit ec1e35c

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

src/image.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,14 @@ export const ImageContent = React.forwardRef<HTMLImageElement, ImageProps>(
281281
<script
282282
// eslint-disable-next-line react/no-danger
283283
dangerouslySetInnerHTML={{
284-
__html: `document.getElementById("${imageId}").addEventListener('load', (e) => e.target.style.opacity = "1")`,
284+
__html: `(function () {
285+
var img = document.getElementById("${imageId}");
286+
if (img.complete) {
287+
img.style.opacity = "1";
288+
} else {
289+
img.addEventListener('load', function (e) { e.target.style.opacity = "1"; });
290+
}
291+
})();`,
285292
}}
286293
/>
287294
)}

0 commit comments

Comments
 (0)