I don't like the default Magento combine js / css feature. It just clumps up the entire page of js and css into one file on a per-page basis. This results in pages that have one additional css resource generating a new css file - 90% of which should be cached by the client and not downloaded AGAIN as part of this new css file.

What I am wanting to do is intelligently combine js and css files, so the default head addJs / addItem actions are grouped into one file, then any additional page-specific js is grouped into another file.

How do I detect what layout block (e.g. default) the action addItem was being called from?

Alternatively, if there is an extension that does this then I would be interested in the pre-built solution (I haven't found one).

1 Answer 1


This is tricky and I don't have a solution, but some hints: The layout handles (this is the term for what you called "layout block") are loaded and merged in Mage_Core_Model_Layout_Update before they are processed. This means, at the moment where addAction is called, it comes from one big XML tree of blocks and actions without the handles and there is no way to find out where it comes from.

So I could only imagine a solution with an extended layout model, where you preprocess the XML coming from the default handle before it is merged. Then you could for example add an additional parameter to addCss actions, $fromDefault = true - of course you have to extend Mage_Page_Block_Html_Head as well to process this parameter somehow.

  • I have thought I will be needing to add a flag to the add functions. I was kinda hoping now to, as it would allow any extensions I install to work out of the box without me going through the layouts adding the extra parameter. And if I can get this to work as I want it, I might even pop it on the extensions site where other people may not know how to add the extra params. Apr 15, 2016 at 13:29
  • What if I turn this around, and do the merge thing last? In the admin panel pull out a list of all css / js that is being added to the head on the homepage so that the admin may select what to combine / merge on ALL pages, then do something like str_replace to remove the selected css / js from the generated response, and add the combined css / js urls to the head? Actually, that feels very very hacky and I feel dirty just for thinking it. But I am open to alternative suggestions? Apr 15, 2016 at 13:32
  • Probably still a better approach than what I suggested. You could use your own code to read the merged layout XML (not the merged updates for a particular site) to find out which files are added in the default handle, cache this information and use it for the merged files. Then you can either remove the files that have been merged from the response using an observer or rewrite Mage_Page_Block_Html_Head::addItem() to not do anything if the given file is in the list. Apr 15, 2016 at 13:37

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.