Skip to content

Commit

Permalink
fix: make Picture generate valid dev URLs (#7500)
Browse files Browse the repository at this point in the history
  • Loading branch information
wooseopkim authored Jul 3, 2023
1 parent 3df71fd commit e644b34
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/few-ants-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/image": patch
---

fix: make `Picture` generate valid dev URLs
2 changes: 1 addition & 1 deletion packages/integrations/image/src/lib/get-picture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function getPicture(params: GetPictureParams): Promise<GetPictureRe
image = img;
}

return `${encodeURI(img.src ?? '')} ${width}w`;
return `${img.src?.replaceAll(' ', encodeURI)} ${width}w`;
})
);

Expand Down
10 changes: 10 additions & 0 deletions packages/integrations/image/test/picture-ssg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,16 @@ describe('SSG pictures with subpath - dev', function () {
const src = image.attr('src');
const [route, params] = src.split('?');

for (const srcset of picture.children('source').map((_, source) => source.attribs['srcset'])) {
for (const pictureSrc of srcset.split(',')) {
const pictureParams = pictureSrc.split('?')[1];

const expected = new URLSearchParams(params).get('href');
const actual = new URLSearchParams(pictureParams).get('href').replace(/\s+\d+w$/, '');
expect(expected).to.equal(actual);
}
}

expect(route).to.equal(url);

const searchParams = new URLSearchParams(params);
Expand Down
10 changes: 10 additions & 0 deletions packages/integrations/image/test/picture-ssr-build.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ describe('SSR pictures with subpath - build', function () {
const src = image.attr('src');
const [route, params] = src.split('?');

for (const srcset of picture.children('source').map((_, source) => source.attribs['srcset'])) {
for (const pictureSrc of srcset.split(',')) {
const pictureParams = pictureSrc.split('?')[1];

const expected = new URLSearchParams(params).get('href');
const actual = new URLSearchParams(pictureParams).get('href').replace(/\s+\d+w$/, '');
expect(expected).to.equal(actual);
}
}

expect(route).to.equal(url);

const searchParams = new URLSearchParams(params);
Expand Down
10 changes: 10 additions & 0 deletions packages/integrations/image/test/picture-ssr-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ describe('SSR pictures - dev', function () {
const src = image.attr('src');
const [route, params] = src.split('?');

for (const srcset of picture.children('source').map((_, source) => source.attribs['srcset'])) {
for (const pictureSrc of srcset.split(',')) {
const pictureParams = pictureSrc.split('?')[1];

const expected = new URLSearchParams(params).get('href');
const actual = new URLSearchParams(pictureParams).get('href').replace(/\s+\d+w$/, '');
expect(expected).to.equal(actual);
}
}

expect(route).to.equal(url);

const searchParams = new URLSearchParams(params);
Expand Down

0 comments on commit e644b34

Please sign in to comment.