1

I have faced a lot of issues with CSS and JS caching, so i decided to add timestamps to CSS and JS files.

so i added this param at the end of links:

?t=<%= DateTime.Now.Ticks %>

So here how it looks like:

JS:

<script type="text/javascript" src="Scripts/global_scripts.js?t=<%= DateTime.Now.Ticks %>"></script>

CSS:

<link rel="stylesheet" type="text/css" href="Styles/mysite.css?t=<%= DateTime.Now.Ticks %>" />

The JS file work fine. But for CSS files this param ?t=<%= DateTime.Now.Ticks %> gets recognized as a string. Why? I cannot find the difference

Developer
  • 4,158
  • 5
  • 34
  • 66

3 Answers3

2

While this is not an answer to your original question, I can suggest you to use a different approach altogether. Instead of adding ticks, you can use default Microsoft libraries to make bundles. When you do it that way, the engine will automatically add a string like ?v=dfsdf9fsdfasd which will be a hash of the content, so every time it changes it will be different. Then you can just cache forever the resources forever on the client.

Your approach is problematic since the browser will download js every time, this is extremely inefficient.

You can get some details on how to use bundles with minification e.g. here. Please see my answer for some details on how to make script bundles properly.

P.S. If you don't want to do bundling yourself, you can still implement the hash of the file content yourself. Another easy option is to do ?v=1, where you get the version from config, then by changing the config, you will be able to force cache update on all you clients.

With all of the approaches you should set cache for 1 year (max if I remember correctly).

Community
  • 1
  • 1
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
  • I have modified my approach, now i add a App version as a parameter, this will tell browsers to reload JS and CSS only when i update version – Developer Feb 10 '16 at 11:08
  • You should put Cache-Control: max-age=31536000 in response, this will make the browser effectively cache the data forever (1 year) until you change the version. And if you did like my answer, please accept it :) – Ilya Chernomordik Feb 10 '16 at 11:42
2

You can easily solve your problem with this simple adjustement

<link rel="stylesheet" type="text/css" href="Styles/uberhint.css?t=<%= "" +DateTime.Now.Ticks %>" />

Just add "" +.

ASP.NET is treating the link as an HtmlLink control, and rendering the contents of the href attribute as a literal. Inserting that Extra string forces ASP.NET to accept that you are trying to generate a dynamic string

Vhortex
  • 381
  • 1
  • 7
  • I am getting the same problem on my url_rewritten css links, Which was solve by padding an extra empty char – Vhortex Feb 10 '16 at 09:55
0

If the above answers dont work, you can try to add a

<asp:placeholder runat="server" id="ph">

above script and css tag, and in the page_load you can bind placeholder.

ph.DataBind();
oneNiceFriend
  • 6,691
  • 7
  • 29
  • 39