next-sanity
Version:
Sanity.io toolkit for Next.js
36 lines (35 loc) • 1.77 kB
JavaScript
"use client";
;
Object.defineProperty(exports, "__esModule", { value: !0 });
var jsxRuntime = require("react/jsx-runtime"), NextImage = require("next/image");
function _interopDefaultCompat(e) {
return e && typeof e == "object" && "default" in e ? e : { default: e };
}
var NextImage__default = /* @__PURE__ */ _interopDefaultCompat(NextImage);
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__ */ jsxRuntime.jsx(NextImage__default.default, { ...rest, src: srcUrl.toString(), loader: imageLoader });
}
exports.Image = Image;
exports.imageLoader = imageLoader;
//# sourceMappingURL=image.cjs.map