Skip to content

Commit f165d3a

Browse files
committed
fix: Fixed processing of the local image in the production mode.
1 parent 1fa3dcc commit f165d3a

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

src/__tests__/uploadcare-loader.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ describe('uploadcareLoader', () => {
5050
quality: 80
5151
});
5252

53-
expect(result).toEqual(`${basePath}${src}`);
53+
expect(result).toEqual(
54+
`https://test-public-key.ucr.io/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/0x/-/quality/normal/${basePath}${src}`
55+
);
5456

5557
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_APP_BASE_URL');
5658
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY');

src/utils/loader.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ export function uploadcareLoader({
4343
);
4444

4545
const proxy = trimTrailingSlash(proxyEndpoint);
46+
const isProductionMode = isProduction();
47+
const isImageOnCdn = isCdnUrl(src, cdnDomain);
48+
const isImageRelative = src.startsWith('/');
4649

47-
const isOnCdn = isCdnUrl(src, cdnDomain);
48-
49-
if (!isProduction() && !isOnCdn) {
50+
// Development mode; not on CDN.
51+
if (!isProductionMode && !isImageOnCdn) {
5052
const isPublicKeySet = !isDotenvParamEmpty(publicKey);
5153
const isCustomProxyEndpointSet = !isDotenvParamEmpty(customProxyEndpoint);
5254

@@ -56,34 +58,22 @@ export function uploadcareLoader({
5658
);
5759
}
5860

59-
if (src.startsWith('/')) {
61+
if (isImageRelative) {
6062
return src;
6163
}
6264
}
6365

64-
// Process local images in Production.
65-
if (isProduction() && !isOnCdn && src.startsWith('/')) {
66-
const isBasePathSet = !isDotenvParamEmpty(basePath);
67-
68-
if (!isBasePathSet) {
69-
return src;
70-
}
71-
72-
return `${basePath}${src}`;
73-
}
74-
7566
const filename = getFilename(src);
7667
const extension = getExtension(filename);
7768

7869
// Some extensions are not processed by Uploadcare, e.g. SVG.
7970
if (NOT_PROCESSED_EXTENSIONS.includes(extension)) {
80-
return isOnCdn ? src : `${basePath}${src}`;
71+
return isImageOnCdn ? src : `${basePath}${src}`;
8172
}
8273

8374
// Demo: https://ucarecdn.com/a6f8abc8-f92e-460a-b7a1-c5cd70a18cdb/-/format/auto/-/resize/300x/vercel.png
8475

8576
const userParams = parseUserParamsString(userParamsString);
86-
8777
const requestedFormat = getRequestedFormatFromParams(userParams);
8878
const qualityString = convertToUploadcareQualityString(quality);
8979

@@ -101,10 +91,22 @@ export function uploadcareLoader({
10191

10292
const apiParamsString = '/-/' + params.join('/-/') + '/';
10393

104-
if (isOnCdn) {
94+
if (isImageOnCdn) {
10595
const withoutFilename = src.slice(0, src.lastIndexOf('/'));
10696
return `${withoutFilename}${apiParamsString}${filename}`;
10797
}
10898

99+
// Production mode; local image.
100+
if (isProductionMode && isImageRelative) {
101+
const isBasePathSet = !isDotenvParamEmpty(basePath);
102+
103+
// Return the relative url AS IS if the base path is not set.
104+
if (!isBasePathSet) {
105+
return src;
106+
}
107+
108+
return `${proxy}${apiParamsString}${basePath}${src}`;
109+
}
110+
109111
return `${proxy}${apiParamsString}${src}`;
110112
}

0 commit comments

Comments
 (0)