Skip to content

Commit 5241386

Browse files
committed
feat: optimize image for cloudflare
1 parent 641636e commit 5241386

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/lib/optimizeImage.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
export default function optimizeImage(url: string, width?: number) {
2+
if (url.includes('imagedelivery.net')) {
3+
return optimizeImageForCloudflare(url, width);
4+
}
25
if (
36
!url.includes('https://images.velog.io') &&
47
!url.includes('https://media.vlpt.us')
@@ -16,3 +19,13 @@ export default function optimizeImage(url: string, width?: number) {
1619
}
1720
return replaced.concat(`?w=${width}`);
1821
}
22+
23+
export function optimizeImageForCloudflare(url: string, width?: number) {
24+
if (!width) return url;
25+
const replacer = (variant: string) => url.replace('public', variant);
26+
if (width === 640) return replacer('w640');
27+
if (width === 768) return replacer('w768');
28+
if (width === 240) return replacer('512x512');
29+
if (width === 120) return replacer('256x256');
30+
return url;
31+
}

0 commit comments

Comments
 (0)