【问题标题】:Angular 6 How to Increase speed of Inital load PageAngular 6如何提高初始加载页面的速度
【发布时间】:2019-05-30 20:31:33
【问题描述】:

您好,我不知道从服务器 gzip 压缩响应以减小捆绑 JS 大小!!!。

  • 我已经尝试过这个link 1,link 2,link 3,但仍然无法成功初始加载 页面太慢了。
  • Angular 包大小,例如 vendor.js 15 MB 大小,main.js 784KB, style.js 952KB。
  • 我的路由模块带有基于路由器的渲染组件。
  • 我已经更新了 package.json & angular.json 文件,我只是运行我的应用程序 使用 npm start 然后初始加载页面太慢,几乎 16.9 MB 大小 已转移。谢谢

Package.JSON

    {
      "name": "cfch",
      "version": "0.0.0",
      "scripts": {
        "ng": "ng",
        "start": "ng serve  --proxy-config  proxy.conf.json ",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
      },
    "dependencies": {
    "@angular/animations": "^7.1.4",
    "@angular/common": "^6.1.0",     //may be I need to downgrade to lower version? 
    "@angular/compiler": "^6.1.0",
    "@angular/core": "^6.1.0",
    "@angular/forms": "^6.1.0",
    "@angular/http": "^6.1.0",

      },
       "devDependencies": {
        "@angular-devkit/build-angular": "^0.8.8",
        "@angular/cli": "6.1.0",
        "@angular/compiler-cli": "^6.1.0",
        "@angular/language-service": "^6.1.0",
        "@types/jasmine": "^2.8.14",
         "typescript": "^2.7.2"
         }
    }

angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "CFCH": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/CFCH",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              {
                "glob": "**/*",
                "input": "./node_modules/@ant-design/icons-angular/src/inline-svg/",
                "output": "/assets/"
              }
            ],
            "styles": [
              "src/theme.less",
              "node_modules/ng-zorro-antd/src/ng-zorro-antd.min.css",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "CFCH:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "CFCH:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "CFCH:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/theme.less",
              "src/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "CFCH-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "CFCH:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "CFCH:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "CFCH"
}

app-routing.module.ts

    const routes: Routes = [
    {
        path: '',
        component: CfchDataTableComponent,
       //loadChildren:'./cfch-data-table/cfch-data-table.component#CfchDataTableComponent',  //lazy loaded module
        //path: '', loadChildren: () => CfchDataTableComponent,
        data: { preload: true }

        //pathMatch: 'full',
    },
    {
        path:'singleCompanyStockList',
        component:SingleCompanyListComponent
    },
    {
        path:'valuation',
        component:ValuationComponent
    },
    {
        path:'risk',
        component:RiskComponent
    },
    {   path:'login',
    component:LoginComponent
   },
   {   path:'register',
    component:RegisterPageComponent
   },
   {   path:'forgetPwd',
    component:ForgetPasswordComponent
   },
   {   path:'info',
    component:InfoComponent
   },
   {   path:'essentialInfo',
    component:EssentialInfoComponent
  },

   {    path:'manageCompany',
        //canActivate: [AuthGuard],
        component:ManageCompanyComponent
   },

   {   
        path:'dataManage',
        //canActivate: [AuthGuard],
        component:DatamanageComponent
   },
   {
        path:'benchmark',
        canActivate: [AuthGuard],
        component:BenchmarkingComponent
   },
   {
    path:'financeEntry',
    component:FinanceEntryComponent
   },



   {   path:'joinUS',
        component:JoinUsPageComponent,
   },

];
@NgModule({
    imports: [RouterModule.forRoot(routes)
    ],
    exports: [RouterModule],
})
export class AppRoutingModule { }

- 请查看附件中的浏览器控制台网络结果

【问题讨论】:

  • 生产版本中捆绑包的大小是多少? ng build --prod
  • Angular 包大小,例如 vendor.js 15 MB 大小,main.js 784KB,style.js 952KB
  • 这是一个疯狂的供应商捆绑包 - 您是否使用 AOT 编译器/构建优化器? (如果没有,为什么不呢?)
  • 我已经更新了 package.json 文件,我只是用 npm start 运行我的应用程序,然后初始加载页面太慢了,几乎传输了 16.9 MB 大小。谢谢
  • 修改 package.json。将“build”:“ng build”更改为“build”:“ng build --prod”,

标签: angular angular6 angular-cli


【解决方案1】:

Gzip 由您的托管服务器完成,该服务器为您的 Angular 应用程序提供服务。它与您的 javascript 框架无关,无论是 Angular 还是其他任何框架。

不妨碍大多数云托管服务提供商开箱即用地配置 gzip 压缩。但我发现免费且更容易用于个人项目试用的是来自 google 的 firebase.com,它也免费提供 SSL,您可以查看他们的托管服务。

【讨论】:

  • 我想减少捆绑JS文件,因为角度初始加载太慢,请看上图结果浏览器控制台网络传输16.9 MB,那么我该如何减少大小?谢谢
  • 有一个适用于您的链接问题的编辑:EDIT: Seems I misunderstood the question, if it is about bundle size when serving the stuff to the end user, you should take a look at AOT + Rollup to minimize your bundle sizes. And enable gzip compression on your webserver when serving files (probably most servers have it enabled already).
  • @sameer 你能发布你的 package.json 和 angular.json 文件吗?
  • 请看我用 angular.json 文件更新的帖子。谢谢
  • 好吧,在查看你的路由模块之后,你似乎把你的整个应用程序放在一个模块中,这不是使用 Angular 开发的推荐方式,因为它会产生非常大的初始负载。推荐的方法是使用延迟加载,Angular 以延迟加载模块的形式使用延迟加载的概念,您可以在官方文档 (angular.io/guide/lazy-loading-ngmodules) 中阅读有关它们的信息。这个视频可以给你很好的开端youtube.com/watch?v=8tBQI5grhbE。但我建议您阅读更多关于延迟加载的主题。祝你好运
【解决方案2】:

**延迟加载** 你必须将应用路由模块更改为延迟加载模块。显着减小 main.js 和 vendor.js 的大小。

【讨论】:

    【解决方案3】:

    通过修改您的 package.json 启用生产编译

    这个:

    {
      "name": "cfch",
      "version": "0.0.0",
      "scripts": {
        "ng": "ng",
        "start": "ng serve  --proxy-config  proxy.conf.json ",
        "build": "ng build",
    

    应该是:

    (添加--prod)

    {
      "name": "cfch",
      "version": "0.0.0",
      "scripts": {
        "ng": "ng",
        "start": "ng serve  --proxy-config  proxy.conf.json ",
        "build": "ng build --prod",
    

    您的供应商 Java 脚本在您的 dist 目录中应该小得多。

    当然,这不会在运行“npm start”时改变 Java 脚本的大小。那是开发者模式。

    【讨论】:

    • 嗨,在 build 中添加 --prod 后,我收到如下错误:" ERROR in ./src/app/app.module.ngfactory.js" ,我正在使用 angular 6 和angular cli 7 version.reference 链接尝试但没有运气:stackoverflow.com/questions/48402695/… 但我不想降级我的最新版本。谢谢
    • 你能把你的 "@angular/cli": 从 "7.1.4" 切换到 "6.1.0" 吗?这是一个很大的猜测。生产编译要严格得多(一件好事)也不要混合版本,@angular/animations 应该与您的其他 @angular 组件的版本相同
    • 我曾经构建过 ng build --prod --aot=false
    • 你的 js 会小很多,但不会那么小。
    猜你喜欢
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2020-11-07
    相关资源
    最近更新 更多