Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image: Address onLoad not being called for SSR images #3612

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/gestalt/src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ type Props = {
* A comma-separated list of one or more strings indicating a set of possible image sources for the user agent to use.
*/
srcSet?: string;
/**
* Experimental prop: Fixes an issue where onLoad is not triggered if the image is already loaded by the time the component renders (i.e. SSR)
*/
_fixCompletedOnLoad?: boolean;
};

/**
Expand All @@ -102,6 +106,8 @@ export default class Image extends PureComponent<Props> {

static displayName: string | null | undefined = 'Image';

onLoadCalled: boolean = false;

componentDidMount() {
if (shouldScaleImage(this.props.fit)) {
this.loadImage();
Expand All @@ -116,6 +122,7 @@ export default class Image extends PureComponent<Props> {
}

handleLoad: (event: React.SyntheticEvent<HTMLImageElement>) => void = (event) => {
this.onLoadCalled = true;
this.props.onLoad?.({ event });
};

Expand All @@ -134,6 +141,30 @@ export default class Image extends PureComponent<Props> {
}
}

refCallback = (node: HTMLImageElement | null) => {
const { _fixCompletedOnLoad } = this.props;
// For certain scenarios, such as server-side rendering, the image may already be loaded by the time the component is rendered resulting in the onLoad event not being triggered.
// To address these, we can use a ref callback and check whether the image is already loaded - if it is, we trigger the onLoad event manually.
if (_fixCompletedOnLoad && node?.complete && !this.onLoadCalled) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!this.onLoadCalled

so handleLoad when image loads and we set this.onLoadCalled = true;. Then, when it renders we set the ref and we check onLoadCalled

handleLoad gets sets manually if this.onLoadCalled = true;.

"if it is, we trigger the onLoad event manually."

should && !this.onLoadCalled be && this.onLoadCalled

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!this.onLoadCalled is here to guard against a scenario where the native img load callback is fired (and onLoad called) before the ref callback

i think the only situation where this could happen is:

  • image component renders
  • image request completes very quickly
  • ref callback is triggered

i think this would be an edge case but wanted to play it safe

// Since we don't have the SyntheticEvent here,
// we must create one with the same shape.
// See https://reactjs.org/docs/events.html
const loadEvent = new Event('load');
Object.defineProperty(loadEvent, 'target', { writable: false, value: node });
this.handleLoad({
...loadEvent,
nativeEvent: loadEvent,
currentTarget: node,
target: node,
isDefaultPrevented: () => false,
isPropagationStopped: () => false,
persist: () => {},
preventDefault: () => {},
stopPropagation: () => {},
});
}
};

render() {
const {
alt,
Expand Down Expand Up @@ -183,6 +214,7 @@ export default class Image extends PureComponent<Props> {
{...(isScaledImage ? { height: '100%' } : {})}
>
<img
ref={this.refCallback}
alt={alt}
className={imageStyles}
crossOrigin={crossOrigin}
Expand Down
Loading