Skip to content

Commit

Permalink
[frontend] Pass down image optimization requests to imageprovider in …
Browse files Browse the repository at this point in the history
…checkoutitem component (open-telemetry#1529)

* [frontend] Pass down image optimization requests to imageprovider in checkoutitem component

* updating changelog

* Update CHANGELOG.md

---------

Co-authored-by: Austin Parker <austin@ap2.io>
Co-authored-by: Juliano Costa <julianocosta89@outlook.com>
  • Loading branch information
3 people authored and AlexPSplunk committed Jul 10, 2024
1 parent 3fb0518 commit d6f4b6c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ the release.
([#1519](https://github.com/open-telemetry/opentelemetry-demo/pull/1519))
* [flagd] export flagd traces to otel collector
([#1522](https://github.com/open-telemetry/opentelemetry-demo/pull/1522))
* [frontend] Pass down image optimization requests to imageprovider
([#1522](https://github.com/open-telemetry/opentelemetry-demo/pull/1522))

## 1.9.0

Expand Down
26 changes: 25 additions & 1 deletion src/frontend/components/CheckoutItem/CheckoutItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ interface IProps {
address: Address;
}

interface ImageLoaderProps {
src: string;
width: number;
quality?: number;
}
/**
* We connect to imageprovider through the envoy proxy, straight from the browser, for this we need to know the current hostname and port.
* During building and serverside rendering, these are undefined so we use some conditionals and default values.
*/
let hostname = "";
let port = 80;
let protocol = "http";

if (typeof window !== "undefined" && window.location) {
hostname = window.location.hostname;
port = window.location.port ? parseInt(window.location.port, 10) : (window.location.protocol === "https:" ? 443 : 80);
protocol = window.location.protocol.slice(0, -1); // Remove trailing ':'
}
const imageLoader = ({ src, width, quality }: ImageLoaderProps): string => {
// We pass down the optimization request to the iamgeprovider service here, without this, nextJs would trz to use internal optimizer which is not working with the external imageprovider.
return `${protocol}://${hostname}:${port}/${src}?w=${width}&q=${quality || 75}`;
}


const CheckoutItem = ({
checkoutItem: {
item: {
Expand All @@ -29,7 +53,7 @@ const CheckoutItem = ({
return (
<S.CheckoutItem data-cy={CypressFields.CheckoutItem}>
<S.ItemDetails>
<S.ItemImage src={"/images/products/" + picture} alt={name} />
<S.ItemImage src={"/images/products/" + picture} alt={name} loader={imageLoader}/>
<S.Details>
<S.ItemName>{name}</S.ItemName>
<p>Quantity: {quantity}</p>
Expand Down

0 comments on commit d6f4b6c

Please sign in to comment.