Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle srcset local image paths with spaces #9537

Merged
merged 5 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-oranges-relate.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading
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
Original file line number Diff line number Diff line change
@@ -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