3

I am working on a Symfony2 application that uses a lot of images, JS and CSS files.

I would like the browser to cache these aggressively, so that the majority of pageloads will not re-requests these assets.

The default appears to be for the browser to keep re-checking them and for the server to respond with a 304 Not Modified. How do I change this so that it doesn’t even run this request? (I don’t mind if it uses ETag or Expires or whatever mechanism, just as long as the majority of page requests don’t run separate requests for all the images.)

I’ve googled around a lot and found a lot of information on how to do this for pages where you have a $request object in your controller that you can set various properties on. However, I’m asking how to do this for assets (such as images) which are not returned by any of my PHP code.

2
  • The caching of asset is done with your web server (Apache, IIS, ...). What is your web server ? Commented Aug 4, 2014 at 6:57
  • @griotteau: The webserver is Apache. Commented Aug 4, 2014 at 11:54

2 Answers 2

6

You must add this in your httpd.conf file :

ExpiresActive On
ExpiresByType image/gif "access 1 month"
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 month"
ExpiresByType text/js "access 1 week"
ExpiresByType application/javascript "access 1 week"

The module 'mod_expires' must be activated

Also, don't forget to add an asset_version in your config file (see http://symfony.com/doc/current/reference/configuration/framework.html#ref-framework-assets-version), in order to invalidate cache when you change some asset

Sign up to request clarification or add additional context in comments.

3 Comments

where is the httpd.conf file located? is this in apache or a symfony directory?
by the way, I put this in my .htaccess file and worked okay, the key difference in symfony seems to be, ExpiresByType text/js "access 1 week" ExpiresByType application/javascript "access 1 week", instead of other ways of mentioning javascript like, ExpiresByType text/x-javascript "access plus 1 month" ExpiresByType text/javascript "access plus 1 month"
@JosephAstrahan httpd.conf is in /etc/apache2. In ubuntu use the apache2.conf file
1

It is not for your web server, but for someone who use nginx+symfony the solution is:

  1. Edit config.yml to enamle asset version control it describer here
#app/config/config.yml
    framework:
        # ...
        assets:
            version: 'v2'

Where 'v2' - is your custom asset verion name.

  1. Edit your nginx config as it described here
server {
...
    location ~* ^.+\.(rss|atom|jpg|jpeg|gif|png|ico|rtf|js|css)$ {
        expires max;
    }
...
}

Now all your assets will have the longest expiration period.

If your make new release and change some .css and .js files, just edit version name eg. from 'v2' to 'v3'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.