5

I'm having an issue with the browser cache interfering with my Laravel application.

If the browser cache is disabled, everything works fine. However, if enabled, and the same link is clicked repeatedly, the Laravel method to create the view or collect data is not even executed.

The implications are manifold. For instance, a form to edit a resource or a grid which displays data (loaded form the server using ajax), do not show the current values until the browser is reloaded.

I've put a line in some of my methods which logs the current timestamp to prove this.

public function index()
{
    Log::info( microtime() );

    return View::make( $this->templates_root . 'index' );
}

No line turns up in the log, when a link is clicked repeatedly or a view is accessed once again. But it does if I reload the browser.

What can I do to prevent the browser from caching my views?

Anshad Vattapoyil
  • 23,145
  • 18
  • 84
  • 132
Rico Leuthold
  • 1,975
  • 5
  • 34
  • 49
  • Maybe this can be helpful http://stackoverflow.com/questions/8937447/force-cache-refresh-after-deployment – intelis Sep 26 '13 at 20:21

1 Answers1

6

EDIT:

Surprise, surprise - the previous solution did not work in IE.

After spending another couple of hours I ended up adding the following to the blade template header:

<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="cache-control" content="no-store" />
<meta http-equiv="cache-control" content="must-revalidate" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />

This seems to work for all browsers.

Furthermore, I had to prevent caching of all AJAX calls. This question provided some very useful answers.

The following does not work in IE:

I've found a solution - not a pretty one in my opinion.

Using a (global after) filter as follows ...

App::after(function($request, $response)
{
    // prevent browser caching
    $response->headers->set('Cache-Control','nocache, no-store, max-age=0, must-revalidate');
    $response->headers->set('Pragma','no-cache');
    $response->headers->set('Expires','Fri, 01 Jan 1990 00:00:00 GMT');
});

seems to force the browser to reload the page from the server.

The answers to this question provided some very useful information.

However, I'm still wondering why other developers do not have the same problem or if they have, how they solve it.

Community
  • 1
  • 1
Rico Leuthold
  • 1,975
  • 5
  • 34
  • 49
  • I tried this solution also, but it is not the best one. When you use mentioned header you get white flashes in chrome between loading. – enigmaticus Nov 25 '14 at 08:33