@@ -14,30 +14,25 @@ function uploadcareLoader({ src, width, quality }) {
14
14
const proxyEndpoint = customProxyEndpoint || ( 0 , helpers_1 . generateDefaultProxyEndpoint ) ( publicKey ) ;
15
15
const basePath = ( 0 , helpers_1 . trimTrailingSlash ) ( process . env . NEXT_PUBLIC_UPLOADCARE_APP_BASE_URL || '' ) ;
16
16
const proxy = ( 0 , helpers_1 . trimTrailingSlash ) ( proxyEndpoint ) ;
17
- const isOnCdn = ( 0 , helpers_1 . isCdnUrl ) ( src , cdnDomain ) ;
18
- if ( ! ( 0 , helpers_1 . isProduction ) ( ) && ! isOnCdn ) {
17
+ const isProductionMode = ( 0 , helpers_1 . isProduction ) ( ) ;
18
+ const isImageOnCdn = ( 0 , helpers_1 . isCdnUrl ) ( src , cdnDomain ) ;
19
+ const isImageRelative = src . startsWith ( '/' ) ;
20
+ // Development mode; not on CDN.
21
+ if ( ! isProductionMode && ! isImageOnCdn ) {
19
22
const isPublicKeySet = ! ( 0 , helpers_1 . isDotenvParamEmpty ) ( publicKey ) ;
20
23
const isCustomProxyEndpointSet = ! ( 0 , helpers_1 . isDotenvParamEmpty ) ( customProxyEndpoint ) ;
21
24
if ( ! isPublicKeySet && ! isCustomProxyEndpointSet ) {
22
25
throw new Error ( `Both NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY and NEXT_PUBLIC_UPLOADCARE_CUSTOM_PROXY_DOMAIN are not set. Please set either one.` ) ;
23
26
}
24
- if ( src . startsWith ( '/' ) ) {
27
+ if ( isImageRelative ) {
25
28
return src ;
26
29
}
27
30
}
28
- // Process local images in Production.
29
- if ( ( 0 , helpers_1 . isProduction ) ( ) && ! isOnCdn && src . startsWith ( '/' ) ) {
30
- const isBasePathSet = ! ( 0 , helpers_1 . isDotenvParamEmpty ) ( basePath ) ;
31
- if ( ! isBasePathSet ) {
32
- return src ;
33
- }
34
- return `${ basePath } ${ src } ` ;
35
- }
36
31
const filename = ( 0 , helpers_1 . getFilename ) ( src ) ;
37
32
const extension = ( 0 , helpers_1 . getExtension ) ( filename ) ;
38
33
// Some extensions are not processed by Uploadcare, e.g. SVG.
39
34
if ( constants_1 . NOT_PROCESSED_EXTENSIONS . includes ( extension ) ) {
40
- return isOnCdn ? src : `${ basePath } ${ src } ` ;
35
+ return isImageOnCdn ? src : `${ basePath } ${ src } ` ;
41
36
}
42
37
// Demo: https://ucarecdn.com/a6f8abc8-f92e-460a-b7a1-c5cd70a18cdb/-/format/auto/-/resize/300x/vercel.png
43
38
const userParams = ( 0 , helpers_1 . parseUserParamsString ) ( userParamsString ) ;
@@ -52,10 +47,19 @@ function uploadcareLoader({ src, width, quality }) {
52
47
] ) ;
53
48
const params = ( 0 , helpers_1 . mergeParams ) ( basicParams , userParams ) ;
54
49
const apiParamsString = '/-/' + params . join ( '/-/' ) + '/' ;
55
- if ( isOnCdn ) {
50
+ if ( isImageOnCdn ) {
56
51
const withoutFilename = src . slice ( 0 , src . lastIndexOf ( '/' ) ) ;
57
52
return `${ withoutFilename } ${ apiParamsString } ${ filename } ` ;
58
53
}
54
+ // Production mode; local image.
55
+ if ( isProductionMode && isImageRelative ) {
56
+ const isBasePathSet = ! ( 0 , helpers_1 . isDotenvParamEmpty ) ( basePath ) ;
57
+ // Return the relative url AS IS if the base path is not set.
58
+ if ( ! isBasePathSet ) {
59
+ return src ;
60
+ }
61
+ return `${ proxy } ${ apiParamsString } ${ basePath } ${ src } ` ;
62
+ }
59
63
return `${ proxy } ${ apiParamsString } ${ src } ` ;
60
64
}
61
65
exports . uploadcareLoader = uploadcareLoader ;
0 commit comments