Search code examples
refreshprogressive-web-apps

PWA: how to refresh content every time the app is opened


I created a PWA app which sends API call to my domotic server and prints the response on the home page (e.g. outside temperature and vacuum robot status).

While all the data get refreshed at very first app opening, if I minimize the app whithout completely shutting it off I have no data refreshing at all.

I was wondering how to force a refresh every time the app gets re-opened without having to do it manually (no pull-down to refresh, no refresh-button).


Solution

  • Here is the solution that works.

    You can place this code wherever you have access to the window object:

        window.addEventListener("visibilitychange", function () {
          console.log("Visibility changed");
          if (document.visibilityState === "visible") {
            console.log("APP resumed");
            window.location.reload();
          }
        });
    
    

    Consider this may affect user experience or data loss with a forced reload every time the user swipes between apps.