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

[React] Adding srcSet to Image Component removes the src Attribute reducing compatibility with older browsers #994

Merged
merged 1 commit into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
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
31 changes: 31 additions & 0 deletions packages/sitecore-jss-react/src/components/Image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@ describe('<Image />', () => {
});
});

describe('with responsive image object', () => {
const props = {
media: {
src: '/assets/img/test0.png',
},
srcSet: [{ mw: 100 }, { mw: 300 }],
sizes: '(min-width: 960px) 300px, 100px',
id: 'some-id',
className: 'the-dude-abides',
};

const rendered = mount(<Image {...props} />).find('img');

it('should render <img /> with needed img tags', () => {
expect(rendered).to.have.length(1);
expect(rendered.prop('src')).to.equal(props.media.src);
expect(rendered.prop('srcSet')).to.equal(
'/assets/img/test0.png?mw=100 100w, /assets/img/test0.png?mw=300 300w'
);
expect(rendered.prop('sizes')).to.equal('(min-width: 960px) 300px, 100px');
});

it('should render <img /> with non-media props', () => {
expect(rendered.prop('id')).to.equal(props.id);
});

it('should render <img /> with style and className props', () => {
expect(rendered.prop('className')).to.eql(props.className);
});
});

describe('with "value" property value', () => {
const props = {
media: { value: { src: '/assets/img/test0.png', alt: 'my image' } },
Expand Down
6 changes: 3 additions & 3 deletions packages/sitecore-jss-react/src/components/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ const getImageAttrs = (
if (srcSet) {
// replace with HTML-formatted srcset, including updated image URLs
newAttrs.srcSet = mediaApi.getSrcSet(resolvedSrc, srcSet, imageParams);
} else {
newAttrs.src = resolvedSrc;
}
// always output original src as fallback for older browsers
newAttrs.src = resolvedSrc;
return newAttrs;
};

Expand Down Expand Up @@ -176,4 +176,4 @@ Image.defaultProps = {
editable: true,
};

Image.displayName = 'Image';
Image.displayName = 'Image';