next-sanity
Version:
Sanity.io toolkit for Next.js
33 lines (32 loc) • 1.45 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);
if (url.searchParams.set("auto", "format"), url.searchParams.has("fit") || url.searchParams.set("fit", url.searchParams.has("h") ? "min" : "max"), url.searchParams.has("h") && url.searchParams.has("w")) {
const originalHeight = parseInt(url.searchParams.get("h"), 10), originalWidth = parseInt(url.searchParams.get("w"), 10);
url.searchParams.set("h", Math.round(originalHeight / originalWidth * width).toString());
}
return url.searchParams.set("w", width.toString()), quality && url.searchParams.set("q", quality.toString()), 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), props.height && srcUrl.searchParams.set("h", `${props.height}`), 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=image.js.map