Skip to content

Commit

Permalink
Support relative protocol image URL (#5072)
Browse files Browse the repository at this point in the history
* Support relative protocol image URL

* Fix test

* Actually fix test
  • Loading branch information
bluwy authored Oct 13, 2022
1 parent 45728a0 commit 3918787
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/warm-mangos-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---

Support relative protocol image URL
4 changes: 4 additions & 0 deletions packages/integrations/image/src/build/ssg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ function webToCachePolicyResponse({ status, headers: _headers }: Response): Cach

async function loadRemoteImage(src: string) {
try {
if (src.startsWith('//')) {
src = `https:${src}`;
}

const req = new Request(src);
const res = await fetch(req);

Expand Down
8 changes: 5 additions & 3 deletions packages/integrations/image/src/lib/get-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,14 @@ export async function getImage(
? _loader.serializeTransform(resolved)
: globalThis.astroImage.defaultLoader.serializeTransform(resolved);

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

if (/^[\/\\]?@astroimage/.test(resolved.src)) {
src = `${resolved.src}?${searchParams.toString()}`;
if (/^[\/\\]?@astroimage/.test(imgSrc)) {
src = `${imgSrc}?${searchParams.toString()}`;
} else {
searchParams.set('href', resolved.src);
searchParams.set('href', imgSrc);
src = `/_image?${searchParams.toString()}`;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/integrations/image/src/loaders/squoosh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class SquooshService extends BaseSSRService {
if (autorotate) {
operations.push(autorotate);
}
} else if (transform.src.startsWith('//')) {
transform.src = `https:${transform.src}`;
}

if (transform.width || transform.height) {
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/image/src/utils/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TransformOptions } from '../loaders/index.js';
import { shorthash } from './shorthash.js';

export function isRemoteImage(src: string) {
return /^http(s?):\/\//.test(src);
return /^(https?:)?\/\//.test(src);
}

function removeQueryString(src: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ import { Image } from '@astrojs/image/components';
<Image id="social-jpg" src={socialJpg} width={506} height={253} alt="social-jpg" />
<br />
<Image id="google" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" width={544} height={184} format="webp" alt="Google" />
<br>
<Image id="google-alt" src="//www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" width={544} height={184} format="webp" alt="Google" />
</body>
</html>
13 changes: 12 additions & 1 deletion packages/integrations/image/test/squoosh-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Squoosh service', function () {
let $;

before(async () => {
fixture = await loadFixture({ root: './fixtures/basic-image/' });
fixture = await loadFixture({ root: './fixtures/squoosh-service/' });
devServer = await fixture.startDevServer();
const html = await fixture.fetch('/').then((res) => res.text());
$ = cheerio.load(html);
Expand Down Expand Up @@ -36,6 +36,17 @@ describe('Squoosh service', function () {
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
},
},
{
title: 'Remote images with relative protocol',
id: '#google-alt',
url: '/_image',
query: {
f: 'webp',
w: '544',
h: '184',
href: 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png',
},
},
{
title: 'Public images',
id: '#hero',
Expand Down

0 comments on commit 3918787

Please sign in to comment.