aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Wooseop Kim <wooseop@protonmail.com> 2023-07-03 17:09:40 +0900
committerGravatar GitHub <noreply@github.com> 2023-07-03 16:09:40 +0800
commite644b34659836b463abb8c6a0acc867621c2d3cc (patch)
tree943a7766df5597c64ca7b9ffdee953ee0108c03c
parent3df71fd34fe2f50d81776e5ccdb7a340a4b2ca41 (diff)
downloadastro-e644b34659836b463abb8c6a0acc867621c2d3cc.tar.gz
astro-e644b34659836b463abb8c6a0acc867621c2d3cc.tar.zst
astro-e644b34659836b463abb8c6a0acc867621c2d3cc.zip
fix: make `Picture` generate valid dev URLs (#7500)
-rw-r--r--.changeset/few-ants-brake.md5
-rw-r--r--packages/integrations/image/src/lib/get-picture.ts2
-rw-r--r--packages/integrations/image/test/picture-ssg.test.js10
-rw-r--r--packages/integrations/image/test/picture-ssr-build.test.js10
-rw-r--r--packages/integrations/image/test/picture-ssr-dev.test.js10
5 files changed, 36 insertions, 1 deletions
diff --git a/.changeset/few-ants-brake.md b/.changeset/few-ants-brake.md
new file mode 100644
index 000000000..607c248ae
--- /dev/null
+++ b/.changeset/few-ants-brake.md
@@ -0,0 +1,5 @@
+---
+"@astrojs/image": patch
+---
+
+fix: make `Picture` generate valid dev URLs
diff --git a/packages/integrations/image/src/lib/get-picture.ts b/packages/integrations/image/src/lib/get-picture.ts
index 53b9aea8a..c0eaa4058 100644
--- a/packages/integrations/image/src/lib/get-picture.ts
+++ b/packages/integrations/image/src/lib/get-picture.ts
@@ -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`;
})
);
diff --git a/packages/integrations/image/test/picture-ssg.test.js b/packages/integrations/image/test/picture-ssg.test.js
index 61b4f8e2b..5f56ec53c 100644
--- a/packages/integrations/image/test/picture-ssg.test.js
+++ b/packages/integrations/image/test/picture-ssg.test.js
@@ -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);
diff --git a/packages/integrations/image/test/picture-ssr-build.test.js b/packages/integrations/image/test/picture-ssr-build.test.js
index 5e760d0b4..6c06fd1fc 100644
--- a/packages/integrations/image/test/picture-ssr-build.test.js
+++ b/packages/integrations/image/test/picture-ssr-build.test.js
@@ -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);
diff --git a/packages/integrations/image/test/picture-ssr-dev.test.js b/packages/integrations/image/test/picture-ssr-dev.test.js
index 325e0d08b..39a5431dc 100644
--- a/packages/integrations/image/test/picture-ssr-dev.test.js
+++ b/packages/integrations/image/test/picture-ssr-dev.test.js
@@ -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);