2

I have created style and script bundles in my MVC project as given below. Now I need to enable caching for bundles as well. Is there any way by which we can control caching duration for these bundles?

bundles.Add(new StyleBundle("~/bundles/css").Include(
  "~/lib/bootstrap/css/bootstrap.min.css",                                                        
  "~/lib/owlcarousel/owl.carousel.min.css",
  "~/lib/owlcarousel/owl.theme.default.min.css",
  "~/StylesCdn/google-font.css",
  "~/assests/css/common.min.css"
));

bundles.Add(new StyleBundle("~/bundles/bottomcss").Include(                            
  "~/lib/chartist/scss/chartist.min.css",
  "~/lib/chartist/chartist-plugin-tooltip.min.css",                            
  "~/StylesCdn/export.min.css"
));

bundles.Add(new ScriptBundle("~/bundles/js").Include(
  "~/lib/bootstrap/js/jquery.min.js",
  "~/assests/js/homelayout.min.js"
));

bundles.Add(new ScriptBundle("~/bundles/bottomjs").Include(
  "~/lib/bootstrap/js/bootstrap.min.js",
  "~/lib/owlcarousel/owl.carousel.min.js",
  "~/assests/js/app.min.js",
  "~/Content/Validation.min.js",
  "~/assests/js/InvestmentCalculator.min.js",
  "~/assests/js/common.min.js",
  "~/assests/js/BindDate.min.js"               
));
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • If you inspect requests your browser is making, you will see they are responded with `304 Not modified`. – GSerg Nov 02 '18 at 09:00
  • 1
    Have you read this question? https://stackoverflow.com/questions/28275725/how-can-we-enable-caching-for-bundles-in-mvc5 – Syntax Error Nov 02 '18 at 09:00
  • actually, i am not aware with caching for bundle so, let me give a proper solution to what to do and how to do it. – Mukesh Rajpoot Nov 02 '18 at 09:06
  • yes i read that question but i didn't found my solution yet.. @SyntaxError – Mukesh Rajpoot Nov 02 '18 at 09:09
  • 1
    Not sure what you mean by 'found the solution' as bundles are cached by default. Your code already does what you ask. – Rory McCrossan Nov 02 '18 at 09:15
  • I mean to say that i have created bundles but when i reload the page it will renders every time of page load and take time to load page every time. please let me know how do i versioning for bundle. for that whenever the time of page load it will renders for the first time not every time. – Mukesh Rajpoot Nov 02 '18 at 09:25
  • Bundles are automatically minified and cached for 12 months in release mode - refer [documentation](https://learn.microsoft.com/en-us/aspnet/mvc/overview/performance/bundling-and-minification) –  Nov 02 '18 at 09:39

3 Answers3

2

OK, so the other answers do not seem to be entirely correct. You can influence how browsers attempt to cache these files by altering the 'staticContent' element in your web.config. (Providing you are on IIS7 or later)

This affects what caching headers static content (e.g. images/scripts) will be served with. e.g. you can cause a max-age header and date to be served by setting the following:

<staticContent>
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00" />
</staticContent>

Further reading as to what can be set can be found in the documentation here:

https://learn.microsoft.com/en-us/iis/configuration/system.webserver/staticcontent/clientcache

Paddy
  • 33,309
  • 15
  • 79
  • 114
0

As was said already, the bundle source will be cached on the server and in the browser in most -if not all- cases (but it will depend on the actual server software that you use).

"but when i reload the page it will render every time"

Bundles themselves don't render, they are parsed (they are JS / CSS resources). After that, the parsed result is used for rendering the page.

Whether the JS / CSS parsing step is done efficiently (with or without "caching" of parse results) is upto the browser implementation, and impossible to influence by means of server side programming, scripting or settings.

Peter B
  • 22,460
  • 5
  • 32
  • 69
-2

Bundles are cached by browser by default unless you have disabled it manually. There is nothing you can do on server end.

Sunny Verma
  • 107
  • 4