Google insight is showing very low performance in angular application
Asked Answered
C

2

6

I am running an angular application which is showing very low score in google page insights (13 in mobile and 43 in desktop). I am running this on Nginx server and all the compression and caching issues are solved from Nginx side but still it shows very low score in page insight. There is only one image in the page and it is already compressed.

Erros are to remove unused javascript and css files. But the files they are showing error are the build files which are generated from the angular production build. I am not getting how that files are unused. I am attaching the screenshot of errors here. Please help me solving these issues.enter image description here enter image description here enter image description here

Carboxylase answered 22/8, 2020 at 9:52 Comment(0)
S
1

The issue is Zone.js - Zone.js is slowing down the application on google page speed, I have the same problem (for years). You can try too by disabling the zone.js on polyfills, and then add the zone.js file on assets and load as a script tag on the index.html and you will see the issue, and then disable the zone.js tag and run the lighthouse.

Test google page speed:

  1. normal zone.js in polyfills.
  2. Zone.js in assets with script tag on the index.html (on the head) - make sure to disable on polyfills
  3. Disable zone.js on disable on polyfills and or index.html
Superorder answered 28/1, 2021 at 10:22 Comment(1)
Have a look at this article, the angular team doesn't have a clue github.com/angular/angular/issues/32103Superorder
S
0

It isn't that the files are unused, it is that there is a large proportion of the files that contain code that is not executed / needed to render the initial page.

What you want to do is split your JavaScript into "essential" and "non-essential".

"Essential" is everything that is required to render / use anything that appears "above the fold" (content that appears without scrolling). Everything else is "non-essential" as the page can be rendered without it and it can load in the background.

The whole point of this is perceived speed, the site may take the same amount of time to load overall, but if you split your JS like this it will appear to load a lot faster, helping conversion rates etc. etc.

To achieve this you would either have to manually find all the essential JS and package it separately or use a method such as tree-shaking to remove unused code and then use code-splitting to serve high priority JS first (the article linked was the first one I found on code-splitting in Angular, you will have to do your own research for that I am afraid as I am unfamiliar with an Angular workflow).

Lazy loading JS

Below is a simple method to lazy-load any non-essential JS once you have identified it that works all the way back to IE9.

var dfr = function () {
    var n1 = document.getElementById("ds");
    var r1 = document.createElement("div");
    r1.innerHTML = n1.textContent;
    document.body.appendChild(r1);
    n1.parentElement.removeChild(n1);
};

var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
        window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
if (raf)
    raf(function () {
        window.setTimeout(dfr, 0);
    });
else
    window.addEventListener("load", dfr);
<head>
  <noscript id="ds">
    <script src="some-script-that-is-not-essential.js"></script>
  </noscript>
</head>
Submersed answered 22/8, 2020 at 10:11 Comment(4)
Hello @Graham Ritchie, Thanks for your help. It just got increased by 5 points . It is now 49. Can I do anything else than this ?Carboxylase
It depends on whether you are rendering the page using Angular or just using Angular for data manipulation. If the site will render without JS you can add defer or async to the scripts (async is harder if your site relies on load order for dependencies) . You then need to start tackling other things on the site such as inlining critical CSS etc. Also with the "initial server response time" it looks like you are either querying the database or building things on the fly, caching the page is a good step there.Submersed
I have already removed all unnecessary js and CSS from index.html files and also added async and difer in scripts but nothing is just helpingCarboxylase
Then you need to look at number of requests, page weight etc. 2Mb is large so I would look at shaving that if you can. For requests try combining files until you get requests under 8 total (due to round-trip time and the max number of simultaneous requests being 8) etc. Sadly no magic bullet for you here, just hard graft and loads of optimisation bit by bit.Submersed

© 2022 - 2024 — McMap. All rights reserved.