forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-loader.ts
More file actions
33 lines (26 loc) · 752 Bytes
/
image-loader.ts
File metadata and controls
33 lines (26 loc) · 752 Bytes
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
33
import type { ImageLoaderProps } from 'next/image';
const normalizeSrc = (src: string) => {
return src.startsWith('/') ? src.slice(1) : src;
};
export default function cloudflareLoader({
src,
width,
quality,
}: ImageLoaderProps) {
if (process.env.NODE_ENV === 'development') {
// Serve the original image when using `next dev`
return src;
}
// Sets the requested width by next/image
const params = new Map<string, string>();
if (width > 0) {
params.set('width', `${width}`);
}
if (quality && quality > 0) {
params.set('quality', `${quality}`);
}
const pathParams = [...params]
.map(([key, value]) => `${key}=${value}`)
.join(',');
return `/cdn-cgi/image/${pathParams}/${normalizeSrc(src)}`;
}