[英]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.