0

My website serves an app_offline.htm file when it is down for maintenance. I want to force clients to reload when they detect that this page is being served. This way my users see an app_offline page immediately instead of having to refresh.

I figured out how to send a request to the server in JavaScript...

const opts = { method: 'GET', headers: {} };
fetch('/', opts)
  .then(function (response) {
    console.log('response from server:', response);
  });

Output:

Output of console.log from Chrome DevTools

I don't know how to check the response to see if it is my app_offline page. I tried this:

if (response.statusCode === 503)
  window.location.href = window.location.href; // Reload the page

The problem with this approach is that I'm not sure if 503 error necessarily means that my app_offline page is being served. If the website is inaccessible for any other reason, then I want the user stay on the page.

If I could set the statusText on the response, then I could check that. I couldn't figure out how to do it on a static page.

Rainbolt
  • 3,542
  • 1
  • 20
  • 44

1 Answers1

0

You just need to listen for an error after any fetch request you do.

fetch('/', opts)
  .then(function (response) {
    console.log('response from server:', response);
}).catch(err => {
  reloadPage();
});

let reloadPage= () => {
   window.location.href = window.location.href; // Reload the page
}
Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35
  • According to [the docs](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) a fetch error means the fetch itself failed, not that the response had an error code. In other words, if the client lost internet connectivity, fetch would error. Also, even if it did indicate an error response from the server, that's not what I'm looking for. I only want to reload if app_offline is served. – Rainbolt Jun 25 '20 at 17:27
  • @Rainbolt Do you have modification access to the server? – Ahmed Hammad Jun 25 '20 at 17:31
  • Yes, I am admin on the server. – Rainbolt Jun 25 '20 at 17:35
  • Then you can of coarse write a status text that can be checked by the client regardless of the status code. Also, you can make sure that the status code sent when serving app_offline is unique among all the responses you sent, so that you're sure it is what you think it is. – Ahmed Hammad Jun 25 '20 at 17:58
  • App_offline is a static page that IIS automatically detects and serves. Based on the answers to [this question](https://stackoverflow.com/q/5081187/3224483), I don't think I can control the status code that IIS uses when serving it. I thought of a hacky solution just now - I can put `` in my app_offline page, and then check for the presence of `#APPOFFLINE#` in the response from the server. – Rainbolt Jun 25 '20 at 18:50