0

I was facing the issue of javascript being cached on users' browsers and not getting updated when changes went to production. Because the site uses multiple embedded iframes, just using f5 wasn't sufficient. The solution I went with is to load a script first that gets all the modified dates for the js files I need and then appends those dates to the js file in each page. Each page looks something like this:

    <script type="text/javascript" src="get_dates"></script>//returns the dates for each file in array
    <script type="text/javascript">
        document.writeln('<script type="text/javascript" src="' + some_path + '?Mod=' + date_array[pos] + '"></script>...repeat for all scripts
    </script>

The questions I have are:

  1. How do you tell if the scripts are getting cached until the mod date changes? (I still want them to be cached, just not if they're updated)
  2. How will the caching of the html page affect this?
  3. Does using document.writeln(() affect caching? I.e. will they still be cached if you write the same exact script?
xan
  • 7,440
  • 8
  • 43
  • 65
perry_VW
  • 145
  • 3
  • 14

3 Answers3

0
Joseph
  • 117,725
  • 30
  • 181
  • 234
0

Apply versioning to your js files in every production release. Let say

Release1:

<script src="source.js?v1.0"></script>

Release2:

<script src="source.js?v2.0"></script>

and so on...

This way you still allowing clients to cache your js files but on a particular version.

jerjer
  • 8,694
  • 30
  • 36
  • This would be ideal, but that would require updating the version number manually as the company isn't using version control. I just figured it was easier to write the script once to get the date the file was modified and call that every time we need the script – perry_VW Mar 22 '13 at 18:10
  • IMO, this does not need any version control, you just need to add extra get parameter. – jerjer Mar 25 '13 at 10:53
  • wouldn't that require updating all the get parameters every time we introduce a new version? – perry_VW Mar 26 '13 at 15:41
0

How do you tell if the scripts are getting cached until the mod date changes?

Just set the expiration to infinity (or, say, 1 year - that's long enough). Everytime your Mod parameter changes, the new file will be requested.

How will the caching of the html page affect this?

Not at all. The caching of the get_dates script will, though.

Does using document.writeln(() affect caching? I.e. will they still be cached if you write the same exact script?

No, it does not affect anything. The result - a <script> node referencing an external script will be the same.

Also read on How to force browser to reload cached CSS/JS files?

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I was concerned about the get_dates script being cached and messing up the whole process. Since the mod dates are retrieved from the server, does it matter if that script gets cached? Even if it's cached it will still execute the call to the server and not cache that returned data correct? – perry_VW Mar 22 '13 at 16:07
  • How are they "*retrieved from the server*"? I guessed they were included dynamically in that `get_dates` script – Bergi Mar 23 '13 at 13:32
  • the get_dates script makes an ajax call that returns the mod dates for all the scripts that that html page will use – perry_VW Mar 26 '13 at 15:40
  • OK, then it depends on the caching of the ajax response, not on the `get_dates` script. – Bergi Mar 26 '13 at 15:48