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

Feat: implement optimize vtex image option #1023

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions website/components/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
fetchPriority?: "high" | "low" | "auto";
/** @description Object-fit */
fit?: FitOptions;
/** @description Controls whether VTEX images should be optimized */
optimizeVtex?: "auto" | "manual";
};

const FACTORS = [1, 2];
Expand All @@ -41,6 +43,7 @@
height?: number;
factor: number;
fit: FitOptions;
optimizeVtex?: "auto" | "manual";
}

const optmizeVNDA = (opts: OptimizationOptions) => {
Expand Down Expand Up @@ -102,7 +105,8 @@
if (
/(vteximg.com.br|vtexassets.com|myvtex.com)\/arquivos\/ids\/\d+/.test(
originalSrc,
)
) &&
opts.optimizeVtex !== "manual"
) {
return optimizeVTEX(opts);
}
Expand Down Expand Up @@ -133,6 +137,7 @@
width: number,
height?: number,
fit?: FitOptions,
optimizeVtex?: "auto" | "manual",
) => {
const srcSet = [];

Expand All @@ -147,6 +152,7 @@
height: h,
factor,
fit: fit || "cover",
optimizeVtex,
});

if (src) {
Expand All @@ -158,15 +164,15 @@
};

const Image = forwardRef<HTMLImageElement, Props>((props, ref) => {
const { preload, loading = "lazy" } = props;
const { preload, loading = "lazy", optimizeVtex = "auto" } = props;

if (!props.height) {
console.warn(
`Missing height. This image will NOT be optimized: ${props.src}`,
);
}

const srcSet = getSrcSet(props.src, props.width, props.height, props.fit);
const srcSet = getSrcSet(props.src, props.width, props.height, props.fit, optimizeVtex);
const linkProps = srcSet && {
imagesrcset: srcSet,
imagesizes: props.sizes,
Expand All @@ -189,7 +195,7 @@
srcSet={srcSet}
loading={loading}
ref={ref}
/>

Check failure on line 198 in website/components/Image.tsx

View workflow job for this annotation

GitHub Actions / Bundle & Check Apps (ubuntu-latest)

Passing 'true' to boolean attributes is the same as not passing it`
</>
);
});
Expand Down
Loading