1
1
import type { JSX } from "preact" ;
2
2
import type { ImageMetadata } from "astro" ;
3
+ import { siteUrl } from "../constants/site-config" ;
3
4
4
5
export interface GetPictureSizes {
5
6
[ size : number ] : {
@@ -42,7 +43,11 @@ function getSupportedWidth(width: number) {
42
43
43
44
const isDev = Boolean ( import . meta. env ?. DEV ) ;
44
45
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 ) {
46
51
if ( isDev ) {
47
52
// If the dev server is running, we can use the /_image endpoint
48
53
return `/_image?${ new URLSearchParams ( {
@@ -51,12 +56,9 @@ function getSource(src: string, width: number) {
51
56
q : "100" ,
52
57
} ) } `;
53
58
} 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 ( ) ) } ` ;
60
62
}
61
63
}
62
64
@@ -76,7 +78,7 @@ export function getPictureUrls(options: GetPictureOptions): GetPictureUrls {
76
78
77
79
for ( const width of widths ) {
78
80
const supportedWidth = getSupportedWidth ( width ) ;
79
- formatUrls [ supportedWidth ] = getSource ( src , supportedWidth ) ;
81
+ formatUrls [ supportedWidth ] = getSource ( src , supportedWidth , format ) ;
80
82
}
81
83
}
82
84
0 commit comments