Skip to content

Commit

Permalink
feat(storefront): BCTHEME-446 Improve performance of analyzing homepa…
Browse files Browse the repository at this point in the history
…ge carousel image
  • Loading branch information
yurytut1993 committed Apr 13, 2021
1 parent d59675f commit 516fc7d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Draft
- Improve performance of analyzing homepage carousel images. [#2027](https://github.com/bigcommerce/cornerstone/pull/2027)

## 5.3.0 (03-25-2021)
- Remove AddThis for social sharing, replace with provider sharing links. [#1997](https://github.com/bigcommerce/cornerstone/pull/1997)
Expand Down
10 changes: 3 additions & 7 deletions assets/js/theme/common/carousel/utils/getActiveSlideInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ export default ({ $slider }, isAnalyzedDataAttr) => {
if (isAnalyzedSlide) return { isAnalyzedSlide };

const $activeSlideImg = $activeSlide.find('.heroCarousel-image');

const attrsObj = {
src: $activeSlideImg.attr('src'),
srcset: $activeSlideImg.attr('srcset'),
sizes: $activeSlideImg.attr('sizes'),
};
const activeSlideImgNode = $activeSlideImg[0];

return {
attrsObj,
$slider,
$activeSlide,
$activeSlideImg,
activeSlideImgNode,
};
};
35 changes: 20 additions & 15 deletions assets/js/theme/common/carousel/utils/handleImageAspectRatio.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const IMAGE_CLASSES = {
};
const IS_ANALYZED_DATA_ATTR = 'image-ratio-analyzed';

const defineClass = (imageAspectRatio) => {
const defineAspectRatioClass = (imageAspectRatio) => {
switch (true) {
case imageAspectRatio > 0.8 && imageAspectRatio <= 1.2:
return IMAGE_CLASSES.square;
Expand All @@ -17,13 +17,21 @@ const defineClass = (imageAspectRatio) => {
}
};

export default ({ delegateTarget }, carousel) => {
const setAspectRatioClass = (imageNode, $slides) => {
if (imageNode.naturalHeight <= 1) return;

const imageAspectRatio = imageNode.naturalHeight / imageNode.naturalWidth;
$slides.each((idx, slide) => $(slide).addClass(defineAspectRatioClass(imageAspectRatio)));
};

export default ({ delegateTarget }, carouselObj) => {
const {
isAnalyzedSlide,
attrsObj,
$slider,
$activeSlide,
} = getActiveSlideInfo(carousel || delegateTarget.slick, IS_ANALYZED_DATA_ATTR);
$activeSlideImg,
activeSlideImgNode,
} = getActiveSlideInfo(carouselObj || delegateTarget.slick, IS_ANALYZED_DATA_ATTR);

if (isAnalyzedSlide) return;

Expand All @@ -32,15 +40,12 @@ export default ({ delegateTarget }, carousel) => {

if ($activeSlide.find('.heroCarousel-content').length) return;

$('<img />')
.on('load', function getImageSizes() {
const imageHeight = this.height;
const imageWidth = this.width;

if (imageHeight < 2 || imageWidth < 2) return;

const imageAspectRatio = imageHeight / imageWidth;
$activeSlideAndClones.each((idx, slide) => $(slide).addClass(defineClass(imageAspectRatio)));
})
.attr(attrsObj);
if (activeSlideImgNode.complete) {
if (activeSlideImgNode.naturalHeight === 1) {
// only base64 image from srcset was loaded
$activeSlideImg.on('load', () => setAspectRatioClass(activeSlideImgNode, $activeSlideAndClones));
} else if (activeSlideImgNode.naturalHeight > 1) {
setAspectRatioClass(activeSlideImgNode, $activeSlideAndClones);
}
} else $activeSlideImg.on('load', () => setAspectRatioClass(activeSlideImgNode, $activeSlideAndClones));
};
39 changes: 33 additions & 6 deletions assets/js/theme/common/carousel/utils/handleImageLoad.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import { isBrowserIE } from '../../utils/ie-helpers';
import getActiveSlideInfo from './getActiveSlideInfo';

const IMAGE_ERROR_CLASS = 'is-image-error';
const IS_ANALYZED_DATA_ATTR = 'image-load-analyzed';

export default (e, carousel) => {
const generateImage = ($slide, $image) => {
$('<img />')
.on('error', () => $slide.addClass(IMAGE_ERROR_CLASS))
.attr('src', $image.attr('src'));
};

export default (e, carouselObj) => {
const {
isAnalyzedSlide,
attrsObj,
$activeSlide,
} = getActiveSlideInfo(carousel, IS_ANALYZED_DATA_ATTR);
$activeSlideImg,
activeSlideImgNode,
} = getActiveSlideInfo(carouselObj, IS_ANALYZED_DATA_ATTR);

if (isAnalyzedSlide) return;

$activeSlide.data(IS_ANALYZED_DATA_ATTR, true);

$('<img />')
.on('error', () => $activeSlide.addClass(IMAGE_ERROR_CLASS))
.attr(attrsObj);
if (activeSlideImgNode.complete) {
if (activeSlideImgNode.naturalHeight === 0) {
$activeSlide.addClass(IMAGE_ERROR_CLASS);
} else if (activeSlideImgNode.naturalHeight === 1) {
// only base64 image from srcset was loaded
$activeSlideImg.on('error', () => $activeSlide.addClass(IMAGE_ERROR_CLASS));
}

return;
}

if (!$activeSlideImg.attr('src')) {
$activeSlide.addClass(IMAGE_ERROR_CLASS);
return;
}

if (isBrowserIE) {
generateImage($activeSlide, $activeSlideImg);
return;
}

$activeSlideImg.on('error', () => $activeSlide.addClass(IMAGE_ERROR_CLASS));
};
2 changes: 1 addition & 1 deletion assets/js/theme/common/utils/ie-helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const isBrowserIE = navigator.userAgent.includes('Trident');
export const isBrowserIE = !!document.documentMode;

export const convertIntoArray = collection => Array.prototype.slice.call(collection);
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
}
}

// for IE
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
opacity: 0;

&.slick-initialized {
opacity: 1;
}
}

&:not(.slick-initialized) :not(.heroCarousel-slide--first).heroCarousel-slide {
display: none;
}
Expand Down

0 comments on commit 516fc7d

Please sign in to comment.