Skip to content

Commit c711910

Browse files
authored
fix: include width param in Next.js Cloudflare Image loader examples to resolve warnings (cloudflare#26391)
* fix(nextjs): include width param in Cloudflare image loader examples to resolve Next.js warnings * fix(nextjs): use '&' for dev query params * refactor: use ImageLoaderProps
1 parent 1a0cd19 commit c711910

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module.exports = {
3434
Next, create the `imageLoader.ts` file in the specified path (relative to the root of your Next.js application).
3535

3636
```ts
37+
import type { ImageLoaderProps } from "next/image";
38+
3739
const normalizeSrc = (src: string) => {
3840
return src.startsWith("/") ? src.slice(1) : src;
3941
};
@@ -42,16 +44,15 @@ export default function cloudflareLoader({
4244
src,
4345
width,
4446
quality,
45-
}: { src: string; width: number; quality?: number }) {
46-
if (process.env.NODE_ENV === "development") {
47-
return src;
48-
}
47+
}: ImageLoaderProps) {
4948
const params = [`width=${width}`];
5049
if (quality) {
51-
params.push(`quality=${quality}`);
50+
params.push(`quality=${quality}`);
51+
}
52+
if (process.env.NODE_ENV === "development") {
53+
return `${src}?${params.join("&")}`;
5254
}
53-
const paramsString = params.join(",");
54-
return `/cdn-cgi/image/${paramsString}/${normalizeSrc(src)}`;
55+
return `/cdn-cgi/image/${params.join(",")}/${normalizeSrc(src)}`;
5556
}
5657
```
5758

@@ -62,30 +63,30 @@ Alternatively, define a loader for each `<Image />` component.
6263
```js
6364
import Image from 'next/image';
6465

65-
const normalizeSrc = src => {
66+
const normalizeSrc = (src) => {
6667
return src.startsWith('/') ? src.slice(1) : src;
6768
};
6869

6970
const cloudflareLoader = ({ src, width, quality }) => {
70-
if (process.env.NODE_ENV === "development") {
71-
return src;
72-
}
7371
const params = [`width=${width}`];
7472
if (quality) {
7573
params.push(`quality=${quality}`);
7674
}
77-
const paramsString = params.join(',');
78-
return `/cdn-cgi/image/${paramsString}/${normalizeSrc(src)}`;
75+
if (process.env.NODE_ENV === "development") {
76+
return `${src}?${params.join("&")}`;
77+
}
78+
return `/cdn-cgi/image/${params.join(",")}/${normalizeSrc(src)}`;
7979
};
8080

81-
const MyImage = props => {
81+
const MyImage = (props) => {
8282
return (
8383
<Image
8484
loader={cloudflareLoader}
8585
src="/me.png"
8686
alt="Picture of the author"
8787
width={500}
8888
height={500}
89+
{...props}
8990
/>
9091
);
9192
};

0 commit comments

Comments
 (0)