zchpit zchpit - 2 months ago 12
Javascript Question

Javascript src with question mark

I have a question. In old project that I'm currently working on I have found this code:

<script type="text/javascript" language="Javascript" src='<%= Page.ResolveUrl("~/javascripts/CardConnectorManager.js?2016071203")%>'></script>


I have:


  • /javascripts/CardConnectorManager.js



but don't have


  • /javascripts/CardConnectorManager.js?2016071203



What that question mark is doing and why anybody write such thing?
Maybe this file exists only on server on some bulid stuff thing?

Answer

David R's answer is pretty good, but I want to add a little bit info:

Usually there are two approaches for cache-breaking:

  1. Rename file;
  2. Add some hash to the end of the file.

The first approach may be better for some cases (see this question), but can be more painful. How would you keep this file in version control? What if there are many files like this?

The second approach is much easier. You just add something like app.js?_=<some_string>. The <some_string> can be whatever: timestamp, build number or just a random string.

For this approach, you may find it better to use automatic tools like gulp-rev.

Update: Honestly, it would be much better to have a revision number for all statics in the project: html, images, css, js. There a lot of tools to make this automatic.

Alternatively, there are some technics, for example angular developers have the $templateCache service which allows the developer to put all the project's html (excluding index.html) in a single js file.

Comments