Problem/Motivation
When using JavaScript aggregation modules such as AdvAgg (https://www.drupal.org/project/advagg), the recent updates to the eu_cookie_compliance.js file can cause pages to load improperly. Specifically, aggregators will often load files in a different order than expected, and if variables have not yet been defined, it can cause JavaScript errors.
Steps to reproduce
Install EU Cookie Compliance and AdvAgg modules, and attempt to rebuild AdvAgg JS aggregation files until errors are seen in the browser console that Drupal.settings.eu_cookie_compliance is not defined.
Proposed resolution
See attached patch. In our testing, this approach resolves the issue.
Remaining tasks
Review attached patch.
| Comment | File | Size | Author |
|---|---|---|---|
| eu_cookie_compliance-js_aggregation-1.patch | 2.36 KB | sgdev |
Issue fork eu_cookie_compliance-3242662
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
svenryen commentedThanks for the patch. Does this also solve the issue if you set custom values for the three cookie states under Advanced?
Comment #3
sgdev commentedI don't see how this would solve the issue if there are custom values. If
Drupal.settings.eu_cookie_complianceis not yet accessible by the DOM, custom values will not be present.The options would be to deliver default values (as the code currently does), or re-structure the functions in a way that depends on certain events occurring to ensure the values are available.
Comment #4
svenryen commentedI will see if I can move the initialization of those values inside the behavior. I don't think they have to live in the global space, so it should be possible to solve this issue. I'll mark this as "Needs work" for now and look into it one of the coming days.
Comment #6
svenryen commented@ron_s, is there any chance you can help set up a test site that demonstrates the problem, or send me a database or advagg generated js from such a site?
I tried to enable advagg and a few related modules on a basic Drupal 7 site and can't seem to reproduce the issue here. The variables are loaded at the right place, and the script still works.
Comment #7
svenryen commentedComment #8
berdirI'm also seeing this with the latest version on Drupal 7. The site is not yet using the module, no advagg or similar is used. The site _is_ using jquery_update with version 1.10. Reverting to 1.4 (core) does not fix the error (and causes others in custom JS then of course). Neither does completely disabling jquery_update.
What I'm seeing is that in the aggregated version, eu_cookie_compliance.js is still in its own file and is loaded as the very first aggregated JS file. JQuery is then obviously not defined yet. Why that happens and only when aggregated I'm not sure yet.
I did test downgrading to 1.36 and 1.30 instead of .37, that didn't help either ( I didn't reinstall with those lower versions).
Ping me/contact me if you want to have a look at the site, but not sure if you can really see anything useful from the outside.
I'll start looking more closely into this in the coming days.
Comment #9
sgdev commented@svenryen, I agree the initialization needs to be inside the behavior. Having it in the global space is the cause of the problem.
I would most likely not be able to provide a reproducible test site, because the issue is reliant on the order and aggregation of the JavaScript. If I completely purge the aggregation and rebuild, more than likely it will be restructured in a way where this is not a problem. However, given enough time and rebuilding of aggregation files, it will eventually return again.
@Berdir, our site uses both advagg and jquery_update. In our case, disabling advagg, while continuing to use default Drupal performance tools and jquery_update, fixes the issue. If you do have the default "Aggregate JavaScript files" on the Performance tab enabled, I would recommend disabling to see if it corrects the issue.
Comment #10
berdirYes, disabling aggregation does help, but I think the problem I have is different than yours, because I get a different error. Mine is that jQuery is undefined. Sorry for not reading it properly, just looked at the issue title.
Comment #11
sgdev commentedYes I see, we are not receiving a jQuery is undefined error. I'm fairly certain both problems are due to attempting to initialize variables outside a behavior. Have seen other modules with "jQuery is undefined" when trying to run code in a global space, and jQuery has not quite loaded yet.
Comment #12
anybodySadly I can confirm I'm having the same issue since the last update. Same as in the issue summary.
We're not using AdvAgg on the page, just Drupal Core Aggregation.
Comment #13
guardiola86 commentedIt doesn't work for me, even if I disable advanced aggregation:
I'm using jquery update module 2.7 and jquery version 1.10
Comment #14
anybodyFor us the error is "Uncaught TypeError: Cannot read properties of undefined (reading 'cookie_value_disagreed')" if the script is enabled to load in the head (which is required to block certain scripts).
Still unresolved. The only working fix is to downgrade to 7.x-1.36! So the issue must have been introduced in 7.x-1.37 and should thereby be solvable.
We do NOT use AdvAgg!
Comment #15
anybodyThese are the many changes introduced with 7.x-1.37:
https://git.drupalcode.org/project/eu-cookie-compliance/-/compare/7.x-1....
Mainly the code in js/eu_cookie_compliance.js is broken due to the fact that if put in header
Drupal.settings.eu_cookie_compliancemay be undefined if placed before Drupal.settings were initialized.So the implemented fallbacks are not well implemented as they can't handle an undefined
Drupal.settings.eu_cookie_compliancevariable.I'd suggest to first set the fallbacks in an array and then override them with the actual values, if the keys exist.
This whole part:
Must be refactored and tested with eucc placed in head setting.
Comment #16
svenryen commentedI'll try to find time this weekend. Thanks for the updates.
Comment #17
anybodyThank you very much @svenryen! :)
7.x-1.36 is fine for us, so we'll keep on using that.
Comment #18
svenryen commentedComment #19
svenryen commentedCan any of you share what settings you're using for Advagg, and what sub-modules are enabled?
I tried to enable Advagg and set it to the recommended (optimized) settings, but I don't get any JavaScript errors.
Comment #20
svenryen commentedFor now, since I couldn't reproduce this with Advagg on a fairly basic site, as a last resort I'm excluding this module's script from Advagg aggregation.
Let me know how the patch in the branch helps, or please provide an archive with an installable site + database that I can use to reproduce.
https://git.drupalcode.org/project/eu-cookie-compliance/-/merge_requests...
Comment #21
anybody@svenryen, did you see #15 and put the code into the header?
My personal guess is, that this is the root cause. Not AdvAgg.
Comment #22
svenryen commentedI hear you, but there's also the variable euCookieComplianceBlockCookies, and some code between the lines 57 and 73, which could be affected by the same bug.
Since disabling advagg did solve the issue for @ron_s, I'm offering the patch in #20, which should solve the issue.
If you want to make a patch where the conflicting code is in the head tag, feel free to go ahead. We just need to find a way to test it afterwards, and make sure we move all conflicting code to the head.
I'm also wondering why $.cookie() was undefined for @guardiola86, though since disabling advagg didn't resolve that issue, I'd think that's a different bug, not related to advagg or this module.
Comment #23
svenryen commentedI've looked at this again, and it feels safer to move the lines of concern inside the behavior rather than reverting to defaults.
I'll give one example. Let's say advagg rebuilt the aggregated files in such a way that the variables are not available when the lines are executed, and there are custom values set, you would store the wrong value in the cookie. Then if at a later rebuild by advagg you didn't run into the same situation, you would have the real values available to the script and you would potentially show the banner again, since the values set in config, which are now being used, don't match the wrong value in the cookie.
I think this could happen, but I'm not sure.
If I'm not able to reproduce, I'll include the patch from #20 in the next version, and keep this issue open so that we can find a permanent solution.
Comment #24
anybodyPerhaps you could create a further MR and show the proposed changes to ensure we understand it correctly?
I'm adressing the logical issues from #15.
Comment #25
svenryen commentedThis doesn't have highest priority, but if a different patch is posted with a solution that has no side-effects, I'd definitely review it.
Comment #26
svenryen commentedHere's another idea, maybe this will work.
We could try wrapping the script in:
Shouldn't that make advagg respect that we need the Drupal variable for our command lines and place the code in such an order so that
Drupal.settings.eu_cookie_complianceis available?Comment #27
svenryen commented@Anybody and @ron_s, can you try the patch from the MR?
https://git.drupalcode.org/project/eu-cookie-compliance/-/merge_requests...
I've tested it here and moving the initialization of the variables inside a function seems to work well. Does it also resolve the issues you're seeing with advagg?
Comment #29
svenryen commentedI'm going to take the risk of merging this code. There are no changes, just variable renaming done through Find/Replace (stepped through and validated every single change). I need to work on #3275786: 7.x: Banner at top is not hiding correctly which also changes a lot in the JavaScript.
I will also test the module extensively together with another maintainer before I tag a new release.
Comment #32
sgdev commentedJust to add a cross reference, the patch committed for this issue causes problems when calling
hasAgreedin custom code. Rolling back to 7.x-1.37 fixes the problem: https://www.drupal.org/project/eu_cookie_compliance/issues/3315526