4

I am using the Angularjs Framework, and i update my js controllers a lot, and i want to know how i can force the broweser to get the new/updates js files and not pull them up from cache.

If anyone can help me out that will be great thanks!

JamesDeveloper
  • 93
  • 1
  • 12
  • 3
    Possible duplicate of [Cache busting via params](http://stackoverflow.com/questions/9692665/cache-busting-via-params) – Alon Eitan May 20 '17 at 19:59
  • 1
    If you want to get an un-cached file append a new random string to the end of the filename after a `?` for example: `http://example.com/js/file.js?12312312313` – Get Off My Lawn May 20 '17 at 20:00
  • Heh, I wish I asked about this on SO about 2 years ago. We never found out a clean way to do this and did weird things to check version number and ask the user to clear their cache. – VSO May 20 '17 at 20:03

1 Answers1

2

The easiest way to get an un-cached file is to append a string of sorts to the end of the file. This could be a version number, timestamp or some random string.

PHP:

<?php
echo '<script src="/js/file.js?t=' . time() . '"></script>';

JavaScript:

<script>
    document.head.appendChild(
        document.createElement('script')
    ).src='/js/file.js?t=' + (new Date().getTime());
<script>

For an uncached file every time, change the string on every request.

For an uncached file occasionally, use something like a version number, or an md5 version number.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338