Skip to content

Commit 7f18177

Browse files
authored
fix: accept src without filename (#14)
* fix: accept src without filename * chore(readme): add few words about filenames and output dimension limits
1 parent 11a958b commit 7f18177

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ where `anotherLoader` will be used instead of the Uploadcare loader for this par
169169

170170
## Notes
171171

172-
If you pass a local image url, the loader returns it AS IS if the app is run in the development mode or if the `NEXT_PUBLIC_UPLOADCARE_APP_BASE_URL` is not set.
172+
- If you have jpegs larger than 3000px and you want loader to resize them up to 5000px, you need to pass filename with `jpeg` or `jpg` extension to the src url. See [Output image dimensions docs](https://uploadcare.com/docs/transformations/image/#dimensions) for more details. When no filename provided, we'll treat the image as non-jpeg and can resize it up to 3000px only.
173+
174+
- If you pass a local image url, the loader returns it AS IS if the app is run in the development mode or if the `NEXT_PUBLIC_UPLOADCARE_APP_BASE_URL` is not set.
173175

174176
## Known Issues
175177

src/__tests__/uploadcare-image.spec.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import { addEnvVar, removeEnvVar } from './utils';
88

99
describe('UploadcareImage', () => {
1010
beforeEach(() => {
11+
addEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY', 'test-public-key');
12+
1113
cleanup();
1214
});
1315

14-
test('The UploadcareImage component renders passed image with default settings properly', () => {
15-
addEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY', 'test-public-key');
16+
afterEach(() => {
17+
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY');
18+
});
1619

20+
test('The UploadcareImage component renders passed image with default settings properly', () => {
1721
const src =
1822
'https://ucarecdn.com/a6f8abc8-f92e-460a-b7a1-c5cd70a18cdb/vercel.png';
1923

@@ -32,7 +36,24 @@ describe('UploadcareImage', () => {
3236
expect(screen.getByRole('img').getAttribute('src')).toEqual(
3337
'https://ucarecdn.com/a6f8abc8-f92e-460a-b7a1-c5cd70a18cdb/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/1080x/-/quality/normal/vercel.png'
3438
);
39+
});
3540

36-
removeEnvVar('NEXT_PUBLIC_UPLOADCARE_PUBLIC_KEY');
41+
test('The UploadcareImage component should accept src without filename', () => {
42+
const src = 'https://ucarecdn.com/a6f8abc8-f92e-460a-b7a1-c5cd70a18cdb/';
43+
44+
render(
45+
<UploadcareImage
46+
src={src}
47+
width={500}
48+
height={500}
49+
quality={80}
50+
// Necessary because lazy-loading causes a placeholder image render first.
51+
loading="eager"
52+
/>
53+
);
54+
55+
expect(screen.getByRole('img').getAttribute('src')).toEqual(
56+
'https://ucarecdn.com/a6f8abc8-f92e-460a-b7a1-c5cd70a18cdb/-/format/auto/-/stretch/off/-/progressive/yes/-/resize/1080x/-/quality/normal/'
57+
);
3758
});
3859
});

src/utils/loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function uploadcareLoader({
8080

8181
const isJpeg =
8282
requestedFormat === 'jpeg' ||
83-
(requestedFormat === 'auto' && isJpegExtension(extension));
83+
(requestedFormat === 'auto' && !!extension && isJpegExtension(extension));
8484
const maxResizeWidth = getMaxResizeWidth(width, isJpeg);
8585
const forceJpeg = isJpeg && maxResizeWidth > MAX_OUTPUT_IMAGE_DIMENSION;
8686

0 commit comments

Comments
 (0)