Skip to content

Commit

Permalink
Image component: support local assets in development (#2573)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottdixon authored Oct 3, 2024
1 parent f041568 commit 229d4aa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-ads-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/hydrogen-react': patch
---

Image component: support local assets in development
21 changes: 21 additions & 0 deletions packages/hydrogen-react/src/Image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ describe('<Image />', () => {
crop,
});
});

it('handles remote assets', () => {
render(<Image {...defaultProps} />);
expect(screen.getByRole('img')).toHaveAttribute(
'src',
'https://cdn.shopify.com/s/files/1/0551/4566/0472/products/Main.jpg?width=100&crop=center',
);
});

it('handles local assets', () => {
const props = {
...defaultProps,
src: '/assets/image.png',
};

render(<Image {...props} />);
expect(screen.getByRole('img')).toHaveAttribute(
'src',
'/assets/image.png?width=100&crop=center',
);
});
});

describe('aspect-ratio', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/hydrogen-react/src/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -549,12 +549,13 @@ const FluidImage = React.forwardRef<HTMLImageElement, FluidImageProps>(
* })
* ```
*/
const PLACEHOLDER_DOMAIN = 'https://placeholder.shopify.com';
export function shopifyLoader({src, width, height, crop}: LoaderParams) {
if (!src) {
return '';
}

const url = new URL(src);
const url = new URL(src, PLACEHOLDER_DOMAIN);

if (width) {
url.searchParams.append('width', Math.round(width).toString());
Expand All @@ -567,7 +568,7 @@ export function shopifyLoader({src, width, height, crop}: LoaderParams) {
if (crop) {
url.searchParams.append('crop', crop);
}
return url.href;
return url.href.replace(PLACEHOLDER_DOMAIN, '');
}

/**
Expand Down

0 comments on commit 229d4aa

Please sign in to comment.