3

is there an easy way to add subdomains to the URLs of my static assets (JS, CSS, images) in Magento? What I am looking for is a way to change an url like this

http://store.com/js/script.js

to this

http://static1.store.com/js/script.js

The number behind 'static' should be rand(1,3).

The reason I want that is of course performance. By spreading assets over several domains browsers load them faster since they are restricted to load not more than 2 files from one domain at a time.

I know there exists a plugin for Wordpress and I would like to do it for Magento.

This whole thing has to happen server side, so before any html is generated.

Thank you for your input!

3 Answers 3

4

this is actually not needed anymore with modern browsers, as the number of parallel requests got increased to mostly 6, and some even to 8, and thx to new http standards they reuse the same connection.

So spreading over several domains only makes caching harder, and maybe causes a faster hit of the connection limit on the server side, as the connections are more limited by bandwidth so the connections are longer used.

2
  • Adding to that, why don't you let varnish handle the static resources? There's no reason they should hit an app server.
    – user4351
    Commented Nov 7, 2014 at 20:01
  • 1
    Just a few notes. Given most sites average 100 requests per page, there is a good reason to still do this. Adding Varnish will achieve nothing other than reducing load balancer to web server bandwidth - it will do nothing to improve asset delivery speed. Using multiple domains will promote cacheability (due to removal of cookie), not the opposite, provided the sub domain uses a fixed seed for the rand method (ie. so the same request comes from the same sub domain). Keepalives will reduce the need for renegotiation for serial requests, but will not improve delivery speed or parrallize requests. Commented Nov 14, 2014 at 0:26
1

app/code/core/Mage/Core/Model/Store.php has a function getBaseUrl that returns the base url for media and skin files. It also caches them and it'll only be used once at any given point in magento. So you could override it, but it'd still only chuck out one url, you could do something like alex_b says, you'd also need to do it for css in the head but also your media urls, or you could use something like the google pagespeed module, and let it do the sharding for you.

1
  • The google pagespeed module was an excellent tip. But I am using Varnish as a cache which seems to be a bit more difficult to integrate with pagespeed (at least the site breaks and everything is mangled when coming out of varnish). Without that I was able to set it up in 10 minutes and it worked like a charm. Commented Oct 30, 2014 at 14:17
0

You can create new setting (textarea) as a config node in system.xml and define the list of servers each one on new line in that setting.

After you need to rewrite method _prepareStaticAndSkinElements in class Mage_Page_Block_Html_Head. That line $baseJsUrl = Mage::getBaseUrl('js');.

Here you should extract list of servers from new setting and use them instead of $baseJsUrl below.

P.S. also read about load balancer http://en.wikipedia.org/wiki/Load_balancing_(computing). That is server-side solution.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.