Skip to content

Commit

Permalink
MWPW-139621 - LCP image loading of fragments
Browse files Browse the repository at this point in the history
- implement decorateArea function from milo
- only run loadLCPImage function if no previous LCP image has been set already
  • Loading branch information
honstar committed Dec 7, 2023
1 parent 77bdf81 commit f2f7e50
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions creativecloud/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ const locales = {
gr_el: { ietf: 'el', tk: 'fnx0rsr.css' }, // Greece (Greek)
};

const decorateArea = getDecorateAreaFn();

Check failure on line 119 in creativecloud/scripts/scripts.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 'getDecorateAreaFn' was used before it was defined. Raw Output: {"ruleId":"no-use-before-define","severity":2,"message":"'getDecorateAreaFn' was used before it was defined.","line":119,"column":22,"nodeType":"Identifier","messageId":"usedBeforeDefined","endLine":119,"endColumn":39}

// Add any config options.
const CONFIG = {
contentRoot: '/cc-shared',
Expand All @@ -124,6 +126,7 @@ const CONFIG = {
locales,
geoRouting: 'on',
prodDomains: ['www.adobe.com'],
decorateArea,
stage: {
marTechUrl: 'https://assets.adobedtm.com/d4d114c60e50/a0e989131fd5/launch-2c94beadc94f-development.min.js',
edgeConfigId: '8d2805dd-85bf-4748-82eb-f99fdad117a6',
Expand All @@ -147,33 +150,45 @@ const CONFIG = {
},
};

// Load LCP image immediately
const eagerLoad = (img) => {
img?.setAttribute('loading', 'eager');
img?.setAttribute('fetchpriority', 'high');
};
function getDecorateAreaFn() {
let lcpImgSet = false;

(async function loadLCPImage() {
const firstDiv = document.querySelector('body > main > div:nth-child(1) > div');
let fgDivs = null;
switch (true) {
case firstDiv?.classList.contains('changebg'):
firstDiv.querySelector(':scope > div:nth-child(1)').querySelectorAll('img').forEach(eagerLoad);
import(`${CONFIG.codeRoot}/deps/interactive-marquee-changebg/changeBgMarquee.js`);
break;
case firstDiv?.classList.contains('marquee'):
firstDiv.querySelectorAll('img').forEach(eagerLoad);
break;
case firstDiv?.classList.contains('interactive-marquee'):
firstDiv.querySelector(':scope > div:nth-child(1)').querySelectorAll('img').forEach(eagerLoad);
fgDivs = firstDiv.querySelector(':scope > div:nth-child(2)').querySelectorAll('div:not(:first-child)');
fgDivs.forEach((d) => eagerLoad(d.querySelector('img')));
break;
default:
eagerLoad(document.querySelector('img'));
break;
// Load LCP image immediately
const eagerLoad = (lcpImg) => {
lcpImg?.setAttribute('loading', 'eager');
lcpImg?.setAttribute('fetchpriority', 'high');
if (lcpImg) lcpImgSet = true;
};

function loadLCPImage(area = document, { fragmentLink = null } = {} ) {

Check failure on line 163 in creativecloud/scripts/scripts.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 There should be no space before this paren. Raw Output: {"ruleId":"space-in-parens","severity":2,"message":"There should be no space before this paren.","line":163,"column":70,"nodeType":"Program","messageId":"rejectedClosingSpace","endLine":163,"endColumn":71,"fix":{"range":[6769,6770],"text":""}}
const firstBlock = area.querySelector('body > main > div > div');
let fgDivs = null;
switch (true) {
case firstBlock?.classList.contains('changebg'):
firstBlock.querySelector(':scope > div:nth-child(1)').querySelectorAll('img').forEach(eagerLoad);
import(`${CONFIG.codeRoot}/deps/interactive-marquee-changebg/changeBgMarquee.js`);
break;
case firstBlock?.classList.contains('marquee'):
firstBlock.querySelectorAll('img').forEach(eagerLoad);
break;
case firstBlock?.classList.contains('interactive-marquee'):
firstBlock.querySelector(':scope > div:nth-child(1)').querySelectorAll('img').forEach(eagerLoad);
fgDivs = firstBlock.querySelector(':scope > div:nth-child(2)').querySelectorAll('div:not(:first-child)');
fgDivs.forEach((d) => eagerLoad(d.querySelector('img')));
break;
default:
if (window.document.querySelector('a.fragment') == fragmentLink && !window.document.querySelector('img[loading="eager"]')) {

Check failure on line 180 in creativecloud/scripts/scripts.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Expected '===' and instead saw '=='. Raw Output: {"ruleId":"eqeqeq","severity":2,"message":"Expected '===' and instead saw '=='.","line":180,"column":57,"nodeType":"BinaryExpression","messageId":"unexpected","endLine":180,"endColumn":59}
eagerLoad(area.querySelector('img'));
}
break;
}
}
}());

return (area, options) => {
if (!lcpImgSet) loadLCPImage(area, options);
}

Check failure on line 189 in creativecloud/scripts/scripts.js

View workflow job for this annotation

GitHub Actions / Running eslint

[eslint] reported by reviewdog 🐶 Missing semicolon. Raw Output: {"ruleId":"semi","severity":2,"message":"Missing semicolon.","line":189,"column":4,"nodeType":"ReturnStatement","messageId":"missingSemi","endLine":190,"endColumn":1,"fix":{"range":[7967,7967],"text":";"}}
}
decorateArea();

/*
* ------------------------------------------------------------
Expand Down

0 comments on commit f2f7e50

Please sign in to comment.