Skip to content

Commit 6499f5a

Browse files
tanoabeleyraatscott
authored andcommitted
fix(common): invalid ImageKit transformation (#49201)
q-auto is an invalid/unsupported transformation and should not be used PR Close #49201
1 parent a1578de commit 6499f5a

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

packages/common/src/directives/ng_optimized_image/image_loaders/imagekit_loader.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {createImageLoader, ImageLoaderConfig, ImageLoaderInfo} from './image_loa
1313
*/
1414
export const imageKitLoaderInfo: ImageLoaderInfo = {
1515
name: 'ImageKit',
16-
testUrl: isImageKitUrl
16+
testUrl: isImageKitUrl,
1717
};
1818

1919
const IMAGE_KIT_LOADER_REGEX = /https?\:\/\/[^\/]+\.imagekit\.io\/.+/;
@@ -39,12 +39,18 @@ export const provideImageKitLoader = createImageLoader(
3939
createImagekitUrl,
4040
ngDevMode ? ['https://ik.imagekit.io/mysite', 'https://subdomain.mysite.com'] : undefined);
4141

42-
export function createImagekitUrl(path: string, config: ImageLoaderConfig) {
42+
export function createImagekitUrl(path: string, config: ImageLoaderConfig): string {
4343
// Example of an ImageKit image URL:
4444
// https://ik.imagekit.io/demo/tr:w-300,h-300/medium_cafe_B1iTdD0C.jpg
45-
let params = `tr:q-auto`; // applies the "auto quality" transformation
46-
if (config.width) {
47-
params += `,w-${config.width}`;
45+
const {src, width} = config;
46+
let urlSegments: string[];
47+
48+
if (width) {
49+
const params = `tr:w-${width}`;
50+
urlSegments = [path, params, src];
51+
} else {
52+
urlSegments = [path, src];
4853
}
49-
return `${path}/${params}/${config.src}`;
54+
55+
return urlSegments.join('/');
5056
}

packages/common/test/image_loaders/image_loader_spec.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('Built-in image directive loaders', () => {
8282
const loader = createCloudinaryLoader(path);
8383
expect(loader({src: 'img.png'})).toBe(`${path}/image/upload/f_auto,q_auto/img.png`);
8484
expect(loader({
85-
src: 'marketing/img-2.png'
85+
src: 'marketing/img-2.png',
8686
})).toBe(`${path}/image/upload/f_auto,q_auto/marketing/img-2.png`);
8787
});
8888

@@ -126,8 +126,16 @@ describe('Built-in image directive loaders', () => {
126126
it('should construct an image loader with the given path', () => {
127127
const path = 'https://ik.imageengine.io/imagetest';
128128
const loader = createImageKitLoader(path);
129-
expect(loader({src: 'img.png'})).toBe(`${path}/tr:q-auto/img.png`);
130-
expect(loader({src: 'marketing/img-2.png'})).toBe(`${path}/tr:q-auto/marketing/img-2.png`);
129+
expect(loader({src: 'img.png'})).toBe(`${path}/img.png`);
130+
expect(loader({src: 'marketing/img-2.png'})).toBe(`${path}/marketing/img-2.png`);
131+
});
132+
133+
it('should construct an image loader with the given path', () => {
134+
const path = 'https://ik.imageengine.io/imagetest';
135+
const loader = createImageKitLoader(path);
136+
expect(loader({src: 'img.png', width: 100})).toBe(`${path}/tr:w-100/img.png`);
137+
expect(loader({src: 'marketing/img-2.png', width: 200}))
138+
.toBe(`${path}/tr:w-200/marketing/img-2.png`);
131139
});
132140

133141
describe('input validation', () => {
@@ -148,13 +156,13 @@ describe('Built-in image directive loaders', () => {
148156
it('should handle a trailing forward slash on the path', () => {
149157
const path = 'https://ik.imageengine.io/imagetest';
150158
const loader = createImageKitLoader(`${path}/`);
151-
expect(loader({src: 'img.png'})).toBe(`${path}/tr:q-auto/img.png`);
159+
expect(loader({src: 'img.png'})).toBe(`${path}/img.png`);
152160
});
153161

154162
it('should handle a leading forward slash on the image src', () => {
155163
const path = 'https://ik.imageengine.io/imagetest';
156164
const loader = createImageKitLoader(path);
157-
expect(loader({src: '/img.png'})).toBe(`${path}/tr:q-auto/img.png`);
165+
expect(loader({src: '/img.png'})).toBe(`${path}/img.png`);
158166
});
159167
});
160168
});

0 commit comments

Comments
 (0)