Skip to content
Open
Show file tree
Hide file tree
Changes from all 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/fix-animated-avif.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes build crash when processing animated AVIF images. Sharp now gracefully passes through unsupported image formats instead of crashing during the build.
9 changes: 8 additions & 1 deletion packages/astro/src/assets/services/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ const sharpService: LocalImageService<SharpImageServiceConfig> = {
result.rotate();

// get some information about the input
const { format } = await result.metadata();
let format: string | undefined;
try {
({ format } = await result.metadata());
} catch {
// Sharp cannot decode this image (e.g. animated AVIF sequences).
// Pass it through unmodified rather than crashing the build.
return { data: inputBuffer, format: transform.format };
}

if (transform.width && transform.height) {
const fit: keyof FitEnum | undefined = transform.fit
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/core-image.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,16 @@ describe('astro:image', () => {
);
});

it('animated avif does not crash the build', async () => {
const html = await fixture.readFile('/animated-avif/index.html');
const $ = cheerio.load(html);
const src = $('#animated-avif img').attr('src')!;
assert.ok(src, 'expected img src to be set');
const data = await fixture.readBuffer(src);
assert.equal(data instanceof Buffer, true);
assert.ok(data.byteLength > 0);
});

it('markdown images are written', async () => {
const html = await fixture.readFile('/post/index.html');
const $ = cheerio.load(html);
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
import { Image } from 'astro:assets';
import animatedAvif from '../assets/animated.avif';
---
<html>
<body>
<div id="animated-avif">
<Image src={animatedAvif} alt="animated avif" />
</div>
</body>
</html>
Loading