-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
fix(assets): ensure valid mime types in picture component #11147
Conversation
🦋 Changeset detectedLatest 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 |
@@ -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}`} |
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.
Instead of ''
, you can pass the src of the image. mrmime should be able to handle it
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.
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
.
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.
Oh I see pass the source, read too quickly.
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.
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?
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.
Pushed a commit with this, just noting the above caveat. It doesn't materially change the undefined edge case.
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! |
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.
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!
Thanks! |
Changes
mrmime
dependency to ensure valid mapping between file name extensions and image MIME typesTesting
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.