• Resolved jamiechong

    (@jamiechong)


    I’m getting a javascript error in my cart because jQuery isn’t found. This is because an inline script added from another plugin is placed above the optimize/concatenated js file placed before </body>

    To reproduce go to:
    https://savewithledlight.com/product/led-liquid-cooled-a15-6w/

    Click “Add to Cart”
    Click “View Cart”

    The javascript error appears on this page (need an item in the cart):
    https://savewithledlight.com/cart/

    If you view the page source, you will see near the bottom of the screen a few inline <script> tags that are before the optimized javascript file (where jQuery is included).

    I know there is the option to include the javascript in <head> but I really don’t want to do that. Also, the issue only seems to happen on the cart page, not others on my site.

    The relevant plugins I’m using are:
    autoptimize
    woocommerce
    enhanced-e-commerce-for-woocommerce-store

    So is there a reason why Autoptimize is not picking up these inline scripts? They’re being inserted with add_action('wp_footer', '...')

    https://wordpress.org/plugins/autoptimize/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Frank Goossens

    (@futtta)

    evening Jamie;
    inline script with “nonce” or “post_id” in it gets automatically excluded from optimization, to avoid the “random” strings in them to break the autoptimize’s cache. in your case I indeed see;

    "update_shipping_method_nonce":"534f904eea"

    (and there’s a nonce in the other inline JS block as well) Not excluding that from optimization would result in AO having to re-aggregate & -minify the JS for the cart-page every time the nonce changes, slowing things down & leading to a huge amount of cached JS.

    there are four approaches you can take to fix the page;
    1. force JS into head
    2. exclude jquery.js from optimization
    3. use the API to disable AO on /cart/ (see example code in autoptimize_helper.php_example)
    4. make a small change to your footer template, adding a “js insert marker” and using the API to tell AO to insert JS at that marker instead of before the closing body tag (again see example code in autoptimize_helper.php_example)

    hope this helps,
    frank

    Thread Starter jamiechong

    (@jamiechong)

    Thanks for the quick reply and a great explanation as to why this was happening. I didn’t think about nonces or post_ids cache busting. Makes sense…

    I’ve temporarily fixed this by excluding jquery.js, but will investigate your other solutions later.

    Thanks again!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Inline script Tags Not Optimized’ is closed to new replies.