Forum Discussion

Cliff_Sockman's avatar
Cliff_Sockman
Icon for Nimbostratus rankNimbostratus
Aug 23, 2019

I'm Trying To Create a "Cache Buster" iRule using a STREAM::expression replace

On deployment day we would like to make use of an iRule that appends an updated variable using the date => 20190823.1 to the end of every CSS or JS resource that we are serving up. The idea behind it is to force pesky browsers to request an updated version from our web servers instead of using the cached client version.

For example:

<link href="/content/css/prettystylesforyou.css" rel="stylesheet">
<script src="/content/js/mallninjastuff.js" type="text/javascript"></script>

Should have its content rewritten as:

<link href="/content/css/prettystylesforyou.css?random=20190823.1" rel="stylesheet">
<script src="/content/js/mallninjastuff.js?random=20190823.1" type="text/javascript"></script>

We currently have an iRule that is doing this "just fine":

...
append stream_expression " @\[.\]css\"@\.css\?random\=$RandomNumber\"@"
append stream_expression " @\[.\]js\"@\.js\?random\=$RandomNumber\"@"
append stream_expression " @\[.\]css'@\.css\?random\=$RandomNumber'@"
append stream_expression " @\[.\]js'@\.js\?random\=$RandomNumber'@"
...
STREAM::expression $stream_expression
STREAM::enable

There are multiple pitfalls with this logic, one of them being that it will replace any .css" pattern in the content of the HTTP_RESPONSE. I only want it to replace the .css" pattern in the context of an href attribute. We have dozens of paths to our CSS an JS, so in the example above /content/ could be /global/. You get the idea.

I was hoping to do something like this:


STREAM::expression {@href=["|'](.*?)css["|']@href=["|'](.*?)css?random\=$RandomNumber["|']@}

But alas it does not work. Does anyone know how I can implement a generic regex replace for href and src attributes for my .CSS and .JS files?

No RepliesBe the first to reply