Skip to content

Commit

Permalink
feat: section loader
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnuescheler authored Jul 16, 2024
1 parent c633b9e commit caa123b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 59 deletions.
4 changes: 2 additions & 2 deletions blocks/fragment/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../../scripts/scripts.js';

import {
loadBlocks,
loadSections,
} from '../../scripts/aem.js';

/**
Expand All @@ -34,7 +34,7 @@ export async function loadFragment(path) {
resetAttributeBase('source', 'srcset');

decorateMain(main);
await loadBlocks(main);
await loadSections(main);
return main;
}
}
Expand Down
91 changes: 40 additions & 51 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,30 +556,6 @@ async function fetchPlaceholders(prefix = 'default') {
return window.placeholders[`${prefix}`];
}

/**
* Updates all section status in a container element.
* @param {Element} main The container element
*/
function updateSectionsStatus(main) {
const sections = [...main.querySelectorAll(':scope > div.section')];
for (let i = 0; i < sections.length; i += 1) {
const section = sections[i];
const status = section.dataset.sectionStatus;
if (status !== 'loaded') {
const loadingBlock = section.querySelector(
'.block[data-block-status="initialized"], .block[data-block-status="loading"]',
);
if (loadingBlock) {
section.dataset.sectionStatus = 'loading';
break;
} else {
section.dataset.sectionStatus = 'loaded';
section.style.display = null;
}
}
}
}

/**
* Builds a block DOM Element from a two dimensional array, string, or object
* @param {string} blockName name of the block
Expand Down Expand Up @@ -648,20 +624,6 @@ async function loadBlock(block) {
return block;
}

/**
* Loads JS and CSS for all blocks in a container element.
* @param {Element} main The container element
*/
async function loadBlocks(main) {
updateSectionsStatus(main);
const blocks = [...main.querySelectorAll('div.block')];
for (let i = 0; i < blocks.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await loadBlock(blocks[i]);
updateSectionsStatus(main);
}
}

/**
* Decorates a block.
* @param {Element} block The block element
Expand Down Expand Up @@ -713,17 +675,11 @@ async function loadFooter(footer) {
}

/**
* Load LCP block and/or wait for LCP in default content.
* @param {Array} lcpBlocks Array of blocks
* Wait for Image.
* @param {Element} section section element
*/
async function waitForLCP(lcpBlocks) {
const block = document.querySelector('.block');
const hasLCPBlock = block && lcpBlocks.includes(block.dataset.blockName);
if (hasLCPBlock) await loadBlock(block);

document.body.style.display = null;
const lcpCandidate = document.querySelector('main img');

async function waitForFirstImage(section) {
const lcpCandidate = section.querySelector('img');
await new Promise((resolve) => {
if (lcpCandidate && !lcpCandidate.complete) {
lcpCandidate.setAttribute('loading', 'eager');
Expand All @@ -735,6 +691,39 @@ async function waitForLCP(lcpBlocks) {
});
}

/**
* Loads all blocks in a section.
* @param {Element} section The section element
*/

async function loadSection(section, loadCallback) {
const status = section.dataset.sectionStatus;
if (!status || status === 'initialized') {
section.dataset.sectionStatus = 'loading';
const blocks = [...section.querySelectorAll('div.block')];
for (let i = 0; i < blocks.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await loadBlock(blocks[i]);
}
if (loadCallback) await loadCallback(section);
section.dataset.sectionStatus = 'loaded';
section.style.display = null;
}
}

/**
* Loads all sections.
* @param {Element} element The parent element of sections to load
*/

async function loadSections(element) {
const sections = [...element.querySelectorAll('div.section')];
for (let i = 0; i < sections.length; i += 1) {
// eslint-disable-next-line no-await-in-loop
await loadSection(sections[i]);
}
}

init();

export {
Expand All @@ -749,17 +738,17 @@ export {
fetchPlaceholders,
getMetadata,
loadBlock,
loadBlocks,
loadCSS,
loadFooter,
loadHeader,
loadScript,
loadSection,
loadSections,
readBlockConfig,
sampleRUM,
setup,
toCamelCase,
toClassName,
updateSectionsStatus,
waitForLCP,
waitForFirstImage,
wrapTextNodes,
};
11 changes: 5 additions & 6 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import {
decorateSections,
decorateBlocks,
decorateTemplateAndTheme,
waitForLCP,
loadBlocks,
waitForFirstImage,
loadSection,
loadSections,
loadCSS,
} from './aem.js';

const LCP_BLOCKS = []; // add your LCP blocks to the list

/**
* Builds hero block and prepends to main in a new section.
* @param {Element} main The container element
Expand Down Expand Up @@ -80,7 +79,7 @@ async function loadEager(doc) {
if (main) {
decorateMain(main);
document.body.classList.add('appear');
await waitForLCP(LCP_BLOCKS);
await loadSection(main.querySelector('.section'), waitForFirstImage);
}

try {
Expand All @@ -99,7 +98,7 @@ async function loadEager(doc) {
*/
async function loadLazy(doc) {
const main = doc.querySelector('main');
await loadBlocks(main);
await loadSections(main);

const { hash } = window.location;
const element = hash ? doc.getElementById(hash.substring(1)) : false;
Expand Down

0 comments on commit caa123b

Please sign in to comment.