Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Browser Cache issues in Laravel 4 Application

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?

like image 615
Rico Leuthold Avatar asked Sep 25 '13 18:09

Rico Leuthold


People also ask

How does Laravel manage cache?

The cache configuration is located at app/config/cache. php . In this file you may specify which cache driver you would like used by default throughout your application. Laravel supports popular caching backends like Memcached and Redis out of the box.

How do I enable Laravel cache?

To enable Laravel caching services, first use the Illuminate\Contracts\Cache\Factory and Illuminate\Contracts\Cache\Repository, as they provide access to the Laravel caching services. The Factory contract gives access to all the cache drivers of your application.

Does Laravel have caching?

Laravel provides a robust and easy-to-use implementation of caching and different caching backends. With Laravel cache, you can efficiently and effectively switch between many caching engines without writing any code. You can find the configuration of the Laravel cache within the config/cache.


1 Answers

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.

like image 154
Rico Leuthold Avatar answered Oct 17 '22 04:10

Rico Leuthold