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:
parent
3834c124c9
commit
16e61fcacb
5 changed files with 34 additions and 2 deletions
5
.changeset/weak-oranges-relate.md
Normal file
5
.changeset/weak-oranges-relate.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
"astro": patch
|
||||
---
|
||||
|
||||
`<Image />` srcset now parses encoded paths correctly
|
|
@ -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) {
|
||||
return joinPaths(settings.config.build.assetsPrefix, finalFilePath);
|
||||
return encodeURI(joinPaths(settings.config.build.assetsPrefix, finalFilePath));
|
||||
} else {
|
||||
return prependForwardSlash(joinPaths(settings.config.base, finalFilePath));
|
||||
return encodeURI(prependForwardSlash(joinPaths(settings.config.base, finalFilePath)));
|
||||
}
|
||||
};
|
||||
},
|
||||
|
|
|
@ -945,6 +945,17 @@ describe('astro:image', () => {
|
|||
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 () => {
|
||||
const html = await fixture.readFile('/index.html');
|
||||
const $ = cheerio.load(html);
|
||||
|
|
BIN
packages/astro/test/fixtures/core-image-ssg/src/assets/image 1.jpg
vendored
Normal file
BIN
packages/astro/test/fixtures/core-image-ssg/src/assets/image 1.jpg
vendored
Normal file
Binary file not shown.
After Width: | Height: | Size: 8.5 KiB |
14
packages/astro/test/fixtures/core-image-ssg/src/pages/srcset.astro
vendored
Normal file
14
packages/astro/test/fixtures/core-image-ssg/src/pages/srcset.astro
vendored
Normal 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>
|
Loading…
Add table
Reference in a new issue