我正在使用Angular 12,并使用Service Worker部署新版本。似乎每次我将新版本部署到生产环境(但不知何故在暂存环境中不会出现此问题),一些用户会收到大量错误,如下所示:
Uncaught (in promise): ChunkLoadError: 加载块XXX失败。
虽然有很多关于这个问题的帖子,但我认为解决方案是因情况而异的。
与暂存环境相比,我的生产环境只有以下区别:
我调用 enableProdMode();
我的Service Worker设置如下:
Uncaught (in promise): ChunkLoadError: 加载块XXX失败。
虽然有很多关于这个问题的帖子,但我认为解决方案是因情况而异的。
与暂存环境相比,我的生产环境只有以下区别:
我调用 enableProdMode();
我的Service Worker设置如下:
ServiceWorkerModule.register('ngsw-worker.js', {
enabled:
environment.env === Environment.PRODUCTION || environment.env === Environment.STAGING,
registrationStrategy: 'registerWhenStable:30000',
}),
我的配置看起来像这样
{
"$schema": "../../node_modules/@angular/service-worker/config/schema.json",
"index": "/index.html",
"assetGroups": [
{
"name": "app",
"installMode": "prefetch",
"resources": {
"files": ["/favicon.ico", "/index.html", "/manifest.webmanifest", "/*.css", "/*.js"]
}
},
{
"name": "assets",
"installMode": "lazy",
"updateMode": "lazy",
"resources": {
"files": ["/assets/**", "/*.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)"]
}
}
]
}
这是我展示更新的方式(基本上是一个可以忽略的横幅):
@Component({
selector: 'app-live-update-notifier',
templateUrl: './live-update-notifier.component.html',
styleUrls: ['./live-update-notifier.component.scss'],
})
export class LiveUpdateNotifierComponent implements OnDestroy {
hasUpdate = false;
isIgnored = false;
private subscription = new SubSink();
constructor(
@Inject(DOCUMENT) private document: Document,
private swUpdate: SwUpdate,
private appRef: ApplicationRef,
private changeDetectorRef: ChangeDetectorRef
) {
const appIsStable$ = this.appRef.isStable.pipe(first((isStable) => isStable === true));
const everySixHours$ = interval(6 * 60 * 60 * 1000);
const everySixHoursOnceAppIsStable$ = concat(appIsStable$, everySixHours$);
if (swUpdate.isEnabled) {
this.subscription.add(
everySixHoursOnceAppIsStable$.subscribe(() => swUpdate.checkForUpdate()),
this.swUpdate.available.subscribe((evt) => {
if (evt.type === 'UPDATE_AVAILABLE') {
this.hasUpdate = true;
this.changeDetectorRef.detectChanges();
}
})
);
}
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
updateApp() {
this.document.location.reload();
}
skipNotification() {
this.isIgnored = true;
}
}
如果您有任何能够引导解决方案的线索,谢谢。
编辑:
我的应用程序中有许多延迟加载的模块。我认为问题的原因是某些模块在新版本之前没有加载,并且当用户在部署后导航到它们时,它们会出现这个“chunk”问题。但是我尝试通过以下方式来模拟此问题:
- 编辑模块A和B
- 部署
- 导航到模块A
- 编辑模块A和B
- 部署
- 从旧A导航到旧B,因此没有更新APP的版本 RES:旧B仍然正常加载
因此我不认为这是问题所在。
"installMode": "prefetch",
而不是硬删除缓存文件。 - Bobby