javascript文件没有被缓存?

我正在尝试优化我的网络应用程序,不幸的是最终得到了大约450K的javascript文件大小 - 那个 压缩之后[我需要一段时间来重做javascripting但是在那之前我必须上线] - 我最初已经制作了许多小的javascript库来处理。 而我所做的是我有一个包含所有javascript文件的php文件,然后我将我的php文件包含在下面:</ p>

 &lt; script language =“js / js。  php“&gt;&lt; / script&gt; 
</ code> </ pre>

事情是,我希望我的文件会在第一次加载时缓存,但似乎每次刷新 页面或回到它,文件从服务器重新加载 - 我使用firebug检查了这一点。 还有什么我必须添加以确保我的文件缓存在用户端..或者我在这里误解了缓存的想法?</ p>
</ div>

展开原文

原文

I'm trying to optimize my web application and unfortunately have ended up with a javascript file size of around 450K - that too after compressing [it would take a while for me to redo the javascripting but until then I have to go live] - I initially had made a number of small javascript libraries to work upon. And what I do is I have a php file which includes all the javascript files and then I included my php file as below:

<script language="js/js.php"></script>

The thing is that I was hoping that my file would be cached upon the first load but it seems every time I refresh the page or come back to it the file is reloaded from the server - I checked this using firebug. Is there anything else that I must add to ensure that my file is cached on the user end.. or am I misunderstanding the idea of a cache here?

draj840143
draj840143 Apache的.htaccess是你的选择吗?
大约 10 年之前 回复

2个回答



您需要在php中设置一些标题以确保文件被缓存。</ p>

在js.php的顶部put:</ p>

  ob_start(“ob_gzhandler”); 

$ expires = 2678400; // 1个月的秒数
header(“Pragma:public”);
header(“Cache-Control:maxage =”。$ expires);
header('Expires:'。gmdate('D,d MYH:i: s',time()+ $ expires)。'GMT');
</ code> </ pre>

这将动态添加基本缓存+ gzip压缩。</ p>

</ div>

展开原文

原文

You'll need to set some headers in php to ensure the file is cached.

At the top of js.php put:

ob_start("ob_gzhandler");

$expires = 2678400; // 1 month in seconds
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');

That will add both basic caching + gzip compression on the fly.

doutou7549
doutou7549 工作就像一个魅力:D谢谢!
大约 10 年之前 回复
doukui7574
doukui7574 另外,他可能想要将脚本标签更改为有效,<script type =“text / javascript”src =“js / js.php”> </ script>
大约 10 年之前 回复



为什么不留下它.js文件让web-server处理缓存?

压缩不是你的事 确实需要,但条件获取是</ p>
</ div>

展开原文

原文

Why not to leave it .js file and let web-server take care of caching?
Compression is not the thing you really need but Conditional Get is

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐