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
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions packages/components/psammead-bulletin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- prettier-ignore -->
| Version | Description |
|---------|-------------|
| 5.0.55 | [PR#4608](https://github.com/bbc/psammead/pull/4608) Bump dependencies |
| 5.0.54 | [PR#4606](https://github.com/bbc/psammead/pull/4606) Bump dependencies |
| 5.0.53 | [PR#4600](https://github.com/bbc/psammead/pull/4600) Fix TalkBack reading nested spans incorrectly |
| 5.0.52 | [PR#4601](https://github.com/bbc/psammead/pull/4601) Bumps dependencies |
Expand Down
2 changes: 1 addition & 1 deletion packages/components/psammead-bulletin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bbc/psammead-bulletin",
"version": "5.0.54",
"version": "5.0.55",
"main": "dist/index.js",
"module": "esm/index.js",
"sideEffects": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,11 @@ exports[`Bulletin should render audio correctly 1`] = `
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Iron man"
Expand Down Expand Up @@ -625,9 +627,11 @@ exports[`Bulletin should render audio correctly with lang prop passed in 1`] = `
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Iron man"
Expand Down Expand Up @@ -986,9 +990,11 @@ exports[`Bulletin should render live audio correctly 1`] = `
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Iron man"
Expand Down Expand Up @@ -1355,9 +1361,11 @@ exports[`Bulletin should render live video correctly 1`] = `
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Iron man"
Expand Down Expand Up @@ -1709,9 +1717,11 @@ exports[`Bulletin should render radio bulletin without ariaId 1`] = `
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Iron man"
Expand Down Expand Up @@ -2029,9 +2039,11 @@ exports[`Bulletin should render radio bulletin without summary correctly 1`] = `
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Iron man"
Expand Down Expand Up @@ -2377,9 +2389,11 @@ exports[`Bulletin should render video correctly 1`] = `
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Iron man"
Expand Down
1 change: 1 addition & 0 deletions packages/components/psammead-grid/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- prettier-ignore -->
| Version | Description |
|---------|-------------|
| 3.1.13 | [PR#4608](https://github.com/bbc/psammead/pull/4608) Bump dependencies |
| 3.1.12 | [PR#4606](https://github.com/bbc/psammead/pull/4606) Bump dependencies |
| 3.1.11 | [PR#4574](https://github.com/bbc/psammead/pull/4574) Bumps psammead-styles |
| 3.1.10 | [PR#4568](https://github.com/bbc/psammead/pull/4568) Bump dependencies |
Expand Down
2 changes: 1 addition & 1 deletion packages/components/psammead-grid/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bbc/psammead-grid",
"version": "3.1.12",
"version": "3.1.13",
"description": "Grid component",
"main": "dist/index.js",
"module": "esm/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1858,9 +1858,11 @@ exports[`Grid component should render Grid with enableGelGutters & margins on on
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/11897/production/_106613817_999_al_.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/11897/production/_106613817_999_al_.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/11897/production/_106613817_999_al_.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/11897/production/_106613817_999_al_.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Robert Downey Junior in Iron Man"
Expand Down
1 change: 1 addition & 0 deletions packages/components/psammead-image/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- prettier-ignore -->
| Version | Description |
|---------|-------------|
| 3.1.0 | [PR#4608](https://github.com/bbc/psammead/pull/4608) Derive mime type from srcset |
| 3.0.1 | [PR#4607](https://github.com/bbc/psammead/pull/4607) Fix amp-img fallback value |
| 3.0.0 | [PR#4606](https://github.com/bbc/psammead/pull/4606) Add support for WebP |
| 2.0.8 | [PR#4486](https://github.com/bbc/psammead/pull/4486) upgrade minor/patch dependencies |
Expand Down
2 changes: 1 addition & 1 deletion packages/components/psammead-image/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bbc/psammead-image",
"version": "3.0.1",
"version": "3.1.0",
"main": "dist/index.js",
"module": "esm/index.js",
"sideEffects": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ exports[`Image - imported as '{ Img }' should render image correctly without wid
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -80,6 +82,7 @@ exports[`Image - imported as '{ Img }' should render image with only srcset corr
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -111,9 +114,11 @@ exports[`Image - imported as '{ Img }' should render image with srcset and fallb
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -232,9 +237,11 @@ exports[`Image - imported as default 'Image' should render image correctly witho
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -294,6 +301,7 @@ exports[`Image - imported as default 'Image' should render image with only srcse
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -325,9 +333,11 @@ exports[`Image - imported as default 'Image' should render image with srcset and
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -446,9 +456,11 @@ exports[`Image - with Fade-in effect' should render image correctly without widt
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -508,6 +520,7 @@ exports[`Image - with Fade-in effect' should render image with only srcset corre
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<img
alt="Student sitting an exam"
Expand Down Expand Up @@ -539,9 +552,11 @@ exports[`Image - with Fade-in effect' should render image with srcset and fallba
>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg.webp 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg.webp 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg.webp 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg.webp 1024w"
type="image/webp"
/>
<source
srcset="https://ichef.bbci.co.uk/news/300/cpsprodpb/7098/production/_104842882_students.jpg 300w, https://ichef.bbci.co.uk/news/450/cpsprodpb/7098/production/_104842882_students.jpg 450w, https://ichef.bbci.co.uk/news/600/cpsprodpb/7098/production/_104842882_students.jpg 600w, https://ichef.bbci.co.uk/news/1024/cpsprodpb/7098/production/_104842882_students.jpg 1024w"
type="image/jpeg"
/>
<img
alt="Student sitting an exam"
Expand Down
36 changes: 34 additions & 2 deletions packages/components/psammead-image/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,45 @@ const StyledImg = styled.img`
width: 100%;
`;

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

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

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;
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.

}
};

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 })}
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

/>
{fallbackSrcset && (
<source
srcSet={fallbackSrcset}
{...(secondaryMimeType && { type: secondaryMimeType })}
/>
)}
<StyledImg src={src} {...otherProps} />
</StyledPicture>
);
Expand Down
13 changes: 13 additions & 0 deletions packages/components/psammead-image/src/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
import { shouldMatchSnapshot } from '@bbc/psammead-test-helpers';
import snapshotTests from './testHelpers/snapshotTests';
import { landscape } from './testHelpers/fixtureData';
Expand Down Expand Up @@ -30,3 +31,15 @@ describe("Image - with Fade-in effect'", () => {
);
snapshotTests(Image);
});

describe("Image - should have mime-types set'", () => {
const props = { ...landscape, width: null };

const { container } = render(<Img {...props} />);

const sourceTags = container.querySelectorAll('source');

expect(sourceTags).toHaveLength(2);
expect(sourceTags[0].type).toEqual('image/webp');
expect(sourceTags[1].type).toEqual('image/jpeg');
});
Loading