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

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.
 
L

Larry Bud

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?
 
M

mark4asp

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.
 
B

bruce barker

the easiest is to change the name or folder, by appending the version.
cleaner than fake query strings.

-- bruce (sqlwork.com)
 
M

mark4asp

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.
 
R

Rad [Visual C# MVP]

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
 
M

mark4asp

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?
 
M

mark4asp

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top