0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-03 22:57:08 -05:00

fix: handle srcset local image paths with spaces (#9537)

* fix: handle srcset local image paths with spaces

* replaced janky 'replaceAll' with encodeURI

* Update .changeset/weak-oranges-relate.md

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>

* fix: encodeURI the returned filepath directly

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Co-authored-by: Princesseuh <3019731+Princesseuh@users.noreply.github.com>
This commit is contained in:
Alex Waldron 2023-12-28 21:23:46 +00:00 committed by GitHub
parent 3834c124c9
commit 16e61fcacb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
`<Image />` srcset now parses encoded paths correctly

View file

@ -132,10 +132,12 @@ export default function assets({
}); });
} }
// The paths here are used for URLs, so we need to make sure they have the proper format for an URL
// (leading slash, prefixed with the base / assets prefix, encoded, etc)
if (settings.config.build.assetsPrefix) { if (settings.config.build.assetsPrefix) {
return joinPaths(settings.config.build.assetsPrefix, finalFilePath); return encodeURI(joinPaths(settings.config.build.assetsPrefix, finalFilePath));
} else { } else {
return prependForwardSlash(joinPaths(settings.config.base, finalFilePath)); return encodeURI(prependForwardSlash(joinPaths(settings.config.base, finalFilePath)));
} }
}; };
}, },

View file

@ -945,6 +945,17 @@ describe('astro:image', () => {
expect(data).to.be.an.instanceOf(Buffer); expect(data).to.be.an.instanceOf(Buffer);
}); });
it('client images srcset parsed correctly', async () => {
const html = await fixture.readFile('/srcset/index.html');
const $ = cheerio.load(html);
const srcset = $('#local-2-widths-with-spaces img').attr('srcset');
// Find image
const regex = /^(.+?) [0-9]+[wx]$/gm;
const imageSrcset = regex.exec(srcset)[1];
expect(imageSrcset).to.not.contain(' ');
});
it('supports images with encoded characters in url', async () => { it('supports images with encoded characters in url', async () => {
const html = await fixture.readFile('/index.html'); const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html); const $ = cheerio.load(html);

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.5 KiB

View file

@ -0,0 +1,14 @@
---
import { Image } from "astro:assets";
import imageWithSpaces from "../assets/image 1.jpg";
---
<div id="local-2-widths-with-spaces">
<!--
In this example, only two images should be generated :
- The base image
- The 2x image
Additionally, the image URL should be encoded to avoid issues with spaces and other special characters.
-->
<Image src={imageWithSpaces} width={imageWithSpaces.width / 2} widths={[imageWithSpaces.width]} alt="" />
</div>