Browser Cache issues in Laravel 4 Application

南楼画角 提交于 2020-01-01 10:53:12

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19012698/browser-cache-issues-in-laravel-4-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!