Making sure Http-Requests are executed when Angular-PWA is opened

Viewed 210

I need to make an http-request when my Progressive-Web-App is openend. This works fine when the user closes the app. But when it is still in memory and opened (e.g. the next day), than my http-request is not executed.

The Request is placed in ngOnInit().

Is there any lifecycle method that guarantees it is executed when the component comes to the foreground?

1 Answers

In addition to making the requests in the ngOnInit() function, listen for changes to the page visibility, and make the request once the page becomes visible again.

document.addEventListener('visibilitychange', () => {
  if (document.visibilityState === 'visible') {
    console.log('Page visible, make HTTP requests!');
  }
});

The Page Lifecycle API does cover some of this, but there isn't a single event that will fire both on page load, and when the page becomes visible again.

Related