3

Lighthouse suggests that I need to remove the unused javascript from the third party javascript library say index.js . It is okay for this particular library to load after few 10 seconds once the page is loaded. So I used the SetTimeout method to do that but I don't think that is the right approach to do it.

Once I do that, lighthouse does not complain about unused javascript from this library and improves the score, but I am worried that in production Google measure the overall performance of the page from the moment page loads to the moment user leaves the page. So in that sense, the unused javascript is never removed and just delayed for the execution. People also suggested that lazy loading JS on user events will help but in our the case mentioned above, the JS should load automatically.

I am basically looking for the suggestion to:

  • How to handle the JS libraries that I can't get rid of and which has lot of unused code in the page?
  • Is setTimeout good solution for the above case?
  • Is my understanding of performance calculation by Google correct (in production environment) although it does not show unused JS after delaying for 10seconds?

I would be more than happy to get the answers.

Rabin Poudyal
  • 717
  • 6
  • 20
  • 1
    I would not to try lazyload the libaries with a setTimeout, but only load them when they are really needed by appending the file to the dom. Or try to load those files only on the needed page instead on all pages – Baracuda078 Aug 20 '21 at 12:42
  • @Baracuda078 Thanks for your suggestion but that library is really needed in that page in which the performance is measured. – Rabin Poudyal Aug 20 '21 at 12:46
  • If you really whant to clean up your code, I think you should use bundlers like `Gulp` or `Webpack`. I never used it but I know they have some npm packages that can remove unused code. – Baracuda078 Aug 20 '21 at 13:16
  • @Baracuda078 I use web pack. Actually the library I am talking does not support tree shaking so that's why I could not remove unused code that way. – Rabin Poudyal Aug 20 '21 at 15:55

0 Answers0