2

We're noticing an issue where, due to our caching scheme (using external services like CloudFlare), old compiled assets are deleted from our server - ie a merged JS file with a generated hash as the filename. Sometimes this causes clients to no longer be able to see the assets and the site functionality diminishes. We're using 1.13.*. Is there a way to tell Magento to keep old asset files when it generates new ones?

2 Answers 2

2

It looks like the function that starts the removal of the merged JS and CSS files is called cleanMergedJsCss located in app/code/core/Mage/Core/Model/Design/Package.php. You'll need to override that class to change the function.


Start by creating app/etc/modules/Magentose_Keepassets.xml

 <?xml version="1.0"?>
 <config>
      <modules>
           <Magentose_Keepassets>
                <active>true</active>
                <codePool>local</codePool>
           </Magentose_Keepassets>
      </modules>
 </config>

Create the path & file: app/code/local/Magentose/Keepassets/etc/config.xml

 <?xml version="1.0"?>
      <config>
           <modules>
                <Magentose_Keepassets>
                     <version>1.0.0</version>
                </Magentose_Keepassets>
           </modules>
           <global>
                <models>
                     <magentose_keepassets>
                          <class>Magentose_Keepassets_Model</class>
                     </magentose_keepassets>
                     <core>
                          <rewrite>
                               <design_package>Magentose_Keepassets_Model_Design_Package</design_package>
                          </rewrite>
                     </core>
                </models>
           </global>
      </config>

Create the path & file: app/code/local/Magentose/Keepassets/Model/Design/Package.php

 <?php

 class Magentose_Keepassets_Model_Design_Package extends Mage_Core_Model_Design_Package
 {

      // To stop Magento from deleting the old files, change the second argument in the calls to _initMergerDir to false. 

      public function cleanMergedJsCss()
      {
         $result = (bool)$this->_initMergerDir('js', false);
         $result = (bool)$this->_initMergerDir('css', false) && $result;
         return (bool)$this->_initMergerDir('css_secure', false) && $result;
      }

 }
1

No, by default magento uses a hash of the included file name to generate the merged js/css file name. Therefore if you haven't add, remove or change any of files then the merged js/css files will retain the same name. Shouldn't the file remain on your cdn until you purge it?

You could create a custom module to change this behavior

/app/code/core/Mage/Adminhtml/controllers/System/CacheController.php

// clean javascript/css cache
 if ($this->getRequest()->getPost('jscss_action')) {
        if (Mage::getDesign()->cleanMergedJsCss()) {
            $this->_getSession()->addSuccess(
                Mage::helper('adminhtml')->__('The JavaScript/CSS cache has been cleared.')
            );
        } else {
            $this->_getSession()->addError(Mage::helper('adminhtml')->__('Failed to clear the JavaScript/CSS cache.'));
        }
    }

/app/code/core/Mage/Core/Model/Design/Package.php

/**
 * Remove all merged js/css files
 *
 * @return  bool
 */
public function cleanMergedJsCss()
{
    $result = (bool)$this->_initMergerDir('js', true);
    $result = (bool)$this->_initMergerDir('css', true) && $result;
    return (bool)$this->_initMergerDir('css_secure', true) && $result;
}

/**
 * Make sure merger dir exists and writeable
 * Also can clean it up
 *
 * @param string $dirRelativeName
 * @param bool $cleanup
 * @return bool
 */
protected function _initMergerDir($dirRelativeName, $cleanup = false)
{
    $mediaDir = Mage::getBaseDir('media');
    try {
        $dir = Mage::getBaseDir('media') . DS . $dirRelativeName;
        if ($cleanup) {
            Varien_Io_File::rmdirRecursive($dir);
            Mage::helper('core/file_storage_database')->deleteFolder($dir);
        }
        if (!is_dir($dir)) {
            mkdir($dir);
        }
        return is_writeable($dir) ? $dir : false;
    } catch (Exception $e) {
        Mage::logException($e);
    }
    return false;
}

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.