我很难将angulars服务人员集成到我的应用程序中。我跟着向导走到目前为止。我可以在我的主屏幕上创建一个快捷方式并启动到我的应用程序中。问题是我的应用程序不知何故没有更新。如果我更改一个按钮的名称,构建应用程序并将其放到我的服务器上,应用程序仍然显示旧版本,直到我点击F5 (重新启动应用程序也没有帮助)。
我试着把下面的代码放入我的应用程序的ngOnInot中,但是没有帮助
ngOnInit() {
if (this._SwUpdate.isEnabled) {
setInterval( () => {
this._SwUpdate.checkForUpdate().then(() => console.log('checking for updates'));
}, this.updateInterval);
this._SwUpdate.available.subscribe(() => {
console.log('update found');
this._SwUpdate.activateUpdate().then(() => {
console.log('updated');
window.location.reload();
});
});
}}
这个应用程序正在我的apache2 linux机器上运行。我的apache缓存有什么作用,或者为什么我的应用程序没有意识到有一个新版本?
谢谢你的帮助:)
编辑:
我的ngsw-config.json
{
"index": "/index.html",
"assetGroups": [{
"name": "roomPlan",
"installMode": "prefetch",
"resources": {
"files": [
"/index.html",
"/*.css",
"/*.js"
]
}
}, {
"name": "assets",
"installMode": "lazy",
"updateMode": "prefetch",
"resources": {
"files": [
"/assets/**"
]
}
}]
}编辑2:
如果我使用"http-server“在本地运行应用程序,它就能工作,但是当我将文件复制到我的apache时,它不会检测到更新。在“网络”选项卡中,我可以看到间隔工作,应用程序每3秒从服务器获得一个新的"ngsw.json“。如果我更新我的应用程序,我可以看到在响应"ngsw.json“中有新的哈希值。之后,浏览器从我的服务器加载新的"index.html“和"main.***.js”,但是应用程序没有应用新版本。根据我的代码,应该说“找到了更新”,但什么也没有发生。
发布于 2018-06-21 12:54:00
您可能需要告诉服务工作人员检查服务器的更新,我通常使用服务:
export class UpdateService {
constructor(public updates: SwUpdate) {
if (updates.isEnabled) {
interval(6 * 60 * 60).subscribe(() => updates.checkForUpdate()
.then(() => console.log('checking for updates')));
}
}
public checkForUpdates(): void {
this.updates.available.subscribe(event => this.promptUser());
}
private promptUser(): void {
console.log('updating to new version');
this.updates.activateUpdate().then(() => document.location.reload());
}在你的app-component.ts里
constructor(private sw: UpdateService) {
// check the service worker for updates
this.sw.checkForUpdates();
}无论出于什么原因,角质有时不能正确登记服务人员。因此,您可以修改main.ts:
取代:
platformBrowserDynamic().bootstrapModule(AppModule);通过以下方式:
platformBrowserDynamic().bootstrapModule(AppModule).then(() => {
if ('serviceWorker' in navigator && environment.production) {
navigator.serviceWorker.register('ngsw-worker.js');
}
}).catch(err => console.log(err));发布于 2020-08-30 06:02:34
我在角度文档中找到了以下信息:https://angular.io/guide/service-worker-devops#debugging-the-angular-service-worker
摘要:角站点上有一个显示服务工作人员状态的有用端点:
http://site-url/ngsw/state将/ngsw/state附加到您的站点地址。如果服务有问题,它应该在那里显示出来。
发布于 2019-06-22 06:52:04
在堆栈上的所有其他解决方案都不起作用之后,我已经开始调试angular的ngsw-worker.js脚本。我跟踪了更新逻辑,并在"PrefetchAssetGroup“方法中结束。每次调用方法时,我都会插入日志记录,然后我意识到它一直在尝试缓存我的favicon.ico。我已经检查了favicon.ico在我的ngsw.json中的位置,并意识到偏袒不在那里。一旦我把图标放在那个位置,一切就开始正常工作了。
https://stackoverflow.com/questions/50968902
复制相似问题