TYPO free

home

fighting for TYPO free code

Enable cookie-free domains in TYPO3

16 Jun 2009

YSlow says:

When the browser requests a static image and sends cookies with the request, the server ignores the cookies. These cookies are unnecessary network traffic. To workaround this problem, make sure that static components are requested with cookie-free requests by creating a subdomain and hosting them there.

If you have your own host, you can create such a subdomain with very little effort. The only thing you need is a new virtual host. 

<VirtualHost 73.23.32.45:80>
  ServerName www.s1.typofree.org

  DocumentRoot /var/www/typofree.org

  <Location />
    php_admin_value session.use_cookies 0
  </Location>

  Include /etc/apache2/sites-available/far-future-expires.conf
</VirtualHost> 

The Virtualhost has the same DocumentRoot as your real domain. Just the ServerName and ServerAlias are different. And set the PHP value session.use_cookies to 0. Don't ask me why, but after enabling the static domains, there was still a PHPSESSION cookie being set by some file.

Now we can use a content replacer like ja_replacer to append the static domains to all our static resources. Here is my TS setup:

config.tx_ja_replacer {
    search {
        1 ="/fileadmin/site/typofree.org
        2 ="fileadmin/site/typofree.org
        3 ="/fileadmin/site/typofree.org/script
        4 ="fileadmin/site/typofree.org/script
        5 ="/fileadmin/site/typofree.org/style
        6 ="fileadmin/site/typofree.org/style
        7 ="/uploads/
        8 ="uploads/
        9 ="/typo3conf/
        10="typo3conf/
        11="/typo3temp/
        12="typo3temp/
    }
    replace {
        1 ="{$s1BaseURL}/fileadmin/site/typofree.org
        2 ="{$s1BaseURL}/fileadmin/site/typofree.org
        3 ="{$s1BaseURL}/fileadmin/site/typofree.org/script
        4 ="{$s1BaseURL}/fileadmin/site/typofree.org/script
        5 ="{$s1BaseURL}/fileadmin/site/typofree.org/style
        6 ="{$s1BaseURL}/fileadmin/site/typofree.org/style
        7 ="{$s1BaseURL}/uploads/
        8 ="{$s1BaseURL}/uploads/
        9 ="{$s1BaseURL}/typo3conf/
        10="{$s1BaseURL}/typo3conf/
        11="{$s1BaseURL}/typo3temp/
        12="{$s1BaseURL}/typo3temp/
    }
}

ja_replacer can be improved to use preg_replace instead of str_replace. This would simplify the TS rules (or make them more coplex, depending on your regex skillz ;-).

You will not get more speed improvement if you add more subdomains. We just created a cookie free domain for static content. No more cookies are set for files served from this domain. The content is delivered to the client faster than before because we no longer have the cookie overhead.

You can get a speed improvement if you store the static content on a server with a different IP address. Most browsers limit the number of concurrent downloads from a single IP to 4. So if your server has more than one IP address, you can attach a different IP to the static virtual host and gain an extra speed advantage. The next step would be to add another IP and another virtual host for a s2 domain, gaining an even greater speed advantage.

It is not recommended (thx Georg ;-) to create more than two extra subdomains. There is a trade-off between the amount of extra domain names you use to store data and the time it takes the browser to resolve those names into IP addresses. Chances are you are also using tracking cookies that require the browser to resolve some more domains like google.com for example. So keep the number of extra domains down.

UPDATE 25 March 2013

The restriction is NOT per IP, but per Hostname!

Michael 21 Jun 2009, 13:58
Michiel, Thank you very much for your recent <a href="https://www.acqal.com/typo3-extension-support/typo3-performance-tuning/">TYPO3 performance</a> updates. We've got quite a few of our own, but the YSlow improvements have been among the slowest. Thanks to you we've got a better handle on them.

FYI - Your rating tool IS working now.
Commenting is closed for this item