Skip to content
This repository has been archived by the owner on Aug 13, 2023. It is now read-only.

Add primaryMimeType and fallbackMimeType props to psammead-image #4608

Merged
merged 11 commits into from
Dec 16, 2021

Conversation

amoore108
Copy link
Contributor

@amoore108 amoore108 commented Dec 15, 2021

Part of bbc/simorgh#9628

Overall change:
Derives the <source /> mime-type by checking the srcsets of the image. This appears to be required by some older browsers that cannot derive the mime-type themselves from the server response.

Code changes:

  • Updates the psammead-image canonical component to check the srcset and fallbackSrcset URLs to determine what the mime-type of the respective <source /> tag should be set to.

  • (BBC contributors only) This PR follows the repository use guidelines
  • I have assigned myself to this PR and the corresponding issues
  • Automated jest tests added (for new features) or updated (for existing features)
  • This PR requires manual testing

@amoore108 amoore108 self-assigned this Dec 15, 2021
{fallbackSrcset && <source srcSet={fallbackSrcset} />}
<source
srcSet={srcset}
{...(primaryMimeType && { type: primaryMimeType })}
Copy link
Contributor

Choose a reason for hiding this comment

The 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

const getMimeType = srcset => {
if (!srcset) return null;

const firstSrcsetUrl = srcset.split(',')[0].split(' ')[0];
Copy link
Contributor

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

Copy link
Contributor Author

@amoore108 amoore108 Dec 15, 2021

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 a mimeType 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

case 'gif':
return 'image/gif';
default:
return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

don't we support the raw extension as well? Are there any other extensions missing here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

raw is used as an iChef recipe, so something like https://ichef.bbci.co.uk/news/raw/myfile.jpg. It doesn't support the '.raw' file extension as it were.

const getMimeType = srcset => {
if (!srcset) return null;

const firstSrcsetUrl = srcset.split(',')[0].split(' ')[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const firstSrcsetUrl = srcset.split(',')[0].split(' ')[0];
// ?. captures an edge case where the srcset passed in is null or invalid
const [ firstSrcSet ] = srcset?.split(',');
const [ firstSrcSetUrl ] = firstSrcSet?.split(' ');

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

@amoore108 amoore108 changed the title Derive image mime type from srcset Add primaryMimeType and fallbackMimeType props to psammead-image Dec 15, 2021
Copy link
Contributor

@jroebu14 jroebu14 left a comment

Choose a reason for hiding this comment

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

The component API looks good. Do you think we should have defaults for the new props though?

@amoore108
Copy link
Contributor Author

amoore108 commented Dec 16, 2021

The component API looks good. Do you think we should have defaults for the new props though?

Its probably safe enough to have defaults of image/jpeg, although we do use PNG a couple of times thoughout, so I wouldn't want to have the situation of a PNG srcset and an image/jpeg type being set for example. Unlikely, but possible I think, although happy to add them if its more sensible?

@amoore108
Copy link
Contributor Author

amoore108 commented Dec 16, 2021

The component API looks good. Do you think we should have defaults for the new props though?

Its probably safe enough to have defaults of image/jpeg, although we do use PNG a couple of times thoughout, so I wouldn't want to have the situation of a PNG srcset and an image/jpeg type being set for example. Unlikely, but possible I think, although happy to add them if its more sensible?

I've added a conditional check for the primary srcset as well and set mime-type defaults. This should cover most cases I think, as anywhere in Simorgh that is using the psammead-image should now be deriving mime-types and srcsets. And if not, it won't render an empty <source /> tag and just return the basic <img /> tag inside the picture element.

@amoore108 amoore108 merged commit 041479e into latest Dec 16, 2021
@amoore108 amoore108 deleted the image-mime-type branch April 13, 2022 12:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants