next-sanity
Version:
Sanity.io toolkit for Next.js
36 lines (35 loc) • 1.43 kB
JavaScript
"use client";
import { jsx } from "react/jsx-runtime";
import NextImage from "next/image";
const imageLoader = ({ src, width, quality }) => {
const url = new URL(src);
url.searchParams.set("auto", "format");
if (!url.searchParams.has("fit")) url.searchParams.set("fit", url.searchParams.has("h") ? "min" : "max");
if (url.searchParams.has("h") && url.searchParams.has("w")) {
const originalHeight = parseInt(url.searchParams.get("h"), 10);
const originalWidth = parseInt(url.searchParams.get("w"), 10);
url.searchParams.set("h", Math.round(originalHeight / originalWidth * width).toString());
}
url.searchParams.set("w", width.toString());
if (quality) url.searchParams.set("q", quality.toString());
return url.href;
};
function Image(props) {
const { loader, src,...rest } = props;
if (loader) throw new TypeError("The `loader` prop is not supported on `Image` components. Use `next/image` directly to use a custom loader.");
let srcUrl;
try {
srcUrl = new URL(src);
if (props.height) srcUrl.searchParams.set("h", `${props.height}`);
if (props.width) srcUrl.searchParams.set("w", `${props.width}`);
} catch (err) {
throw new TypeError("The `src` prop must be a valid URL to an image on the Sanity Image CDN.", { cause: err });
}
return /* @__PURE__ */ jsx(NextImage, {
...rest,
src: srcUrl.toString(),
loader: imageLoader
});
}
export { Image, imageLoader };
//# sourceMappingURL=index.js.map