Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update lazy loading #203

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,26 @@ export function loadLogin(main) {
return loadBlock(loginBlock);
}

function loadHeaderAndFooter(doc) {
const header = doc.querySelector('header');
const footer = doc.querySelector('footer');

header.style.visibility = 'hidden';
footer.style.visibility = 'hidden';

const headerLoaded = loadHeader(header);
const footerLoaded = loadFooter(footer);

headerLoaded.then(() => {
header.style.visibility = null;
});
footerLoaded.then(() => {
footer.style.visibility = null;
});

return Promise.all([headerLoaded, footerLoaded]);
}

/**
* Loads everything that doesn't need to be delayed.
* @param {Element} doc The container element
Expand All @@ -328,13 +348,18 @@ async function loadLazy(doc) {
const { hash } = window.location;
const element = hash ? doc.getElementById(hash.substring(1)) : false;
if (hash && element) element.scrollIntoView();
loadHeader(doc.querySelector('header'));
loadFooter(doc.querySelector('footer'));
loadLogin(doc.querySelector('main'));
// no need to await any of these individually
// but want them all to complete before moving on to delayed phase
const lazyPromises = [];

lazyPromises.push(loadHeaderAndFooter(doc));
lazyPromises.push(loadLogin(doc.querySelector('main')));
lazyPromises.push(loadCSS(`${window.hlx.codeBasePath}/styles/lazy-styles.css`));
lazyPromises.push(loadFonts());
lazyPromises.push(addFavIcon(`${window.hlx.codeBasePath}/styles/bhhs_seal_favicon.ico`));

await Promise.all(lazyPromises);
bstopp marked this conversation as resolved.
Show resolved Hide resolved

loadCSS(`${window.hlx.codeBasePath}/styles/lazy-styles.css`);
loadFonts();
addFavIcon(`${window.hlx.codeBasePath}/styles/bhhs_seal_favicon.ico`);
sampleRUM('lazy');
sampleRUM.observe(main.querySelectorAll('div[data-block-name]'));
sampleRUM.observe(main.querySelectorAll('picture > img'));
Expand Down
Loading