-
Notifications
You must be signed in to change notification settings - Fork 54
Add primaryMimeType
and fallbackMimeType
props to psammead-image
#4608
Changes from 7 commits
495748b
a8ee719
d2381ec
f9addb1
279c2d3
e95d233
ecb01b7
48268f0
1b1bbcd
3340925
24db1f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -32,13 +32,45 @@ const StyledImg = styled.img` | |||||||||
width: 100%; | ||||||||||
`; | ||||||||||
|
||||||||||
const getMimeType = srcset => { | ||||||||||
if (!srcset) return null; | ||||||||||
|
||||||||||
const firstSrcsetUrl = srcset.split(',')[0].split(' ')[0]; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This reads a bit more clearly imo. The main bit is we need to be a bit defensive if the src set is invalid, so null or contains an unexpected format. I believe if the srcset was invalid in this way the page would not render at all with the code as it is; I'd write a test to cover this eventuality. I'd agree with @jroebu14 that this logic may be better to be in Simorgh, is that where the srcset is constructed; you could pass the mime type in as a prop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll move this logic over to Simorgh and pass through the mime-types as props for each of the srcsets |
||||||||||
const urlFileExtension = firstSrcsetUrl.split('.').pop(); | ||||||||||
|
||||||||||
switch (urlFileExtension) { | ||||||||||
case 'webp': | ||||||||||
return 'image/webp'; | ||||||||||
case 'jpg': | ||||||||||
case 'jpeg': | ||||||||||
return 'image/jpeg'; | ||||||||||
case 'png': | ||||||||||
return 'image/png'; | ||||||||||
case 'gif': | ||||||||||
return 'image/gif'; | ||||||||||
default: | ||||||||||
return null; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. don't we support the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
} | ||||||||||
}; | ||||||||||
|
||||||||||
export const Img = props => { | ||||||||||
const { src, srcset, fallbackSrcset, onLoad, ...otherProps } = props; | ||||||||||
|
||||||||||
const primaryMimeType = getMimeType(srcset); | ||||||||||
const secondaryMimeType = getMimeType(fallbackSrcset); | ||||||||||
|
||||||||||
return ( | ||||||||||
<StyledPicture onLoad={onLoad}> | ||||||||||
<source srcSet={srcset} /> | ||||||||||
{fallbackSrcset && <source srcSet={fallbackSrcset} />} | ||||||||||
<source | ||||||||||
srcSet={srcset} | ||||||||||
{...(primaryMimeType && { type: primaryMimeType })} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure you need to conditionally spread like this. I think if the value is null then the attribute doesn't get rendered |
||||||||||
/> | ||||||||||
{fallbackSrcset && ( | ||||||||||
<source | ||||||||||
srcSet={fallbackSrcset} | ||||||||||
{...(secondaryMimeType && { type: secondaryMimeType })} | ||||||||||
/> | ||||||||||
)} | ||||||||||
<StyledImg src={src} {...otherProps} /> | ||||||||||
</StyledPicture> | ||||||||||
); | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just wondering if this is easier to work out in simorgh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think its much of a muchness really. It would probably be a bit more work in Simorgh to pass through amimeType
prop or something similar to the Psammead component, with similar checking done in Simorgh. Conscious not having too much rendering logic in Psammead though.Moved this over to Simorgh and passed through as props