Laravel 4应用程序中的浏览器缓存问题 [英] Browser Cache issues in Laravel 4 Application

查看:111
本文介绍了Laravel 4应用程序中的浏览器缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的浏览器缓存干扰了我的Laravel应用程序.

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

如果禁用了浏览器缓存,则一切正常.但是,如果启用该功能,并且重复单击同一链接,则甚至不会执行用于创建视图或收集数据的Laravel方法.

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.

含义是多种多样的.例如,用于编辑资源的表单或显示数据的网格(使用ajax从服务器加载),直到重新加载浏览器后才显示当前值.

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?

推荐答案

惊喜,令人惊讶-以前的解决方案在IE中不起作用.

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

再花几个小时后,我最终将以下内容添加到blade模板标题中:

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.

此外,我不得不防止缓存所有AJAX调用. 此问题提供了一些非常有用的答案.

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

以下内容在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.

这篇关于Laravel 4应用程序中的浏览器缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆