Skip to content

Commit

Permalink
fix: encode spaces when generating srcset (#7340)
Browse files Browse the repository at this point in the history
  • Loading branch information
wooseopkim committed Jun 22, 2023
1 parent 33cdc86 commit 9739adc
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/late-steaks-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---

Fix problem where filenames with spaces produce invalid srcset attributes
1 change: 1 addition & 0 deletions packages/integrations/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"mocha": "^9.2.2",
"rollup-plugin-copy": "^3.4.0",
"sharp": "^0.32.1",
"srcset-parse": "^1.1.0",
"vite": "^4.3.9"
},
"peerDependencies": {
Expand Down
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 `${img.src} ${width}w`;
return `${encodeURI(img.src ?? '')} ${width}w`;
})
);

Expand Down
12 changes: 12 additions & 0 deletions packages/integrations/image/test/picture-ssg.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import path from 'path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { join } from 'node:path';
import { loadFixture } from './test-utils.js';
import srcsetParse from 'srcset-parse';

const matchSrcset = srcsetParse.default;

const __dirname = fileURLToPath(new URL('.', import.meta.url));
const toAstroImage = (relpath) =>
Expand Down Expand Up @@ -96,6 +99,11 @@ describe('SSG pictures - dev', function () {
const sources = picture.children('source');
expect(sources.length).to.equal(3);

sources.each((_, el) => {
const srcset = $(el).attr('srcset');
expect(matchSrcset(srcset).length).to.equal(2);
});

const src = image.attr('src');
const [route, params] = src.split('?');

Expand Down Expand Up @@ -293,6 +301,8 @@ describe('SSG pictures - build', function () {
const source = $(el);
const srcset = source.attr('srcset');

expect(matchSrcset(srcset).length).to.equal(2);

for (const src of srcset.split(',')) {
const segments = src.split(' ');

Expand Down Expand Up @@ -400,6 +410,8 @@ describe('SSG pictures with subpath - build', function () {
const source = $(el);
const srcset = source.attr('srcset');

expect(matchSrcset(srcset).length).to.equal(2);

for (const src of srcset.split(',')) {
const [pathname, width] = src.split(' ');
const widthNum = parseInt(width.substring(0, width.length - 1));
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9739adc

Please sign in to comment.