0


I have webapp that uses a service worker that detects, when the network is offline and serves an offline page.
If I just load the main page and the network is offline, then the offline page, is served and rendered, as expected.

But I have problem to handle offline situation in the middle of the program, during a fetch request to the network that should return result data in json format.
In such case, the service worker returns the offline.html page, instead of the json data.
But the function that called the network fetch, expects result data in json format, throws an error of invalid data, and nothing is renderred (because the program doesn't know that it needs to load a new page)

How should I update the calling code to:

  • check the result
  • detect that instead of json data, the response was teh offline.html page, and
  • render the offline page instead of continuing the regular flow?

Thanks,
Avner

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

1 Answers1

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