生成应用程序时找不到Angular Service Worker /未注册错误

时间:2018-10-11 09:06:11

标签: angular angular-service-worker

尝试使用ng build --prod构建应用时出现以下错误

Error: Expected to find an ngsw-config.json configuration file in the 'project' folder.

如果我运行ng serve --prod,该应用程序将成功运行,但会获得一个控制台error TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.

我曾尝试将ngsw-config.json文件移至应用程序根目录和src目录中,但无济于事。

我的angular.json文件如下。我使用的是角度6。请协助。

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "ngJava": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/ngJava",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.json"
            ],
            "styles": [
              "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,
              "serviceWorker": true
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "ngJava:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "ngJava:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "ngJava: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/styles.css"
            ],
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/manifest.json"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "ngJava-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "ngJava:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "ngJava:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "ngJava"
}

1 个答案:

答案 0 :(得分:0)

当我将ngsw-config.json放入src时,我遇到了相同的错误(请参阅下文)。正确的文件夹是您应用程序的根目录。在您的情况下,它应该是ngJava文件夹。您没有提供有关该项目的更多信息(例如,文件夹结构的快照),而且我不确定如何首先实现sw。

如果文件位于正确的位置,建议您通过运行ng add @angular/pwa --project ngJava

确保正确实现了sw。

其他说明位于https://v6.angular.io/guide/service-worker-getting-started

如果仍然无法解决问题,请随时与我联系,我们很乐意帮助您解决问题。

Error: Expected to find an ngsw-config.json configuration file in the /home/alberto/Workspaces/KiAndCo/kiandco/ClientApp folder. Either provide one or disable Service Worker in your angular.json configuration file.