部署 Angular 8 应用后如何执行缓存清除

6

我有一个 Angular 8 应用程序,部署后经常收到最终用户的投诉,他们无法看到新功能。我需要加入缓存破坏吗?在进行了一些谷歌搜索后,我看到了以下命令:

ng build --output-hashing=all

我的问题是,使用 Angular 8 CLI 的 ng build --prod 命令是否考虑了缓存破坏,或者我需要在部署步骤中添加以下命令。我还想知道如何测试它。

我的 angular.json 文件

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "quickapp": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "css"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "aot": false,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css",
              "src/app/assets/styles/components/buttons.css",
              "src/app/assets/styles/components/forms.css",
              "src/app/assets/styles/components/inputs.css",
              "./node_modules/bootstrap/dist/css/bootstrap.min.css",
              "./node_modules/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css"
            ],
            "scripts": [
              "./node_modules/jquery/dist/jquery.min.js",
              "./node_modules/bootstrap/dist/js/bootstrap.js",
              "./node_modules/bootstrap-toggle/js/bootstrap-toggle.js",
              "./node_modules/bootstrap-select/dist/js/bootstrap-select.js",
              "./node_modules/bootstrap-datepicker/dist/js/bootstrap-datepicker.js",
              "./node_modules/moment/moment.js",
              "./src/app/assets/scripts/drive-js.js",
              "./src/app/assets/scripts/external-js.js"
            ]
          },
          "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,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "400kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "quickapp:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "quickapp:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "quickapp:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "src/styles.css"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "tsconfig.app.json",
              "tsconfig.spec.json",
              "e2e/tsconfig.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "quickapp:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "quickapp:serve:production"
            }
          }
        }
      }
    }},
  "defaultProject": "quickapp"
}
1个回答

2

如您在angular.json输出中所见,默认情况下启用了哈希。问题可能是您的用户正在缓存index.html文件,因此他不知道新块的存在。 这也可能是由于未被Angular“哈希”的缓存静态文件造成的问题。请参阅https://stackoverflow.com/Questions/728616/disable-cache-for-some-images

    "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all", <---- enabled by default
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }

我已经使用我的angular.json更新了帖子。不幸的是,在生产部分下没有看到任何设置。这是个问题吗?我需要添加你分享的内容吗?最近,我将Angular 5项目升级到了Angular 8。我选择了Angular 8的angular json文件,这是在创建新项目时默认生成的,但它没有这些开关。 - Tom
这是Angular 8 CLI的默认angular.json:https://pastebin.com/hxudJux9。 您可以轻松地查看哈希是否已启用。在运行ng build --prod之后,您将在dist文件夹中看到类似于以下名称的文件:runtime-es2015.d61f42a1e4be5689ac82.js。如果随机数更改,则已启用哈希。 - karoluS
我刚刚运行了ng build --prod,而且在生成的json中没有这些条目,它还是进行了哈希。那么这些条目是必需的吗? - Tom
我可以在你的 angular.json 文件中看到这些条目。从第50行开始。 - karoluS
当你说问题可能是用户缓存了index.html文件时,这是否意味着将网站加入书签? - Tom
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接