Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
825 views
in Technique[技术] by (71.8m points)

php - Leverage Browser Caching and CakePHP

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:

enter image description here

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 in app/webroot , i.e without, theming are affected well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...