Forum Discussion

VIN_Support_890's avatar
VIN_Support_890
Icon for Nimbostratus rankNimbostratus
Sep 18, 2015

Looking to force a JavaScript refresh through iRule

Admitted newbie here, so feel free to "explain it to me like I am 5".

Instead of adding a random querystring name/value pair to our JS files in our web pages to force a refresh from the web server, we were hoping to trigger a refresh of a specific JavaScript file using an iRule.

Does anyone have any experience with this simple technique? I was thinking something along the lines of this:

when HTTP_REQUEST {

set uri [string tolower [HTTP::uri]]

switch -glob $uri {

    "/global/js/alertnotificationmanager.js"

    {

        HTTP::uri "/global/js/alertnotificationmanager.js?rand=20150918"

    }

}   

}

The concept being that if a request for /global/js/alertnotificationmanager.js comes through, the iRule will detect it, add a random number to the end of the URI and then pass that along to the web server. I don't think this is actually working though because the browser still thinks it is requesting /global/js/alertnotificationmanager.js.

Any suggestions?

5 Replies

  • Does the browser caching this file? If yes, I think you will not see that request. Sorry for my quick view, but I did not see anything wrong with code. I think rather that the server will receive the new uri. Do you intend to force the browser to update this JS on each request?
  • Hi cjunior, Yup, I used Fiddler to determine that the JS file is served up from cache for the web page. The server does get the new URI request (confirmed in the IIS logs), but the browser still serves up the cached version. I also tried an HTTP:redirect with the same results. So, do you know how to force the browser to update the JS from an iRule? Thanks!
  • Ow, I think yes, you can replace the original src content in the HTTP_RESPONSE event. see below.
  • First, you need to set up a stream profile in the VS, then, set up this iRule:

     

    when HTTP_REQUEST {
        STREAM::disable
        HTTP::header remove "Accept-Encoding"
    }
    
    when HTTP_RESPONSE {
        if { [HTTP::header "Content-Type"] contains "text" } {
            This code will create a random number and append query string to the original source file.
            STREAM::expression "@global/js/alertnotificationmanager.js@global/js/alertnotificationmanager.js?rand=[expr rand()]@"
            STREAM::enable
        }
    }

    I think this should work! πŸ™‚ Regards.