How to prevent browser and proxy caching of web pages

Rate It (5)

Last post 10-18-2007 3:33 PM by Evervision. 29 replies.

Sort Posts:

  • How to prevent browser and proxy caching of web pages

    08-01-2006, 3:20 PM
    • Contributor
      2,221 point Contributor
    • Harold.NET
    • Member since 07-16-2003, 12:54 PM
    • McLean, VA
    • Posts 457


    I ran into the issue of if the user presses the "BACK" button, the page does not refresh. So I placed the meta tag in the header:

    <meta http-equiv="CACHE-CONTROL" content="NO-CACHE" />

    To attempt to resolve the issue. This fixed the issue for IE and other browsers. But firefox gave me a particular problem. They interpret this tag unlike the other browsers, thus not refreshing the page. After a bit of digging, I discovered this code: (Place in the OnInit block, prefereably)

     

     

    Note, this is new code as of 8-31-06

     

                Response.ClearHeaders();
                Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
                Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
                Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
                Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
                Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1 
                Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1 
                Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1 
                Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1 
                Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1 
                Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1 
     

    forces all browsers to grab new copies of the pages when the user pressed the BACK or FORWARD button on their browsers, which is quite annoying.

    GO HOKIES!!!
  • Re: How to prevent browser and proxy caching of web pages

    08-05-2006, 2:56 AM
    • Member
      5 point Member
    • hi_raj91
    • Member since 08-05-2006, 6:36 AM
    • Posts 1

    hi,

              i've implement this code but not proper handle the browser expaire bucouse some page did handle but whne loggout form is handle ,

                                                                 if have a any other solution thne plz send it.

     

  • Re: How to prevent browser and proxy caching of web pages

    08-31-2006, 3:03 PM
    • Contributor
      2,221 point Contributor
    • Harold.NET
    • Member since 07-16-2003, 12:54 PM
    • McLean, VA
    • Posts 457

    I have updated the code, the changes were not persisting the way i desired. New code below:

     

     

                Response.ClearHeaders();
                Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
                Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
                Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
                Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
                Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1 
                Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1 
                Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1 
                Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1 
                Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1 
                Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1 
      
    GO HOKIES!!!
  • Re: How to prevent browser and proxy caching of web pages

    08-31-2006, 9:08 PM
    • Star
      8,743 point Star
    • LudovicoVan
    • Member since 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,752

    Hello, afaik all you need to prevent output caching in asp.net is:

       Response.Cache.SetCacheability(HttpCacheability.NoCache);

    http://msdn2.microsoft.com/en-us/library/system.web.httpcacheability.aspx 

    HTH. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
    Filed under:
  • Re: How to prevent browser and proxy caching of web pages

    08-31-2006, 11:01 PM
    • Contributor
      2,221 point Contributor
    • Harold.NET
    • Member since 07-16-2003, 12:54 PM
    • McLean, VA
    • Posts 457
    That works for IE, but Firefox interprets it all differently. I had a nightmare trying to figure all of this out.
    GO HOKIES!!!
  • Re: How to prevent browser and proxy caching of web pages

    09-01-2006, 12:51 AM
    • Star
      8,743 point Star
    • LudovicoVan
    • Member since 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,752

    I might be wrong, but as far as i know and i've seen, that's just enough.

    Maybe keep in mind that outputs an HTTP/1.1 compliant Cache-Control header, not the deprecated HTTP/1.0 Pragma: NoCache. Also, sending HTTP headers as META tags as you show above is furtherly deprecated and known to be unreliable, so that's maybe where your original problems came from.

    Check the help topic i've linked above for details and a reference to the official W3C documentation.

    Hope i've not misinterpreted. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: How to prevent browser and proxy caching of web pages

    09-01-2006, 2:56 PM
    • Contributor
      2,221 point Contributor
    • Harold.NET
    • Member since 07-16-2003, 12:54 PM
    • McLean, VA
    • Posts 457

    I took a few minutes to retest this out with just setting the page cachability. In firefox it still doesn't prevent caching.

     This is mainly due to the way firefox caches pages in memory (which also leads to it gobbling up memory, the "memory bug")

    So I reverted back to the code posted above.
     

    GO HOKIES!!!
  • Re: How to prevent browser and proxy caching of web pages

    12-05-2006, 2:15 AM
    • Member
      17 point Member
    • Sivakumaar
    • Member since 12-13-2005, 10:56 AM
    • Bangalore
    • Posts 5

    Hi LudovicoVan,

        I had tried Response.Cache.SetCacheability(HttpCacheability.NoCache); it refreshes the page perfectly. but instead of refreshing the page, i want to display the "WebPage Expired" error page when the user clicks the back button. is it possible, if yes, please give the solution to achieve this?

     

    Thanks in advance

    Regards, 

    Siva 

  • Re: How to prevent browser and proxy caching of web pages

    12-05-2006, 6:34 AM
    good
  • Re: How to prevent browser and proxy caching of web pages

    12-05-2006, 8:38 AM
    • Star
      8,743 point Star
    • LudovicoVan
    • Member since 12-02-2004, 3:01 PM
    • The World's End
    • Posts 1,752

    Sivakumaar:
    instead of refreshing the page, i want to display the "WebPage Expired" error page when the user clicks the back button. is it possible, if yes, please give the solution to achieve this?

    Hello Siva,

    I am afraid a simple and straight answer is: forget it. The main point is the web model is the web model, and you shouldn't be bothering at all about which button the user pressed to go to some page, because however s/he got there, that's just a page request.

    So, disabling cache is something useful to force a page to be requested back to the server instead of being reloaded from the local cache. And, something you might need to add is some form of "idempotency", to prevent the same request to be processed twice on the server. But you have not, and are not supposed to have any serious control over browser's workings, and after all the browser might be everything including a telnet against port 80 of your server. So no point in working-around it. Simply code your site to "handle requests" and independent from the buttons users are pressing: back button, writing the url into the address bar, clicking a link or a bookmark, doesn't make any difference...

    At least, that's my approach. -LV

    Julio P. Di Egidio
    Software Analyst Programmer
    =BUSINESS AND SCIENTIFIC=
    =SOFTWARE DEVELOPMENT=
    http://julio.diegidio.name

    (Peace X Love] = [++1)
  • Re: How to prevent browser and proxy caching of web pages

    12-21-2006, 12:10 PM
    • Member
      94 point Member
    • sdgough
    • Member since 07-29-2005, 4:06 PM
    • Posts 27

    This works fine in IE & Firefox for me...

    Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1))
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.Cache.SetNoStore()

    I have it in my master page code-behind.

     

    Cheers!
    -Sean
  • Re: How to prevent browser and proxy caching of web pages

    01-08-2007, 12:44 AM
    • Member
      251 point Member
    • ujjaval
    • Member since 12-13-2005, 6:22 AM
    • Posts 73

    just checking.. if I put this in global.asax.cs file, it will work  for whole web application.. right?

     

  • Re: How to prevent browser and proxy caching of web pages

    01-08-2007, 6:40 PM
    • Member
      251 point Member
    • ujjaval
    • Member since 12-13-2005, 6:22 AM
    • Posts 73

    I have this lines in all my master page code-behind Page_Load method. Now, one interesting observation I have found.

     When a session expires, and I tried to open some page in my application, it redirected me to logout.aspx as it should. After that I clicked on [Back] button of browser for few times. In this case, as the session has expired, I expected logout.aspx rather than the actual pages as they are not cached. But it showed actual page with all the contents.

     

    Any ideas, how can I show logout.aspx page instead of all those pages?

     

    Thanks,

    Ujjaval

     

  • Re: How to prevent browser and proxy caching of web pages

    01-09-2007, 5:02 PM
    • Member
      14 point Member
    • AndreiR23
    • Member since 09-17-2006, 9:47 PM
    • Bucharest, Romania, Europe
    • Posts 9

    Thanks! It works great on IE 6.x and FireFlox but it won't work for Opera.. :(

    Anyone knows a trick for the Opera browser (well.. at least if you heard of Opera... )

  • Re: How to prevent browser and proxy caching of web pages

    02-07-2007, 7:55 PM
    • Member
      8 point Member
    • Netee
    • Member since 01-23-2007, 7:18 AM
    • Posts 6

    Thanks! Really helpful. But not working for Safari very weill. Safari will popup window to ask if send back to server, after clicking 'send', you will get the cached page again. Anyone knows how to make it work?

     Sean

Page 1 of 2 (30 items) 1 2 Next >