0

When deploying our app we use app_offline.htm to tell IIS to make our application unavailable. If there are any clients that have the Angular app loaded already, what I would like to do is intercept any requests to the server while the app is offline and then reload the page so that app_offline.htm displays instead of the Angular app.

I thought it would be as simple as putting <!--Offline--> or something at the top of app_offline.htm and then have an http interceptor that checks if any response starts with that, but I never seem to get the content of app_offline.htm in a response. Amazingly, I'm still getting app content in the responses. I'm wondering if maybe it's pulling a cached version of the content or something. If I manually reload the page, I do get the app_offline.htm page as expected.

So how can I detect if the server is unavailable and reload the page?

UPDATE

So this appears to maybe be more of an IIS configuration issue than anything. The app_offline.htm file appears to only affect resources managed by ASP.NET. From this answer, I learned that you can configure ASP.NET MVC to handle all requests using this following code:

<?xml version="1.0"?>
<configuration>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
    </system.webServer>
</configuration>

This has the effect of preventing static content like HTML from being served even if app_offline.htm exists, but it also appears to continue firing the 'response' handler of the Angular http interceptor with undefined as the response instead of firing the 'responseError' handler with an object having a status of 503 Service Unavailable. I'm hesitant to use this solution because I don't know if there are other conditions that may cause an undefined response, but so far it appears to be working:

function appOfflineHttpInterceptor($window, $q) {
    return {
        'response': function (response) {
            if (typeof response == "undefined")
                $window.location.reload();

            return response || $q.when(response);
        }
    };
}

Does anyone know if there are other conditions that would cause an undefined response - which would render this solution nonviable?

Community
  • 1
  • 1
adam0101
  • 29,096
  • 21
  • 96
  • 174
  • A quick solution would be, if you are using ajax calls(or equal), just check on the client side for an Error response(or a status-flag, in the response) and redirect from the client. with `window.location.href`. for a better solution, you would have to share more information. :-D – winner_joiner May 11 '17 at 14:47
  • @winner_joiner, you'd think, but I'm not getting any error responses. I'm not sure why. – adam0101 May 11 '17 at 14:48
  • If no error response maybe atleast a timeout error. Or does your IIS continue serving to calls? If timeouts don't work, could you give use more information how the system works to give a better solution.(last option with little users could be pooling the server every couple of seconds, but with many users, you could kill the server) – winner_joiner May 11 '17 at 14:54
  • maybe i don't get the question right, but isn't the problem coming from the fact that you want to load `app_offline.htm` when app is made unavailable? If so, the client will still have the angular app on his side, then you can't check for `` because it is in the file you want to load? or did i understand the question upside down? – Kaddath May 11 '17 at 14:57
  • @Kaddath, when you put `app_offline.htm` in the root of your site, IIS should serve that for every request instead of the actual requested content, but now I'm wondering if it only applies to ASP.NET content - which would explain why I'm still seeing my partials come through. I'm testing that now. Maybe this is an IIS configuration issue. I'm checking to see if adding `runAllManagedModulesForAllRequests="true"` will force `app_offline.htm` to be served for all types of content. – adam0101 May 11 '17 at 14:59
  • @adam0101 thanks, sorry didn't want to waste your time, i'm just a curious guy that likes to undestand ^^ this looks like a IIS specific issue indeed.. – Kaddath May 11 '17 at 15:05

1 Answers1

0

Just a guess based on what information you have given so far. Is your server serving app_offline.htm as a normal page. In other words is it sending a HTTP Status Code 200 OK? If so that is why you do not see an error in your ajax calls.

You could serve the page with a 503 Service Unavailable which would trigger error handlers in your ajax requests. Then you could conditionally handle the 503 and redirect the entire loaded app page.

gforce301
  • 2,944
  • 1
  • 19
  • 24