Skip to content

Commit

Permalink
feat: implement optimizevtex img option
Browse files Browse the repository at this point in the history
  • Loading branch information
aline-pereira committed Feb 25, 2025
1 parent fdc26a5 commit 864dac1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
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 @@ export type Props =
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 @@ interface OptimizationOptions {
height?: number;
factor: number;
fit: FitOptions;
optimizeVtex?: "auto" | "manual";
}

const optmizeVNDA = (opts: OptimizationOptions) => {
Expand Down Expand Up @@ -102,7 +105,8 @@ export const getOptimizedMediaUrl = (opts: OptimizationOptions) => {
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 @@ export const getSrcSet = (
width: number,
height?: number,
fit?: FitOptions,
optimizeVtex?: "auto" | "manual",
) => {
const srcSet = [];

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

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

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 Down

0 comments on commit 864dac1

Please sign in to comment.