Skip to content

Commit

Permalink
fix(assets): ensure valid mime types in picture component (#11147)
Browse files Browse the repository at this point in the history
* test: Add test for Picture MIME types

* fix(assets): Fix MIME type generation in Picture component

* chore: changeset

* fix: Trust mrmime to handle an undefined lookup argument

* fix: Use image.src as fallback argument to mrmime

---------

Co-authored-by: Erika <[email protected]>
  • Loading branch information
kitschpatrol and Princesseuh authored May 27, 2024
1 parent cdf89a1 commit 2d93902
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/serious-garlics-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes invalid MIME types in Picture source elements for jpg and svg extensions, which was preventing otherwise valid source variations from being shown by the browser
3 changes: 2 additions & 1 deletion packages/astro/components/Picture.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import { type LocalImageProps, type RemoteImageProps, getImage } from 'astro:assets';
import * as mime from 'mrmime';
import type { GetImageResult, ImageOutputFormat } from '../dist/@types/astro';
import { isESMImportedImage, resolveSrc } from '../dist/assets/utils/imageKind';
import { AstroError, AstroErrorData } from '../dist/core/errors/index.js';
Expand Down Expand Up @@ -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.src) ?? `image/${image.options.format}`}
{...sourceAdditionalAttributes}
/>
);
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,26 @@ describe('astro:image', () => {
srcset2.map((src) => src.w),
[207]
);

// MIME Types
const validMimeTypes = [
'image/webp',
'image/jpeg',
'image/avif',
'image/png',
'image/gif',
'image/svg+xml',
];

const $sources = $('#picture-mime-types picture source');
for ($source of $sources) {
const type = $source.attribs.type;
assert.equal(
validMimeTypes.includes(type),
true,
`Expected type attribute value to be a valid MIME type: ${type}`
);
}
});

it('Picture component scope styles work', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
import { Picture } from "astro:assets";
import myImage from "../assets/penguin1.jpg";
import myImageSvg from '../assets/penguin.svg';
---

<div id="picture-density-2-format">
Expand All @@ -19,6 +20,11 @@ import myImage from "../assets/penguin1.jpg";
<Picture src={myImage} fallbackFormat="jpeg" alt="A penguin" class="img-comp" pictureAttributes={{ class: 'picture-comp' }} />
</div>

<div id="picture-mime-types">
<Picture alt="A penguin" src={myImage} formats={['jpg', 'jpeg', 'png', 'avif', 'webp']} />
<Picture alt="A penguin" src={myImageSvg} />
</div>

<style>
.img-comp {
border: 5px solid blue;
Expand Down

0 comments on commit 2d93902

Please sign in to comment.