optimal-img-format
v1.5.0
Published
Get optimal image format on the browser or server.
Downloads
1,322
Maintainers
Readme
optimal-img-format
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
Usage
Install
npm install optimal-img-format
Formats
Formats refer to the subtypes of the MIME type images/*
.
- jxl
- avif
- webp
Browser
import {
getOptimalImgFormatOnBrowser,
isSupportWebP,
} from "optimal-img-format";
const format = await getOptimalImgFormatOnBrowser(["avif", "webp"]);
const formatWebp = await isSupportWebP();
Detecting browser supports by:
Server
import {
getOptimalImgFormatByAccept,
getOptimalImgFormatByAgent,
} from "optimal-img-format";
const format =
getOptimalImgFormatByAccept(request.headers.accept, ["avif", "webp"]) ||
getOptimalImgFormatByAgent(request.headers.agent, ["avif", "webp"]);
Changelog
Contributions
If you have any suggestions for this project or discover a bug, please raise an Issue.