PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 31, 2022

[FIXED] how to hide html elements while waiting for all components to be loaded?

 October 31, 2022     html, javascript, lighthouse, optimization, performance     No comments   

Issue

chromium lighthouse tells me :

performance 89 (orange)

Cumulative Layout Shift 0.388 (red alert !)

Avoid large layout shifts

html :

<html>
   ...
   <body class="loading">
   ...some static elements
   </body>
   ...
</html>

css :

body.loading * { display : none; }

js when DOM is loaded :

document.addEventListener('DOMContentLoaded', (e) => {

    //do many dynamic stuff with the DOM depending on user data (&  register events listeners)

  })

& finally when everything is in place in the dom, through window.onload (or if that event comes too soon, a callback once everything is in place) :

document.body.classList.remove('loading')

If I remove the loading class from the html lighthouse says great perf 100.

I thought before seeing this lighthouse analysis that my layout was shifting from a simple loading (css) animation to a completely ready page with no visible changes of the dom for the user during the load so i assume now that i am wrong, doing something wrong & missing something.

How should i do that instead for optimal loading while not risking having elements that are not ready ever being displayed ?


Solution

It turns out that now Lighthouse doesn't flag for CLS anymore although I didn't make any changes there (on the opposite I've added some code in HTML, CSS and JS that should have made the page slower).

So answer is (at least until being proven otherwise):

Hiding elements while the page is loading & JavaScript is not ready doesn't have negative impact on performance.

Here is minimal code to have a ultra-light spinner while the page is not ready yet:

const MyOnLoad = function() {
    
    setTimeout(function(){ // only for the sake of displaying here the spinner 2 and half second obv
              
              document.body.classList.remove('loading')
        
        }
        ,2500) 
    
}

window.onload = MyOnLoad
.loading {
  margin: 34vmin auto;
  border: 0; 
  border-radius: 50%;
  border-top: 0.89vmin solid red;
  width: 21vmin;
  height: 21vmin;
  animation: spinner 2s linear infinite;

 }

 @keyframes spinner {
  0%   { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
 }

 .loading * {display: none;}
<html>
 <body class="loading">
    <main>show only when page is ready</main>
 </body>
</html>



Answered By - mikakun
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing