Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 9f9ab5b

Browse files
authored
feat(core): enabling avif support (#2126)
1 parent 5308f80 commit 9f9ab5b

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

packages/libs/core/src/images/imageOptimizer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ import fetch from "node-fetch";
2828
import { PlatformClient } from "../platform";
2929

3030
let sharp: typeof import("sharp");
31-
//const AVIF = 'image/avif'
31+
const AVIF = "image/avif";
3232
const WEBP = "image/webp";
3333
const PNG = "image/png";
3434
const JPEG = "image/jpeg";
3535
const GIF = "image/gif";
3636
const SVG = "image/svg+xml";
3737
const CACHE_VERSION = 2;
38-
const MODERN_TYPES = [/* AVIF, */ WEBP];
38+
const MODERN_TYPES = [AVIF, WEBP];
3939
const ANIMATABLE_TYPES = [WEBP, PNG, GIF];
4040
const VECTOR_TYPES = [SVG];
4141

@@ -340,10 +340,9 @@ export async function imageOptimizer(
340340
transformer.resize(width);
341341
}
342342

343-
//if (contentType === AVIF) {
344-
// Soon https://github.com/lovell/sharp/issues/2289
345-
//}
346-
if (contentType === WEBP) {
343+
if (contentType === AVIF) {
344+
transformer.avif({ quality });
345+
} else if (contentType === WEBP) {
347346
transformer.webp({ quality });
348347
} else if (contentType === PNG) {
349348
transformer.png({ quality });

packages/libs/core/tests/images/imageOptimizer.test.ts

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,37 @@ describe("Image optimizer", () => {
129129

130130
describe("Routes", () => {
131131
it.each`
132-
imagePath | expectedObjectKey
133-
${"/test-image.png"} | ${"public/test-image.png"}
134-
${"/static/test-image.png"} | ${"static/test-image.png"}
135-
${"/_next/static/test-image.png"} | ${"_next/static/test-image.png"}
136-
`("serves image request", async ({ imagePath, expectedObjectKey }) => {
137-
const { parsedUrl, req, res } = createEventByImagePath(imagePath);
138-
139-
await imageOptimizer(
140-
"",
141-
imagesManifest as ImagesManifest,
142-
req,
143-
res,
144-
parsedUrl,
145-
mockPlatformClient as PlatformClient
146-
);
147-
148-
expect(res.getHeaders()).toEqual({
149-
"cache-control": "public, max-age=60",
150-
etag: expect.any(String),
151-
"content-type": "image/webp"
152-
});
153-
expect(res.statusCode).toEqual(200);
154-
155-
expect(mockPlatformClient.getObject).toBeCalledWith(expectedObjectKey);
156-
});
132+
imagePath | accept | expectedObjectKey
133+
${"/test-image.png"} | ${"image/avif"} | ${"public/test-image.png"}
134+
${"/test-image.png"} | ${"image/webp"} | ${"public/test-image.png"}
135+
${"/static/test-image.png"} | ${"image/webp"} | ${"static/test-image.png"}
136+
${"/_next/static/test-image.png"} | ${"image/webp"} | ${"_next/static/test-image.png"}
137+
`(
138+
"serves image request",
139+
async ({ imagePath, accept, expectedObjectKey }) => {
140+
const { parsedUrl, req, res } = createEventByImagePath(imagePath, {
141+
accept: accept
142+
});
143+
144+
await imageOptimizer(
145+
"",
146+
imagesManifest as ImagesManifest,
147+
req,
148+
res,
149+
parsedUrl,
150+
mockPlatformClient as PlatformClient
151+
);
152+
153+
expect(res.getHeaders()).toEqual({
154+
"cache-control": "public, max-age=60",
155+
etag: expect.any(String),
156+
"content-type": accept
157+
});
158+
expect(res.statusCode).toEqual(200);
159+
160+
expect(mockPlatformClient.getObject).toBeCalledWith(expectedObjectKey);
161+
}
162+
);
157163

158164
it.each`
159165
imagePath

0 commit comments

Comments
 (0)