0

I would like to make sure users are getting the latest and greatest Javascript and CSS files. Is there a way to add a query string to the app.js to force the browser to pull down the latest version?

It is simple to automate the query string using a gulp task ie, compile my .js files into

app.js?v=10012015

But how do I then get this same query value into my index.html without having to manually add it each time I want to update the .js going out to users?

arahansen
  • 87
  • 1
  • 3
  • 11
  • You don't really need to specify a version. just randomize the query and it will force the browser to download an uncached version. The browser asks itself "do I know the file called app.js?v=whatever" if not, it will download the file again. – Michael Seltenreich Sep 02 '15 at 00:16
  • So if I have a file called app.js and I include a script with src="app.js?v=random" then the js file will be downloaded every time? – arahansen Sep 02 '15 at 00:24
  • 1
    Well that would disable performance benefits from caching the rest of the time. I want to force the browser to re download js when I have actually updated the js file – arahansen Sep 02 '15 at 00:27
  • I don't know how big your file is but it shouldn't be a problem. If you only want to download a new version whenever a new version is available, you will have to manually set the query whenever you change versions... – Michael Seltenreich Sep 02 '15 at 00:28
  • See, the browser doesn't know what version you have hosted on the server, it only knows if it knows the file name or not... – Michael Seltenreich Sep 02 '15 at 00:29
  • Consider minifying your file if you are worried about performance. – Michael Seltenreich Sep 02 '15 at 00:30
  • Also check this out: http://stackoverflow.com/questions/32414/how-can-i-force-clients-to-refresh-javascript-files – Michael Seltenreich Sep 02 '15 at 00:32

1 Answers1

0

I ended up using a gulp task called gulp-replace to look through my index.html and replace app.js with app.js?v=[TODAY'S DATETIME] so I get a unique query string every time I build my app. And this query string forces the browser to download the latest js file when I push a new build.

arahansen
  • 87
  • 1
  • 3
  • 11