0

I use a service worker pattern that returns an offline.html if the in case that the network disconnects.
It works well: the program loads the intial page my_site.html.
Through out the program, the network disconnects, the service worker detects this and loads the offline.html page:
When the service worker returns the offline.html page, the Javascript uses window.location.href = response.url; which reloads the page and changes the address bar to offline.html.

But then, how does the user recover the original html page, once the network re-connects? Naturally, the user will refresh/reload the page. But this operation will keep asking for the last address (offline.html)
But the user expects to see the original page: my_site.html ?

Thanks,
Avner

Avner Moshkovitz
  • 1,138
  • 1
  • 18
  • 35

2 Answers2

1

The navigator.onLine property provides a boolean value if a user is online.

if (navigator.onLine) { // true / false
    // redirect user
}
era-net
  • 387
  • 1
  • 11
  • This is already working and not the problem. My question is what to do in the javascript file, not in the service worker... The reload changes the address bar so when the user refreshes the page, even if network comes back he will be served the offline.html page – Avner Moshkovitz Jun 18 '20 at 17:40
  • Sorry, but i don't understand the problem. Could you show some code? – era-net Jun 18 '20 at 18:24
0

The solution is to replace the page content without using window.location.href = ... which forces to reload the page and change the address bar.
See here

Avner Moshkovitz
  • 1,138
  • 1
  • 18
  • 35