Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect a page refresh (F5) serverside?

...in comparison to requests of normal link click behaviour. I thought I might be able to use this to throw away some cached stuff on the serverside. For a more technical orientated target audience this could be a relative natural way of clearing a cache for i.e. graphs and charts.

To be clear - I am using ASP.NET MVC

like image 847
Dirk Boer Avatar asked Mar 23 '23 00:03

Dirk Boer


2 Answers

I've just checked the three browsers you're likely to care about, and all three add extra cache headers in the request when you refresh the page. Conceivable you could check for those headers to throw out some server-side cache. It also seems a logical and natural way to do it.

  • IE: adds "Pragma: no-cache"
  • Chrome: adds "Cache-Control: max-age=0" and "If-Modified-Since: Tue, 17 Dec 2013 10:16:22 GMT" (disclaimer: time may vary)
  • Firefox: adds "Cache-Control: max-age=0"

I just checked this by refreshing this page in all three browsers, and checking Fiddler. It is possible there is more sophisticated logic going on that I haven't caught on to.

like image 197
Menno van den Heuvel Avatar answered Apr 02 '23 00:04

Menno van den Heuvel


Since it's in MVC, I would use the TempData to achieve this. On the first load of your method, you can set a value in the TempData so on the next load(the refresh) you would have a value set in this.

Let say you have a Add method, I think that should do the trick :

public virtual ActionResult Add(Model model)
        {
            if(TempData.ContainsKey("yourKey"))
            {
                //it means that you reload this method or you click on the link
                //be sure to use unique key by request since TempData is use for the next request 
            }

            TempData.Add("yourKey", true);

            return View(model);
        }
like image 24
VinnyG Avatar answered Apr 01 '23 22:04

VinnyG