4

I use Angular 7 as ssr, and we found out that our application has heavy and serious problems with Google Page Speed and all the issues are funnelling to zone.js

Page speed complain about the script valuation is too high, after breaking down the app, we found the issues were related to polyfills, however, polyfills only containing

  import 'zone.js/dist/zone.js';  // Included with Angular CLI.
  import 'classlist.js/classList.min.js';
  import 'web-animations-js/web-animations.min.js';

then what we did was totally remove all the content on polyfills file, leave it empty, and add those files, directly from a CDN on the bottom of index.html as:

<script src="https://cdnjs.cloudflare.com/ajax/libs/zone.js/0.9.1/zone.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.2.201711092/classList.min.js"></script>

and the result was the same, the page speed still complaining about script valuation, however, we manage to reduce 50% of the script valuation, however, still and is really bad.

We were using a zone.js@0.8.10 then we upgrade to latest but nothing. Same result.

In fact, this is affecting all our apps no matter if we build as aot or server-side render, the result is the same.

Does anyone know how to solve this issue with page speed? Our ranking, is below 20 over 100 as SSR.

This doesn't make any sense, it supposes to be faster, not slower?

image page speed polyfills

image page speed - cdn url

package.json

      "scripts": {
  "ng": "ng",
  "start": "ng serve --watch --open --progress",
  "build": "ng build",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e",
  "build:ssr": "rimraf dist && npm run build:client-and-server-bundles && npm run webpack:server -p",
  "serve:ssr": "node dist/server.js",
  "build:client-and-server-bundles": "node --max_old_space_size=4000 node_modules/@angular/cli/bin/ng build --prod && ng run micksgarage-project:server",
  "webpack:server": "webpack --config webpack.server.config.js --progress --colors",
  "bundle-report": "webpack-bundle-analyzer dist/*/stats.json"
},
"private": true,
"dependencies": {
  "@angular-devkit/build-optimizer": "^0.802.0",
  "@angular/common": "~7.2.0",
  "@angular/compiler": "~7.2.0",
  "@angular/core": "~7.2.0",
  "@angular/forms": "~7.2.0",
  "@angular/http": "~7.2.0",
  "@angular/platform-browser": "~7.2.0",
  "@angular/platform-browser-dynamic": "~7.2.0",
  "@angular/platform-server": "~7.1.0",
  "@angular/pwa": "^0.12.4",
  "@angular/router": "~7.2.0",
  "@angular/service-worker": "~7.2.0",
  "@nguniversal/express-engine": "~7.1.0",
  "@nguniversal/module-map-ngfactory-loader": "~7.1.0",
  "classlist.js": "^1.1.20150312",
  "core-js": "~2.5.4",
  "rxjs": "~6.3.3",
  "rxjs-compat": "~6.3.3",
  "zone.js": "^0.10.1"
},
"devDependencies": {

angular.json as angular.io SSR configuration plus:

          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": true,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": true,
          "commonChunk": true,
          "buildOptimizer": true,
          "statsJson": true,
          "budgets": [
            {
              "type": "initial",
              "maximumWarning": "2mb",
              "maximumError": "5mb"
            }
          ],
          "serviceWorker": true,
          "ngswConfigPath": "src/ngsw-config.json"

I even try zone-evergreen.min.js have a look on the screen shot

 image page speed with zone-evergreen.min.js

Also I found plenty of sites with the same issue, related to polyfills.ts or have problems with Google Page speed, you can see here https://www.madewithangular.com/categories/angular/ that any site made with Angular have this issue with zone.js. Please check any site, copy and paste the URL into Page Speed, and you will see the same issue, over and over again, this is not a coincidence.

and many more.

halfer
  • 19,824
  • 17
  • 99
  • 186
jcdsr
  • 1,123
  • 1
  • 17
  • 35

1 Answers1

1

Basically, Angular uses Zone.js to run change detection every time some async event happened.

The problem is not the zone itself. Consider posting some parts of the application (on stackblitz maybe).

First guess: Are you using functions for template binding? (or getters!)

// component.HTML
<p>{{ getName() }}</p>

// component.TS
getName(): string {
    return 'John Doe';
}

If the answer is yes, function is called on each async event (mouse move, click..) and every time event is triggered, change detection is going to run and check all the values if they are changed.

Hope that helps.

grizlizli
  • 137
  • 7
  • Thanks, I detect 4 places that use functions in binding, however, none those components loads on the homepage. – jcdsr Aug 12 '19 at 13:58
  • Will {{ name.toLowerCase() }} will trigger ? – jcdsr Aug 12 '19 at 13:59
  • Yes! If you want to transform DOM value, use Angular Pipes. For this case: {{ name | lowercase }} – grizlizli Aug 12 '19 at 14:03
  • 1) {{ name }} // value is updated only when you actually change the value of the component's property. 2) {{ name }} // get name() => this._name; and Angular cannot know if change has happened. it's a function call. – grizlizli Aug 12 '19 at 14:07
  • Also, Angular Pipe only updates the DOM if the input parameter is changed: {{ name | lowercase }}, where 'name' is an input parameter of the Pipe's transform function. – grizlizli Aug 12 '19 at 14:08
  • thanks, we update the binding, however, didn't change that much, the bundle still with a high script valuation. – jcdsr Aug 12 '19 at 14:09
  • Are you able to share the repository or parts of it? – grizlizli Aug 12 '19 at 14:10
  • I'm not allowed to share the project, however, what parts do you need? – jcdsr Aug 12 '19 at 14:15
  • I cannot help without more information. It could be anything basically. I encourage you to update to Angular 8 which supports Differential Loading by default (detects if the browser is legacy and needs polyfills; if it is, it will load the polyfills, otherwise will not). Also, I discourage you to load the zone.js that way. – grizlizli Aug 12 '19 at 14:31
  • I found plenty of sites with the same issue, related to polyfills.ts or have problems with google page speed, you can see here https://www.madewithangular.com/categories/angular/ that any site made with angular have this issue with zone.js Please check any site, copy and paste the URL into page speed and you will see the same issue, over and over again, this is not a coincidence. https://www.delta.com/eu/en https://www.forbes.com and many more have... Angular and zone.js must address this issue asap! – jcdsr Aug 15 '19 at 08:47
  • I just analyzed some of the apps that I build with Angular. I don't have anything similar. I think it is not a default behavior. [screenshot](https://imgur.com/gY6gVG6) @jcdsr – grizlizli Aug 15 '19 at 09:22
  • I think you have to test more because your comments are not helping to find the solution – jcdsr Aug 18 '19 at 14:22
  • 1
    We are having the same issue after upgrading to Angular 8, we have implemented SSR, lazy loading and service workers but script evaluation related to zone.js is still causing a poor Google page speed score. – Shane Aug 20 '19 at 11:09