Skip to content

Commit

Permalink
Support remote images with encoded characters (withastro#9540)
Browse files Browse the repository at this point in the history
* Support remote images with encoded characters

* Add a changeset

* Update .changeset/small-snakes-build.md

Co-authored-by: Florian Lefebvre <[email protected]>

---------

Co-authored-by: Florian Lefebvre <[email protected]>
  • Loading branch information
matthewp and florian-lefebvre authored Dec 28, 2023
1 parent b34bd2b commit 7f212f0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-snakes-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes remote images with encoded characters
2 changes: 1 addition & 1 deletion packages/astro/src/assets/utils/transformToPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export function propsToFilename(transform: ImageTransform, hash: string) {
isESMImportedImage(transform.src) ? transform.src.src : transform.src
);
const ext = extname(filename);
filename = basename(filename, ext);
filename = decodeURIComponent(basename(filename, ext));

let outputExt = transform.format ? `.${transform.format}` : ext;
return `/${filename}_${hash}${outputExt}`;
Expand Down
11 changes: 10 additions & 1 deletion packages/astro/test/core-image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ describe('astro:image', () => {
root: './fixtures/core-image-ssg/',
image: {
service: testImageService(),
domains: ['astro.build'],
domains: ['astro.build', 'avatars.githubusercontent.com'],
},
});
// Remove cache directory
Expand Down Expand Up @@ -945,6 +945,15 @@ describe('astro:image', () => {
expect(data).to.be.an.instanceOf(Buffer);
});

it('supports images with encoded characters in url', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);
const img = $('#encoded-chars img');
const src = img.attr('src')
const data = await fixture.readFile(src);
expect(data).to.not.be.undefined;
});

describe('custom service in build', () => {
it('uses configured hashes properties', async () => {
await fixture.build();
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ import myImage from "../assets/penguin1.jpg";
<div id="remote">
<Image src="https://avatars.githubusercontent.com/u/622227?s=64" alt="fred" width="48" height="48" />
</div>

<div id="encoded-chars">
<Image src="https://avatars.githubusercontent.com/u%2f622227?s=64" alt="fred2" width="48" height="48" />
</div>
</body>
</html>

0 comments on commit 7f212f0

Please sign in to comment.