Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 in checkoutitem component
austinlparker marked this conversation as resolved.
Show resolved Hide resolved
([#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
Loading