Skip to content

Commit 75a3664

Browse files
committed
production mode should now use cloudinary for image delivery
1 parent 025fef5 commit 75a3664

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/utils/get-picture.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { JSX } from "preact";
22
import type { ImageMetadata } from "astro";
3+
import { siteUrl } from "../constants/site-config";
34

45
export interface GetPictureSizes {
56
[size: number]: {
@@ -42,7 +43,11 @@ function getSupportedWidth(width: number) {
4243

4344
const isDev = Boolean(import.meta.env?.DEV);
4445

45-
function getSource(src: string, width: number) {
46+
const cloudName = process.env.PUBLIC_CLOUDINARY_CLOUD_NAME;
47+
if (!isDev && !cloudName)
48+
throw Error("missing public variable CLOUDINARY_CLOUD_NAME");
49+
50+
function getSource(src: string, width: number, getFormat: string) {
4651
if (isDev) {
4752
// If the dev server is running, we can use the /_image endpoint
4853
return `/_image?${new URLSearchParams({
@@ -51,12 +56,9 @@ function getSource(src: string, width: number) {
5156
q: "100",
5257
})}`;
5358
} else {
54-
// Otherwise, use Vercel's Image Optimization API
55-
return `/_vercel/image?${new URLSearchParams({
56-
url: src,
57-
w: String(width),
58-
q: "100",
59-
})}`;
59+
// If in production use cloudinary's fetch
60+
const domainUrl = new URL(src, siteUrl);
61+
return `https://res.cloudinary.com/${cloudName}/image/fetch/w_${width},f_${getFormat},q_auto/${encodeURIComponent(domainUrl.toString())}`;
6062
}
6163
}
6264

@@ -76,7 +78,7 @@ export function getPictureUrls(options: GetPictureOptions): GetPictureUrls {
7678

7779
for (const width of widths) {
7880
const supportedWidth = getSupportedWidth(width);
79-
formatUrls[supportedWidth] = getSource(src, supportedWidth);
81+
formatUrls[supportedWidth] = getSource(src, supportedWidth, format);
8082
}
8183
}
8284

0 commit comments

Comments
 (0)