0

new to SE, so apologies if this isn't specific enough.

We use Magento CE for a smallish online store (CE1.7.x).

I am trying to delay loading of JS only for shop browsers to speed the page up.

Per this topic, In the head.php (public_html/app/code/core/Mage/Page/Block/Html) I have added this:

        // static and skin javascripts
        // check if logged in user is Admin, if so, do not async JS -- added by Tim Oct 15 2014
        // remove lines 207-214 to remove this -- note, async JS breaks admin
       if (Mage::app()->getStore()->isAdmin()) {

        $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
            empty($items['js']) ? array() : $items['js'],
            empty($items['skin_js']) ? array() : $items['skin_js'],
            $shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
        );
       }

        // if not Admin, async JS to speed page up
        $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" async src="%s"%s></script>' . "\n",
        empty($items['js']) ? array() : $items['js'],
        empty($items['skin_js']) ? array() : $items['skin_js'],
        $shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
        );

The problem is: Whilst it speeds the page load to visitors up (verified by GTMetrix), some aspects of Admin break. For instance, CMS->Pages, I can't edit anything after the second listed page. (When I mouse over, they're inactive).

I commented out the Admin test and reverted to original code, and it's fine.

This indicates it is the code, but I've no idea why.

I am logged in with a user called Tim, not Admin which makes me wonder if I am testing the

My question: Why would this test cause the CMS pages would be breaking (long shot for the reasons I've explained above).

We test for Admin, and if Admin view, we load first. If not admin, load async JS.

I should note, I am neither a Magento dev nor PHP dev.

Again, apologies if this is not specific enough.

1

To me it looks like the async part is always loaded, so when the store is admin all script tags actually get loaded twice. To solve, make it an else clause:

if (Mage::app()->getStore()->isAdmin()) {

    $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" src="%s"%s></script>' . "\n",
        empty($items['js']) ? array() : $items['js'],
        empty($items['skin_js']) ? array() : $items['skin_js'],
        $shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
    );
}

else {
    // if not Admin, async JS to speed page up
    $html .= $this->_prepareStaticAndSkinElements('<script type="text/javascript" async src="%s"%s></script>' . "\n",
    empty($items['js']) ? array() : $items['js'],
    empty($items['skin_js']) ? array() : $items['skin_js'],
    $shouldMergeJs ? array(Mage::getDesign(), 'getMergedJsUrl') : null
    );
}
| improve this answer | |
  • Of course. This should have been obvious even to my non-developer self. Thanks for your patience with a newbie, I appreciate it. – tmarsh1 Oct 20 '14 at 0:03
  • Actually, I'm finding this code actually prevents a bunch of buttons appearing throughout the admin. For example, if you go to Customer->Add Order, the "Add Product" button won't appear. Similarly, other menu items won't function. I've commented the code out, changed my htaccess to not cache JS, but to no avail. – tmarsh1 Oct 20 '14 at 1:01
  • I tried it myself and cannot recreate the problem you are having. Normally when I optimise sites I recommend mod_pagespeed which does a better job of delaying JS and so is more likely to work with admin pages. – clockworkgeek Oct 20 '14 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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