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 3 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
2 changes: 1 addition & 1 deletion packages/astro/src/assets/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function getImage(
src: imageURL,
srcSet: {
values: srcSets,
attribute: srcSets.map((srcSet) => `${srcSet.url} ${srcSet.descriptor}`).join(', '),
attribute: srcSets.map((srcSet) => `${encodeURI(srcSet.url)} ${srcSet.descriptor}`).join(', '),
},
attributes:
service.getHTMLAttributes !== undefined
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(' ');
});

describe('custom service in build', () => {
it('uses configured hashes properties', async () => {
await fixture.build();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 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,15 @@
---
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
- The image path should replace ' ' with '%20' in srcset
-->
<Image src={imageWithSpaces} width={imageWithSpaces.width / 2} widths={[imageWithSpaces.width]} alt="" />
</div>

Loading