简体   繁体   中英

How to enable caching for all requests in yii2?

I got from google the next notice:

 example.com/assets/a3f4f38d/jquery.min.js (expiration not specified) example.com/css/main.css (expiration not specified) 
example.com/css/swiper.min.css (expiration not specified) 

example.com/img/clouds/cloud-portfolio_mob.png (не указан срок действия) example.com/img/clouds/min0.png (expiration not specified) 

example.com/img/clouds/min1.png (expiration not specified) example.com/img/clouds/min2.png (expiration not specified) 

example.com/img/heroes/A.jpg (expiration not specified) example.com/img/heroes/B.jpg (expiration not specified) 

example.com/img/heroes/C.jpg (expiration not specified) example.com/img/icons/Dot.gif (expiration not specified) example.com/img/icons/menuSm_min.png (expiration not specified) 

example.com/img/icons/red-heart-border_min.png (не указан срок действия) example.com/img/icons/red-heart_min.png (не указан срок действия) example.com/img/loader/Loader-Advanced.gif (не указан срок действия) 

example.com/img/logo/t.svg (expiration not specified) example.com/img/main_background/bg_mob.jpg (expiration not specified)

For resolve this problem, i found the next behavior:

public function behaviors()
  {
    return [
      [
        'class' => 'yii\filters\HttpCache',
        'only' => ['index'],
        'lastModified' => function ($action, $params) {
//          $q = new \yii\db\Query();
          return time() + 3600;
//          return $q->from('users')->max('updated_at');
        },
        'sessionCacheLimiter' => 'public',

//            'etagSeed' => function ($action, $params) {
//                return // generate ETag seed here
//            }
      ],
    ];
  }

After this on main document (not for all requests, only for / ) appears three additional headers:

Cache-Control:public, max-age=3600  Last-Modified:Fri, 13 May 2016 09:03:45 GMT  Pragma:

Althought set time()+3600 , Last-Modified equals Data header

How to set for all requests not for only / So as I understand google wants that I set Expire header?

Expire goes into .htaccess in web folder:

<IfModule mod_expires.c>

ExpiresActive On 
ExpiresDefault "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>

Also, in yii2 you could set the version (query string):

'components' => [
        'assetManager' => [
            'appendTimestamp' => true,
        ],
    ],

Info : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#cache-busting

Versions or last-modified?

Here's a in deep explanation : File Caching: Query string vs Last-Modified?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM