简体   繁体   English

运行build --prod时,没有Service worker文件生成ngsw.json / ngsw-worker.json。 Angular 5

[英]No Service worker files ngsw.json / ngsw-worker.json generated when running build --prod. Angular 5

Using angular 5.1.2 Angular-cli 1.6.3 使用角度5.1.2 Angular-cli 1.6.3

all files are pretty vanilla. 所有文件都很香草。 dist folder is created dist文件夹已创建

My ngsw-config.json 我的ngsw-config.json

{
"index": "/index.html",
"assetGroups": [
    {
        "name": "app",
        "installMode": "prefetch",
        "resources": {
            "files": [
                "/favicon.ico",
                "/index.html"
            ],
            "versionedFiles": [
                "/*.bundle.css",
                "/*.bundle.js",
                "/*.chunk.js"
            ]
        }
    },
    {
        "name": "assets",
        "installMode": "lazy",
        "updateMode": "prefetch",
        "resources": {
            "files": [
                "/assets/**"
            ]
        }
    }]   

} }

my app.module.ts uses 我的app.module.ts使用

  ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })

am using vanilla environment.ts and environment.prod.ts 我正在使用vanilla environment.ts和environment.prod.ts

main.ts main.ts

import './polyfills.ts';
import {enableProdMode} from '@angular/core';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';

import {AppModule} from './app/app.module';
import {environment} from './environments/environment';

if (environment.production) {
enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
.then(() => {
if ('serviceWorker' in navigator) {
    navigator.serviceWorker.register('ngsw-worker.js');
}

})
.catch(err => console.log(err));

Not sure what else to check 不知道还有什么要检查

Do you add serviceWorker property to apps first entry in .angular-cli.json ? 您是否将serviceWorker属性添加到.angular-cli.json apps首次输入? If not, angular-cli not built it. 如果没有,angular-cli没有构建它。

"apps": [
  {
    ...
    "serviceWorker": true
  }
}

EDIT As of December 2018, .angular-cli.json is now angular.json , and the format for the serviceWorker is now: 编辑截至2018年12月, .angular-cli.json现在是angular.json ,现在是serviceWorker的格式:

{
    ...
    "projects": {
         "my-project-name": {
             "architect": {
                 "build": {
                     "configurations": {
                        "production": {
                           "serviceWorker": true
                        }
                     }
                 }
             }
         }
    }

You'll need to specify serviceWorker: true for every configuration where you want the service worker. 您需要为您希望服务工作者的每个配置指定serviceWorker:true。

I had the same problem. 我有同样的问题。 It turns out I was using a old package 事实证明我使用的是旧包装

"@angular-devkit/build-angular": "~0.6.6"

This should be 这应该是

"@angular-devkit/build-angular": "~0.10.0",

after this, the service worker is copied 在此之后,复制服务工作者

Also make sure to be using latest Angular 7 and lateset Angular CLI 还要确保使用最新的Angular 7和Lateset Angular CLI

After a long search, we found out that the missing --prod flag in our build makes the problem. 经过长时间的搜索,我们发现构建中缺少的--prod标志会产生问题。 Exactly this one produces the service-worker files and no additional build flag is able to do it (same with uglifying and the uglifyer was the reason why we had exchanged the --build flag against --env=build earlier). 正是这个产生了服务工作者文件,并且没有额外的构建标志能够做到(与uglifying相同,而uglifyer是我们之前交换--build标志--env = build之前的原因)。 More information about what happens on the --build flag to be found here: Angular 2 with CLI - build for production . 有关在此处找到的--build标志上发生的事情的更多信息: 带有CLI的Angular 2 - 用于生产的构建

It appears my app.module.ts was missing a space 看来我的app.module.ts缺少一个空格

I had: environment.production ? 我有:environment.production? ServiceWorkerModule.register('/ngsw-worker.js') :[] ServiceWorkerModule.register('/ ngsw-worker.js'):[]

changed to: environment.production ? 改为:environment.production? ServiceWorkerModule.register('/ngsw-worker.js') : [] ServiceWorkerModule.register('/ ngsw-worker.js'):[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 6 Service Worker拒绝缓存资产,因为ngsw.json无法正确获取 - Angular 6 Service worker refuses to cache assets as the ngsw.json is not fetched correctly Angular:在哪里可以找到 ngsw.json? - Angular: Where to find ngsw.json? Angular Service Worker ngsw-config.json S3/Cloudflare 图像缓存 CORS 错误 - Angular Service Worker ngsw-config.json S3/Cloudflare Image Caching CORS Error Angular 5 和 Service Worker:如何从 ngsw-config.json 中排除特定路径 - Angular 5 and Service Worker: How to exclude a particular path from ngsw-config.json 我的angular 7 PWA在离线时找不到文件'ngsw.json' - my angular 7 PWA can't found file 'ngsw.json' when is offline Angular 7 Service Worker设置 - ngsw-worker.js注册错误 - Angular 7 Service Worker setup - ngsw-worker.js registration error Angular 6 更新后,ng build --prod 产生“错误:预期找到一个 ngsw-config.json 配置文件” - After Angular 6 update, ng build --prod yields “Error: Expected to find an ngsw-config.json configuration file” Angular 7:ngsw-worker.js无法加载 - Angular 7: ngsw-worker.js failed to load 覆盖@angular/pwa ngsw-worker.js - Overriding the @angular/pwa ngsw-worker.js Angular Servicer Worker 错误,Driver.onPush 出现意外标记(ngsw-worker.js:2023) - Angular Servicer Worker error , Unexpected tokenat Driver.onPush (ngsw-worker.js:2023)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM