Skip to content

Commit bb08ac9

Browse files
maamokunharshil1712
authored andcommitted
better image loader code (#17658)
1 parent adce8bd commit bb08ac9

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/content/docs/images/transform-images/integrate-with-frameworks.mdx

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,37 @@ To use Images with **all** your app's images, define a global [loaderFile](https
2222

2323
Add the following settings to the **next.config.js** file located at the root our your Next.js application.
2424

25-
```js
25+
```ts
2626
module.exports = {
2727
images: {
2828
loader: 'custom',
29-
loaderFile: './imageLoader.js',
29+
loaderFile: './imageLoader.ts',
3030
},
3131
}
3232
```
3333

34-
Next, create the `imageLoader.js` file in the specified path (relative to the root of your Next.js application).
34+
Next, create the `imageLoader.ts` file in the specified path (relative to the root of your Next.js application).
3535

36-
```js
37-
const normalizeSrc = src => {
38-
return src.startsWith('/') ? src.slice(1) : src;
36+
```ts
37+
const normalizeSrc = (src: string) => {
38+
return src.startsWith("/") ? src.slice(1) : src;
3939
};
4040

41-
export default function cloudflareLoader ({ src, width, quality }) {
42-
const params = [`width=${width}`];
43-
if (quality) {
44-
params.push(`quality=${quality}`);
45-
}
46-
const paramsString = params.join(',');
47-
return `/cdn-cgi/image/${paramsString}/${normalizeSrc(src)}`;
48-
};
41+
export default function cloudflareLoader({
42+
src,
43+
width,
44+
quality,
45+
}: { src: string; width: number; quality?: number }) {
46+
if (process.env.NODE_ENV === "development") {
47+
return src;
48+
}
49+
const params = [`width=${width}`];
50+
if (quality) {
51+
params.push(`quality=${quality}`);
52+
}
53+
const paramsString = params.join(",");
54+
return `/cdn-cgi/image/${paramsString}/${normalizeSrc(src)}`;
55+
}
4956
```
5057

5158
### Custom Loaders
@@ -60,6 +67,9 @@ const normalizeSrc = src => {
6067
};
6168

6269
const cloudflareLoader = ({ src, width, quality }) => {
70+
if (process.env.NODE_ENV === "development") {
71+
return src;
72+
}
6373
const params = [`width=${width}`];
6474
if (quality) {
6575
params.push(`quality=${quality}`);

0 commit comments

Comments
 (0)