ngsw pwa how to check version update

The solution for “ngsw pwa how to check version update” can be found here. The following code will assist you in solving the problem.

// You will probably need to tell the service worker to check the server for updates, I usually use a service for this:

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());
}
// In your app-component.ts:

constructor(private sw: UpdateService) {
// check the service worker for updates
this.sw.checkForUpdates();
}
// For whatever reason, Angular sometimes does not register the service worker properly. So you can modify `main.ts` :
// Replace:

// platformBrowserDynamic().bootstrapModule(AppModule);
// With:

platformBrowserDynamic().bootstrapModule(AppModule).then(() => {
if (‘serviceWorker’ in navigator && environment.production) {
navigator.serviceWorker.register(‘ngsw-worker.js’);
}
}).catch(err => console.log(err));

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in