jQuery defer attribute added when they should not be included

Created on 16 October 2022, over 1 year ago
Updated 14 July 2023, 12 months ago

Problem/Motivation

For some time we've seen issues when we attempted to upgrade to a newer version of jQuery using the jquery_update module. If we had AdvAgg disabled, the code worked correctly, but when AdvAgg was enabled, newer versions of jQuery caused random loading problems.

The jquery_update module recently announced they will only be supporting 7.x-4.x going forward, which uses newer versions of jQuery by default. When we tried to update to 7.x-4.0-rc1, we saw the same problems as in the past. jQuery UI features not loading, jQuery functions never running, etc.

We decided to investigate if there might be a problem with AdvAgg, and were surprised to find there seems to be a long-standing issue with the options in the AdvAgg Modifications tab.

Looking through gitlab, there was functionality added in 2015 regarding deferring jQuery if code is being loaded via CDN:
https://www.drupal.org/project/advagg/issues/2531020 β†’
https://git.drupalcode.org/project/advagg/-/commit/ee0680135d5d5aafd8371...

In 2017, there were changes made to this code, but seems as though the patch might have some issues:
https://www.drupal.org/project/advagg/issues/2906290 β†’
https://git.drupalcode.org/project/advagg/-/commit/544dc644cf7e89ec00b33...

First, there is code which still references $form_state['values']['advagg_mod_js_defer_jquery']. This field was removed from the form, so there's no purpose for it to exist.

Second, it looks as though there might have been a mix-up when variable changes were made. In the 2015 version, the code looked like this:
if (!variable_get('advagg_mod_js_defer_jquery', ADVAGG_MOD_JS_DEFER_JQUERY)) {

... and it 2017 it changed to this:
if (!variable_get('advagg_mod_js_defer_jquery', ADVAGG_MOD_JS_DEFER_JQUERY) !== FALSE) {

... however, the variable definitions also changed:
original: define('ADVAGG_MOD_JS_DEFER_JQUERY', FALSE);
new: define('ADVAGG_MOD_JS_DEFER_JQUERY', TRUE);

Given the 2017 patch removed the advagg_mod_js_defer_jquery field, there's no way to change the value. It is always set to TRUE. A negative of a TRUE would result in a FALSE, which will always equal FALSE. Therefore the conditional will never result in TRUE, and jQuery script tags will always have the defer attribute included. This should not be the case if jQuery is loaded by CDN and Deferred inline JavaScript Execution is enabled.

Steps to reproduce

Install latest 7.x-2.x version of AdvAgg, configure for it to be enabled, and set the following modifications:
* Move JS to the footer: All but what is in the $all_in_footer_list (recommended)
* Deferred JavaScript Execution: Add The defer tag to All Script Tags: All but external scripts (recommended)
* Asynchronous JavaScript Execution: Group together in the header (recommended)
* Put a wrapper around inline JS if it was added in the content section incorrectly (recommended)
* Deferred inline JavaScript Execution: Put a wrapper around inline JS so it runs from a setTimeout call (recommended)

Install latest 7.x-4.x version of jQuery Update, configure for it to be enabled with jQuery 1.12 and load jQuery using Google.

Load pages, examine page HTML, and will see defer="defer" is included as an attribute on the script tag for loading jQuery. This should not occur, since the logic says if jQuery loading is to be deferred (it is set by default to be TRUE), and if "Deferred inline JavaScript Execution" is set to TRUE (it is), and jQuery is being loaded via CDN (this example uses Google), then defer should be removed.

Once this test is done, apply the attached patch and see that the defer attribute is successfully removed.

Proposed resolution

See attached patch. Let me know if I'm missing something or thinking about this issue incorrectly.

Remaining tasks

Review and update patch as necessary.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Modifier

Created by

πŸ‡ΊπŸ‡ΈUnited States ron_s

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024