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

fix(assets): ensure valid mime types in picture component #11147

Conversation

kitschpatrol
Copy link
Contributor

@kitschpatrol kitschpatrol commented May 25, 2024

Changes

Testing

Added MIME type test to core-image.test.js alongside existing Picture component validation tests.

Docs

N/A, the Picture component should simply now work as expected.

Verified

This commit was signed with the committer’s verified signature.
henryiii Henry Schreiner

Verified

This commit was signed with the committer’s verified signature.
henryiii Henry Schreiner

Verified

This commit was signed with the committer’s verified signature.
mayeut Matthieu Darbois
Copy link

changeset-bot bot commented May 25, 2024

🦋 Changeset detected

Latest commit: f203342

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label May 25, 2024
@kitschpatrol kitschpatrol changed the title Fix/invalid mime type in picture component fix(assets): invalid mime type in picture component May 25, 2024
@kitschpatrol kitschpatrol changed the title fix(assets): invalid mime type in picture component fix(assets): ensure valid mime types in picture component May 25, 2024
@@ -99,7 +100,7 @@ if (import.meta.env.DEV) {
return (
<source
srcset={srcsetAttribute}
type={'image/' + image.options.format}
type={mime.lookup(image.options.format ?? '') ?? `image/${image.options.format}`}
Copy link
Member

Choose a reason for hiding this comment

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

Instead of '', you can pass the src of the image. mrmime should be able to handle it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok! That was to appease TypeScript, since lookup's definition only accepts a string, but I've confirmed that mrmime correctly returns undefined if given undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh I see pass the source, read too quickly.

Copy link
Contributor Author

@kitschpatrol kitschpatrol May 25, 2024

Choose a reason for hiding this comment

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

Hmm passing image.src works in production builds, but in dev mrmime doesn't know what to make of the URL:

import * as mrmime from 'mrmime'

// Build
mrmime.lookup('http://localhost:4321/_astro/penguin1.wSAOfMN3_ZYmXno.jpg')
// image/jpeg


// Dev
mrmime.lookup('http://localhost:4321/_image?href=%2F%40fs%2FUsers%2Fmika%2FCode%2Fastro%2Fpackages%2Fastro%2Ftest%2Ffixtures%2Fcore-image%2Fsrc%2Fassets%2Fpenguin1.jpg%3ForigWidth%3D207%26origHeight%3D243%26origFormat%3Djpg&f=jpg') 
// undefined

I'm not sure how common an undefined image.options.format is? Presumably not very since it wasn't handled in the previous implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed a commit with this, just noting the above caveat. It doesn't materially change the undefined edge case.

@Princesseuh
Copy link
Member

This is fine with me, I think that ultimately there's a potential better fix in the image service API itself, but that's for me to think about in the future - this is great!

Verified

This commit was signed with the committer’s verified signature.
mayeut Matthieu Darbois
Copy link
Member

@Princesseuh Princesseuh left a comment

Choose a reason for hiding this comment

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

This looks great, thank you very much! As I said, we'll do a proper overhaul of this in the future, one day, maybe. But this works perfectly fine in the meantime!

@Princesseuh Princesseuh merged commit 2d93902 into withastro:main May 27, 2024
4 checks passed
@astrobot-houston astrobot-houston mentioned this pull request May 27, 2024
@kitschpatrol
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid MIME type in Picture component
2 participants