How can I prevent Caching of JavaScript and CSS files ONLY when I deploy a new application?

  • Thread starter Thread starter mark4asp
  • Start date Start date
M

mark4asp

How can I prevent Caching of JavaScript and CSS files ONLY when I
deploy a new application? I only want to force a refresh the first
time the client uses the new build.

For instance, I'm told I can do it with javascript by including a
version number as a querystring like this:
<script type="text/javascript" src="../javascript/menu.js?v=2"></
script>

However that won't solve the problem of the css files.
 
How can I prevent Caching of JavaScript and CSS files ONLY when I
deploy a new application? I only want to force a refresh the first
time the client uses the new build.

For instance, I'm told I can do it with javascript by including a
version number as a querystring like this:
<script type="text/javascript" src="../javascript/menu.js?v=2"></
script>

However that won't solve the problem of the css files.

If the file has changed, why would it used the cached copy?
 
If the file has changed, why would it used the cached copy?

If a client or proxy has the "menu.js" cached then it will continue to
use
it until (a) the headers in the menu.js change (which can only be done
in
IIS as I understand it) or (b) the user refreshes the browser (e.g.
with an F5)

- this will make the page look like a dogs dinner (or worse) if I
change that
file when deploying a new application - that's a huge problem. I want
to
maximise the client caching of files whist ensuring that cached files
are
always updated on becoming obsolete.
 
the easiest is to change the name or folder, by appending the version.
cleaner than fake query strings.

-- bruce (sqlwork.com)
 
the easiest is to change the name or folder, by appending the version.
cleaner than fake query strings.

-- bruce (sqlwork.com)

Is there anything wrong with adding a querystring to the .css and .js
files?
It seems to me far easier than the other suggested methods: a) if I
append a querystring to the folder name, the client end up with
potentially lots of folders containing my obsolete code and it's also
more inconvenient for me. If I append a querystring to the filename
likewise then client still has the obsolete files cached.
 
the easiest is to change the name or folder, by appending the version.
cleaner than fake query strings.

-- bruce (sqlwork.com)

I'm curious -- I don't see the drawback to the quasi query strings. If
anything they'd save the clutter of numerous folders for different
deployments
 
I'm curious -- I don't see the drawback to the quasi query strings. If
anything they'd save the clutter of numerous folders for different
deployments

--http://bytes.thinkersroom.com

With the querystring version the only work needed is to add 1 to the
Build version in config.sys each time you deploy the new build.

web.config entry:

<appSettings>
<add key="BuildVersion" value="1"/>
</appSettings>

Global entry:

public static readonly string Build =
(string)ConfigurationManager.AppSettings["BuildVersion"];

Individual page entries:

<link type="text/css" rel="stylesheet" href="../images/menu.css?v=<
%=MyNS.Global.Build %>" />
<script type="text/javascript" src="../javascript/browser.js?v=<
%=MyNS.Global.Build %>"></script>

Seems like the way to go to me. Could anything be simpler than this?
 
I'm curious -- I don't see the drawback to the quasi query strings. If
anything they'd save the clutter of numerous folders for different
deployments

--http://bytes.thinkersroom.com

With the querystring version the only work needed is to add 1 to the
Build version in config.sys each time you deploy the new build.

web.config entry:

<appSettings>
<add key="BuildVersion" value="1"/>
</appSettings>

Global entry:

public static readonly string Build =
(string)ConfigurationManager.AppSettings["BuildVersion"];

Individual page entries:

<link type="text/css" rel="stylesheet" href="../images/menu.css?v=<
%=MyNS.Global.Build %>" />
<script type="text/javascript" src="../javascript/browser.js?v=<
%=MyNS.Global.Build %>"></script>

Seems like the way to go to me. Could anything be simpler than this?
 
Back
Top