heimrichhannot / contao-haste_plus

GNU Lesser General Public License v3.0
0 stars 4 forks source link

ReferenceError: jQuery is not defined #13

Closed MicioMax closed 6 years ago

MicioMax commented 6 years ago

I'm not sure if this package is the culprit, but as it inserts the jquery script into ['TL_JAVASCRIPT'] array, I suppose it is. I get the error above, ReferenceError: jQuery is not defined, in backend pages. Digging a bit I found that jquery is loaded AFTER the contao-haste_plus scripts : GET https://www.timberstruct.eu/assets/js/haste_plus.min.js,environment.min.js,files.min.js,arrays.min.js,...-1d344c8d.js?v=37bb1afc GET https://www.timberstruct.eu/assets/jquery/js/jquery.min.js even if in 'vendor/heimrichhannot/contao-haste_plus/config/config.php' this is done the way around (jquery is inserted at index 0 and the remaining at index 1, so AFTER it). Probably some other module screws it up, but I couldn't find the cause. The only workaround I found by now is to manually patch the vendor/contao/core-bundle/src/Resources/contao/templates/backend/be_main.html5 template, inserting the jquery loading; after the patch the bug vanishes.

This bug affects my contao-privacy setup (no effect clicking on 3 buttons in general setup page) : jquery_not_defined

With the patched template, the buttons start working.

koertho commented 6 years ago

Please always provide your Contao and Package versions. The jquery problem is caused by another package, we can't really do something here. It's good you could fix it by yourself.

MicioMax commented 6 years ago

Hi, It seems identical to closed issue #12. Contao is version 4.6, all the packages updated to last (or even dev) version. I'm not sure that the culprit is another package, it seems more a stuff of contao 4.6 which probably don't follow the loading order in TL_JAVASCRIPT array. I doubt that another package is swapping just the load order of jquery and haste_plus files, leaving all the remaining unchanged...

MicioMax commented 6 years ago

Found the culprit. It's the package

contao-dd/advanced-classes-bundle

Which does stuffs in a not-so-clever way:

/**
 * Backend Javascript
 */
if (TL_MODE == 'BE') {
    $GLOBALS['TL_JAVASCRIPT']['jquery'] = 'assets/jquery/js/jquery.min.js';
    $GLOBALS['TL_JAVASCRIPT']['noconflict'] = 'bundles/contaoddadvancedclasses/js/jquery.noconflict.js';
    $GLOBALS['TL_JAVASCRIPT']['advanced_classes'] = 'bundles/contaoddadvancedclasses/js/jquery.advanced_classes.js';
}

It doesn't test if jquery is already there and replaces it, with the result that it goes at the end of the array, so haste_plus files brings an error when loaded. IMHO all those ways to add jquery to the backend are prone to bugs, and I don't see any simple solution from the outside. It should be added by contao people.

MicioMax commented 6 years ago

I was (partially) wrong, the culprit is haste_plus. The problem is that all your JS are marked as "|static", but NOT the jquery one. In contao 4.6 the |static javascripts are loaded inside HEADER, not BODY. So they're loaded BEFORE jquery, which causes the error. If I put the |static into the jquery path, it solves the problem for backend, but NOT for the frontend, where jquery is still loaded as non-static, so in the body. Why do you need them static ?

$GLOBALS['TL_JAVASCRIPT'] += [
    'haste_plus'             => '/system/modules/haste_plus/assets/js/haste_plus.min.js|static',
    'haste_plus_environment' => '/system/modules/haste_plus/assets/js/environment.min.js|static',
    'haste_plus_files'       => '/system/modules/haste_plus/assets/js/files.min.js|static',
.....
MicioMax commented 6 years ago

Hi,

I just made a pull request that solves backend jquery problem on contao 4.6. For the frontend (same problem, only with some themes) the cause is external to contao-haste_plus.