Balaji
Balaji

Reputation: 2189

JavaScript file changes deployment

We are having MVC web application. Some of the code is in JavaScript. When we deploy any changes to the JavaScript, the changes are not reflected on the client side. We have to ask clients to do CTRL+F5 to get the changes. Is there a standard way of pushing JavaScript changes to the client side?

Upvotes: 0

Views: 467

Answers (3)

Pekka
Pekka

Reputation: 449525

You're not saying what server-side language your site is in. A nifty trick from the Rails world is to check the "last modified" time of the file, and to add that as a GET parameter to the URL.

In PHP, it would look like this:

<script type="text/javascript" 
        src="script.js?time=<?php echo filemtime("script.js");">

(of course, the path you need to give in the filemtime call will probably need to be an absolute one.)

Upvotes: 0

you can try this meta tags

<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />

Upvotes: 0

Nick Craver
Nick Craver

Reputation: 630469

You can refer to the files with a version, like this:

<script type="text/javascript" src="myScript.js?v=12345"></script>

The number after v represents your build number, so when a new build pushes, they grab the files again. View source on this page to see the same behavior :) This gives you the benefit of allowing the user to cache the files as long as possible (forever), yet still have them automatically grab any update.

Upvotes: 2

Related Questions