Skip to content

Commit 4d84a64

Browse files
klucsikaustinlparkerjulianocosta89
authored
[frontend] Pass down image optimization requests to imageprovider in checkoutitem component (#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>
1 parent 95a3b5c commit 4d84a64

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ the release.
1919
([#1519](https://github.com/open-telemetry/opentelemetry-demo/pull/1519))
2020
* [flagd] export flagd traces to otel collector
2121
([#1522](https://github.com/open-telemetry/opentelemetry-demo/pull/1522))
22+
* [frontend] Pass down image optimization requests to imageprovider
23+
([#1522](https://github.com/open-telemetry/opentelemetry-demo/pull/1522))
2224

2325
## 1.9.0
2426

src/frontend/components/CheckoutItem/CheckoutItem.tsx

+25-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ interface IProps {
1414
address: Address;
1515
}
1616

17+
interface ImageLoaderProps {
18+
src: string;
19+
width: number;
20+
quality?: number;
21+
}
22+
/**
23+
* We connect to imageprovider through the envoy proxy, straight from the browser, for this we need to know the current hostname and port.
24+
* During building and serverside rendering, these are undefined so we use some conditionals and default values.
25+
*/
26+
let hostname = "";
27+
let port = 80;
28+
let protocol = "http";
29+
30+
if (typeof window !== "undefined" && window.location) {
31+
hostname = window.location.hostname;
32+
port = window.location.port ? parseInt(window.location.port, 10) : (window.location.protocol === "https:" ? 443 : 80);
33+
protocol = window.location.protocol.slice(0, -1); // Remove trailing ':'
34+
}
35+
const imageLoader = ({ src, width, quality }: ImageLoaderProps): string => {
36+
// 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.
37+
return `${protocol}://${hostname}:${port}/${src}?w=${width}&q=${quality || 75}`;
38+
}
39+
40+
1741
const CheckoutItem = ({
1842
checkoutItem: {
1943
item: {
@@ -29,7 +53,7 @@ const CheckoutItem = ({
2953
return (
3054
<S.CheckoutItem data-cy={CypressFields.CheckoutItem}>
3155
<S.ItemDetails>
32-
<S.ItemImage src={"/images/products/" + picture} alt={name} />
56+
<S.ItemImage src={"/images/products/" + picture} alt={name} loader={imageLoader}/>
3357
<S.Details>
3458
<S.ItemName>{name}</S.ItemName>
3559
<p>Quantity: {quantity}</p>

0 commit comments

Comments
 (0)