-
Notifications
You must be signed in to change notification settings - Fork 615
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storefront): BCTHEME-446 Improve performance of analyzing homepa…
…ge carousel image (#2027)
- Loading branch information
1 parent
c2f2303
commit 39abb0b
Showing
6 changed files
with
67 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters