File size: 756 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import Image from "next/image";
const DEFAULT_QUALITY = 20;

// https://dotcms.com/docs/latest/image-resizing-and-processing
const getUrlWithResizingParameters = ({
  src,
  width,
  quality = DEFAULT_QUALITY,
}) => {
  const urlParams = [];
  const lastSeparatorIdx = src.lastIndexOf("/");
  const imageIdentifierAndField = src.slice(0, lastSeparatorIdx);

  urlParams.push(imageIdentifierAndField);
  urlParams.push(width + "w");
  urlParams.push(quality + "q");

  return urlParams.join("/");
};

const dotCmsLoader = (props) => {
  return `${process.env.NEXT_PUBLIC_DOTCMS_HOST}${getUrlWithResizingParameters(
    props,
  )}`;
};

const DotCmsImage = (params) => {
  return <Image {...params} loader={dotCmsLoader} />;
};

export default DotCmsImage;