How can I force clients to refresh JavaScript files?

后端 未结 26 2205
旧时难觅i
旧时难觅i 2020-11-22 03:40

We are currently working in a private beta and so are still in the process of making fairly rapid changes, although obviously as usage is starting to ramp up, we will be slo

相关标签:
26条回答
  • 2020-11-22 04:14

    In asp.net mvc you can use @DateTime.UtcNow.ToString() for js file version number. Version number auto change with date and you force clients browser to refresh automatically js file. I using this method and this is work well.

    <script src="~/JsFilePath/JsFile.js?v=@DateTime.UtcNow.ToString()"></script>
    
    0 讨论(0)
  • 2020-11-22 04:14

    One solution is to append a query string with a timestamp in it to the URL when fetching the resource. This takes advantage of the fact that a browser will not cache resources fetched from URLs with query strings in them.

    You probably don't want the browser not to cache these resources at all though; it's more likely that you want them cached, but you want the browser to fetch a new version of the file when it is made available.

    The most common solution seems to be to embed a timestamp or revision number in the file name itself. This is a little more work, because your code needs to be modified to request the correct files, but it means that, e.g. version 7 of your snazzy_javascript_file.js (i.e. snazzy_javascript_file_7.js) is cached on the browser until you release version 8, and then your code changes to fetch snazzy_javascript_file_8.js instead.

    0 讨论(0)
  • 2020-11-22 04:15

    Not all browsers cache files with '?' in it. What I did to make sure it was cached as much as possible, I included the version in the filename.

    So instead of stuff.js?123, I did stuff_123.js

    I used mod_redirect(I think) in apache to to have stuff_*.js to go stuff.js

    0 讨论(0)
  • 2020-11-22 04:17

    The advantage of using a file.js?V=1 over a fileV1.js is that you do not need to store multiple versions of the JavaScript files on the server.

    The trouble I see with file.js?V=1 is that you may have dependant code in another JavaScript file that breaks when using the new version of the library utilities.

    For the sake of backwards compatibility, I think it is much better to use jQuery.1.3.js for your new pages and let existing pages use jQuery.1.1.js, until you are ready to upgrade the older pages, if necessary.

    0 讨论(0)
  • 2020-11-22 04:17

    One simple way. Edit htaccess

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} \.(jpe?g|bmp|png|gif|css|js|mp3|ogg)$ [NC]
    RewriteCond %{QUERY_STRING} !^(.+?&v33|)v=33[^&]*(?:&(.*)|)$ [NC]
    RewriteRule ^ %{REQUEST_URI}?v=33 [R=301,L]
    
    0 讨论(0)
  • 2020-11-22 04:19

    Use a version GET variable to prevent browser caching.

    Appending ?v=AUTO_INCREMENT_VERSION to the end of your url prevents browser caching - avoiding any and all cached scripts.

    0 讨论(0)
提交回复
热议问题