問題描述
我問我的主人他們是否支持 mod_expires,他們告訴我支持.我使用 CakePHP 并在 webroot/.htaccess
和 `app_root/.htaccess 中嘗試了以下代碼,但考慮到 css、js、png、jpg 等緩存存在問題,Google 的頁面速度很慢.它找到了他們 24 小時!如下圖所示
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">標頭集緩存控制max-age=31449600, public"</FilesMatch>
以上代碼摘自.在 cakePHP 1.3+ 中,主題視圖資產已從 app/webroot/themed/themeTitle
移動到 app/views/themed/themeTitle/webroot
.
根據 CakePHP 1.3 文檔 您可以通過創建 app/webroot/theme/themeTitle
來恢復主題靜態資??產的舊位置(即在主 webroot 中)
鏈接到靜態資產與 1.2 略有不同.你還可以使用現有的 app/webroot/themed 并直接鏈接到那些靜態文件.應該注意的是,您需要使用完整路徑鏈接到 app/webroot/themed 中的資產.如果你想保留你的主題在 app/webroot 中的 assets 建議您 重命名app/webroot/themed 到 app/webroot/theme.這將使您能夠利用核心幫助程序路徑查找.以及保持性能不通過 PHP 提供資產的好處.
所以,我為解決這個問題所做的是將 app/views/themed/slate/webroot
中的所有文件復制到 app/webroot/theme/slate
以下屏幕截圖來自 FireFox Firebug,顯示圖像文件在一年后以秒為單位過期:
I asked my host if they supports mod_expires or not, they told me that it is supported. I use CakePHP and I tried the following code in webroot/.htaccess
and `app_root/.htaccess but Google's page speed is stile considering there is issue in caching of css, js, png, jpg, etc. it found them 24 hour! as the screenshot shows below
<FilesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "max-age=31449600, public"
</FilesMatch>
The above code is adopted from this article.
Also I used the following:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 year"
ExpiresByType text/html "access 1 month"
# ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 year"
ExpiresByType text/javascript "access 1 year"
ExpiresByType application/javascript "access 1 year"
# ExpiresByType application/x-shockwave-flash "access 1 month"
# ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 year"
</IfModule>
Also I tried to use both codes with each other but the Google Speed tell me the same. Look at the following screen shot:
I need to know how could I solve this issue?!!
Edit##
Using firebug in Firefox I noticed that only resources in the
app/views/themed/slate/webroot
are not be affected with the cache settings regarded above, but those found directly inapp/webroot
, i.e without, theming are affected well.
It seems to be a bug in cakephp. In cakePHP 1.3+ the themed view assets have been moved from app/webroot/themed/themeTitle
to app/views/themed/themeTitle/webroot
.
According to CakePHP 1.3 documentation you are able to restore the old place (i.e. in the main webroot) of the themed static assets by creating app/webroot/theme/themeTitle
Linking to static assets is slightly different from 1.2. You can still use the existing app/webroot/themed and directly link to those static files. It should be noted that you will need to use the full path to link to assets in app/webroot/themed. If you want to keep your theme assets inside app/webroot it is recommended that you rename app/webroot/themed to app/webroot/theme. This will allow you to leverage the core helper path finding. As well as keep the performance benefits of not serving assets through PHP.
So, what I have done for solve this issue is to copying all files in app/views/themed/slate/webroot
to app/webroot/theme/slate
The following screen shot is from FireFox Firebug that shows an image file get the expire after one year in seconds:
這篇關于利用瀏覽器緩存和 CakePHP的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!