Skip to content

Commit 3918787

Browse files
authored
Support relative protocol image URL (#5072)
* Support relative protocol image URL * Fix test * Actually fix test
1 parent 45728a0 commit 3918787

7 files changed

Lines changed: 31 additions & 5 deletions

File tree

.changeset/warm-mangos-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/image': patch
3+
---
4+
5+
Support relative protocol image URL

packages/integrations/image/src/build/ssg.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ function webToCachePolicyResponse({ status, headers: _headers }: Response): Cach
5454

5555
async function loadRemoteImage(src: string) {
5656
try {
57+
if (src.startsWith('//')) {
58+
src = `https:${src}`;
59+
}
60+
5761
const req = new Request(src);
5862
const res = await fetch(req);
5963

packages/integrations/image/src/lib/get-image.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@ export async function getImage(
140140
? _loader.serializeTransform(resolved)
141141
: globalThis.astroImage.defaultLoader.serializeTransform(resolved);
142142

143+
const imgSrc =
144+
!isLocalImage && resolved.src.startsWith('//') ? `https:${resolved.src}` : resolved.src;
143145
let src: string;
144146

145-
if (/^[\/\\]?@astroimage/.test(resolved.src)) {
146-
src = `${resolved.src}?${searchParams.toString()}`;
147+
if (/^[\/\\]?@astroimage/.test(imgSrc)) {
148+
src = `${imgSrc}?${searchParams.toString()}`;
147149
} else {
148-
searchParams.set('href', resolved.src);
150+
searchParams.set('href', imgSrc);
149151
src = `/_image?${searchParams.toString()}`;
150152
}
151153

packages/integrations/image/src/loaders/squoosh.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ class SquooshService extends BaseSSRService {
8989
if (autorotate) {
9090
operations.push(autorotate);
9191
}
92+
} else if (transform.src.startsWith('//')) {
93+
transform.src = `https:${transform.src}`;
9294
}
9395

9496
if (transform.width || transform.height) {

packages/integrations/image/src/utils/paths.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { TransformOptions } from '../loaders/index.js';
22
import { shorthash } from './shorthash.js';
33

44
export function isRemoteImage(src: string) {
5-
return /^http(s?):\/\//.test(src);
5+
return /^(https?:)?\/\//.test(src);
66
}
77

88
function removeQueryString(src: string) {

packages/integrations/image/test/fixtures/squoosh-service/src/pages/index.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ import { Image } from '@astrojs/image/components';
1414
<Image id="social-jpg" src={socialJpg} width={506} height={253} alt="social-jpg" />
1515
<br />
1616
<Image id="google" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" width={544} height={184} format="webp" alt="Google" />
17+
<br>
18+
<Image id="google-alt" src="//www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" width={544} height={184} format="webp" alt="Google" />
1719
</body>
1820
</html>

packages/integrations/image/test/squoosh-service.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('Squoosh service', function () {
88
let $;
99

1010
before(async () => {
11-
fixture = await loadFixture({ root: './fixtures/basic-image/' });
11+
fixture = await loadFixture({ root: './fixtures/squoosh-service/' });
1212
devServer = await fixture.startDevServer();
1313
const html = await fixture.fetch('/').then((res) => res.text());
1414
$ = cheerio.load(html);
@@ -36,6 +36,17 @@ describe('Squoosh service', function () {
3636
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
3737
},
3838
},
39+
{
40+
title: 'Remote images with relative protocol',
41+
id: '#google-alt',
42+
url: '/_image',
43+
query: {
44+
f: 'webp',
45+
w: '544',
46+
h: '184',
47+
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
48+
},
49+
},
3950
{
4051
title: 'Public images',
4152
id: '#hero',

0 commit comments

Comments
 (0)