Advertisement:

Author Topic: Bender main.css not being cached (since included with random "?#####" string)  (Read 1145 times)

marylooo

  • Newbie
  • *
  • Posts: 16
The Bender theme (in OC 3.2.1) contains the following code to include the main.css file in the page head section:
Code: [Select]
<link href="<?php echo osc_current_web_theme_url('css/main.css') ; ?>?<?php echo rand(0pow(105)); ?>" rel="stylesheet" type="text/css" />
When the page loads, the code that this generates looks like this:
Code: [Select]
<link href="http://www.example.com/oc-content/themes/bender/css/main.css?35947" rel="stylesheet" type="text/css" />
And the next time the page loads, it looks like this (notice the number keeps changing):
Code: [Select]
<link href="http://www.example.com/oc-content/themes/bender/css/main.css?47285" rel="stylesheet" type="text/css" />
Web browsers will see those as 2 different files, so they will re-download it a second time (and a third, fourth, fifth, etc. for the third, fourth, fifth, etc. page a visitors goes to).

Is this an oversight, or by design? The advantage is visitors always get the latest CSS. The disadvantage is the user has to download more data from the server, both requiring server resources and making the user wait longer.

One solution is to compute that number based upon the contents, or timestamp, of the CSS file itself; that way, it's a new number whenever the CSS file's contents change, but it remains at that number from that point on until the CSS file's contents change again. Another solution is to have a textbox in the admin panel, called "string to append to css files", where we can update the value manually whenever we modify our CSS code, and then the PHP code that generates the page can retrieve the value of that option when generating the page, and append it to the CSS file's filename.

More info: http://stackoverflow.com/questions/13976975/refresh-css-file-in-cache-with-version-xxx
« Last Edit: October 07, 2013, 12:21:39 am by marylooo »