Skip to content

Commit

Permalink
fix: handle srcset local image paths with spaces (#9537)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
4 people committed Dec 28, 2023
1 parent 3834c12 commit 16e61fc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-oranges-relate.md
@@ -0,0 +1,5 @@
---
"astro": patch
---

`<Image />` srcset now parses encoded paths correctly
6 changes: 4 additions & 2 deletions packages/astro/src/assets/vite-plugin-assets.ts
Expand Up @@ -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)));
}
};
},
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/core-image.test.js
Expand Up @@ -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);
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions packages/astro/test/fixtures/core-image-ssg/src/pages/srcset.astro
@@ -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>

0 comments on commit 16e61fc

Please sign in to comment.