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): Fallback format not being taken into account properly #8842

Merged
merged 6 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions .changeset/dull-bikes-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix Picture component not taking into account the fallback format specified
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 6 additions & 8 deletions packages/astro/components/Picture.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Props = (LocalImageProps | RemoteImageProps) & {
pictureAttributes?: HTMLAttributes<'picture'>;
};

const { formats = ['webp'], pictureAttributes = {}, ...props } = Astro.props;
const { formats = ['webp'], pictureAttributes = {}, fallbackFormat, ...props } = Astro.props;

if (props.alt === undefined || props.alt === null) {
throw new AstroError(AstroErrorData.ImageMissingAlt);
Expand All @@ -24,16 +24,14 @@ const optimizedImages: GetImageResult[] = await Promise.all(
)
);

const fallbackFormat =
props.fallbackFormat ?? isESMImportedImage(props.src)
? ['svg', 'gif'].includes(props.src.format)
? props.src.format
: 'png'
: 'png';
let resultFallbackFormat = fallbackFormat ?? 'png';
if (isESMImportedImage(props.src) && ['gif', 'svg'].includes(props.src.format)) {
Princesseuh marked this conversation as resolved.
Show resolved Hide resolved
resultFallbackFormat = props.src.format;
}

const fallbackImage = await getImage({
...props,
format: fallbackFormat,
format: resultFallbackFormat,
widths: props.widths,
densities: props.densities,
});
Expand Down
7 changes: 7 additions & 0 deletions packages/astro/src/assets/services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ export const baseService: Omit<LocalImageService, 'transform'> = {
if (options.src.format === 'svg') {
options.format = 'svg';
}

if (
(options.src.format === 'svg' && options.format !== 'svg') ||
(options.src.format !== 'svg' && options.format === 'svg')
) {
throw new AstroError(AstroErrorData.UnsupportedImageConversion);
}
}

// If the user didn't specify a format, we'll default to `webp`. It offers the best ratio of compatibility / quality
Expand Down
15 changes: 15 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,21 @@ export const UnsupportedImageFormat = {
)} are supported by our image services.`,
hint: "Using an `img` tag directly instead of the `Image` component might be what you're looking for.",
} satisfies ErrorData;

/**
* @docs
* @see
* - [Images](https://docs.astro.build/en/guides/images/)
* @description
* Astro does not currently supporting converting between vector (such as SVGs) and raster (such as PNGs and JPEGs) images.
*/
export const UnsupportedImageConversion = {
name: 'UnsupportedImageConversion',
title: 'Unsupported image conversion',
message:
'Converting between vector (such as SVGs) and raster (such as PNGs and JPEGs) images is not currently supported.',
} satisfies ErrorData;

/**
* @docs
* @see
Expand Down
10 changes: 9 additions & 1 deletion packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,16 @@ describe('astro:image', () => {
let html = await res.text();
$ = cheerio.load(html);

// Fallback format
let $img = $('#picture-fallback img');
expect($img).to.have.a.lengthOf(1);

const imageURL = new URL($img.attr('src'), 'http://localhost');
expect(imageURL.searchParams.get('f')).to.equal('jpeg');
expect($img.attr('fallbackformat')).to.be.undefined;

// Densities
let $img = $('#picture-density-2-format img');
$img = $('#picture-density-2-format img');
let $picture = $('#picture-density-2-format picture');
let $source = $('#picture-density-2-format source');
expect($img).to.have.a.lengthOf(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ import myImage from "../assets/penguin1.jpg";
<div id="picture-widths">
<Picture src={myImage} width={Math.round(myImage.width / 2)} alt="A penguin" widths={[myImage.width]} />
</div>

<div id="picture-fallback">
<Picture src={myImage} fallbackFormat="jpeg" alt="A penguin" />
</div>