Get optimal image format on the browser and server.
- Support for AVIF, WebP and JPEG XL
- Zero dependency
- Fast, using promise singleton and cache for detecting
npm install optimal-img-format
Formats refer to the subtypes of the MIME type images/*
.
- jxl
- avif
- webp
import {
getOptimalImgFormatOnBrowser,
isSupportWebP,
} from "optimal-img-format";
const format = await getOptimalImgFormatOnBrowser(["avif", "webp"]);
const formatWebp = await isSupportWebP();
Detecting browser supports by:
import {
getOptimalImgFormatByAccept,
getOptimalImgFormatByAgent,
} from "optimal-img-format";
const format =
getOptimalImgFormatByAccept(request.headers.accept, ["avif", "webp"]) ||
getOptimalImgFormatByAgent(request.headers.agent, ["avif", "webp"]);
If you have any suggestions for this project or discover a bug, please raise an Issue.