How to remove Kentico default cache-control header values

Kashif Akram asked on September 13, 2021 04:32

Hi Everyone,

I am struggling to remove Kentico's default cache-control header values "cache-control: no-cache, no-store, must-revalidate", doesn't matter what I do these are there for every page.

I've tried removing them in web.config, adding max-age and public values but these values are appended to default values.

In addition expires is always -1 which I want to get of as well.

Any help would be much appreciated.

Thanks and Regards, Kashif

Correct Answer

Juraj Ondrus answered on September 13, 2021 07:59

If you want to change the headers that are set within the request and you want the browsers and intermediate servers (proxies, etc.) to cache the data (and you will have no control about the cache expiration), you can follow the steps below:

The first option would be to modify the portal template page which you can find at /CMSPages/PortalTemplate.aspx which is the source template of any page generated by Kentico.

In the code of the page you will see following control:

<asp:Literal runat="server" ID="tags" EnableViewState="false" />

This control is populated in OnPreRender method via:

   // Init the header tags
    tags.Text = HeaderTags;

Although this can cause a problem with future upgrades and hotfixes as it may get overridden. So, make a proper note in the project documentation. In the debug you can find the code that is filled into the header tags and what can be modified.

Secondly you may clear and create custom response headers:

Response.AddHeader("Cache-Control", "no-cache, must-revalidate");

And lastly would be creating custom headers via custom web part in this way second approach would be contained within a reusable control. Also, there is a KB article on this topic available which is still valid.

0 votesVote for this answer Unmark Correct answer

Recent Answers

Dmitry Bastron answered on September 13, 2021 06:43

Hi Kashif,

  • What Kentico version are you using?
  • Is it MVC or Portal Engine?
  • What requests do you want to remove headers from? All, assets, media files, etc?
  • Do you want to completely remove these headers or set up to some cached values?
0 votesVote for this answer Mark as a Correct answer

Kashif Akram answered on September 13, 2021 06:59

Hi Dmitry,

Thanks for response, sorry I should've provided all this info, I'm using Kentico 12 Portal Engine and want to remove completely cach-control header values on every page.

I am able set cache-control header on resource

![Image Text](C:\Users\kakra\Dropbox (Sydney Uni Student)\Work\hs_orders\kentico_ticket\otc_resource.png)

but for pages it appends my custom headers to kentico defaults.

![Image Text](C:\Users\kakra\Dropbox (Sydney Uni Student)\Work\hs_orders\kentico_ticket\otc_page.png)

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on September 13, 2021 07:09

Sorry, your screenshots are not loading as you reference these files from your local drive.

You should be able to hook into Request End event (refer here) and remove response headers. However, may I ask you, is there any particular reason why do you want to completely remove these headers? This is quite an un-common practice.

0 votesVote for this answer Mark as a Correct answer

Kashif Akram answered on September 13, 2021 07:43

Yes sure Dmitry, the reason is to set expire header which is a requirement of pingdome test to get B ranking, current is C. Our client uses pingdome tests.

May I ask you favor if you can provide me sample code with location in the project, I'd really appreciate that.

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on September 13, 2021 08:11

Ok, this task you are raising here is way more complicated than just setting up cache headers.

First of all, Cache-Control and Expires headers are interchangeable, meaning that if your application uses Cache-Control with max-age - it's just the same. Refer here.

Then, if you just apply Expires header for all your requests it will likely break something on your website, as client's browsers will start caching your pages always, which will break dynamic functionality (depending on your implementation, it's typically login state, forms, etc.). If you are sure that this deep caching will not harm your website, then I'd probably point you to Output Caching topic in Kentico. Setting output cache for pages will also set Cache-Control header with max-age.

However, I also think that most likely you'd need caching headers only for all your assets (images, JS, CSS), but not for HTML. I've never seen any tools like Pingdom or PageSpeed complaining about missing cache headers on HTML responses. Please get to the bottom of this recommendation from Pingdom and analyze which exacts requests it's complaining about.

2 votesVote for this answer Mark as a Correct answer

Kashif Akram answered on September 13, 2021 08:18 (last edited on September 13, 2021 08:18)

Thanks Dmitry,

I've done what you mentioned, we have output cache in place, I am caching assets please inspect

but web page itself is the one takes most time when rendering first time (off course second time is way faster) and running pingdom test fails E-tag and expire header suggestions

you can run same url in pingdom and please suggest what you think is best in this scenario\

Appreciate your response

0 votesVote for this answer Mark as a Correct answer

Dmitry Bastron answered on September 13, 2021 08:46

After a quick check I'd say you don't have any issues with cache headers, these all are just fine. However, what I've seen is a large server response time (TTFB) which was from 350-400ms to 1.5+ seconds. According to Google Lighthouse report.

I'd probably suggest investigating it. Maybe one or a few components on your page are slowing down the load. You can investigate it in debug app.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.