JS Aggregation breaks in 10.1 when disabling other javascripts

Created on 5 January 2024, 6 months ago
Updated 25 March 2024, 3 months ago

Problem/Motivation

On a Drupal 10.1 site, when this module is configured to disable certain JavaScript and JS aggregation enabled, certain admin page functionality such as admin toolbar, operations etc. will be broken.

Steps to reproduce

Enable and configure the Google Tag β†’ module, adding an a Google Tag ID like "UA-12345-12" to the site (/admin/config/services/google-tag).

On the EU Cookie Compliance settings page:

Add the following to the "Disable JavaScripts" section:

category:modules/contrib/google_tag/js/gtm.js||google_tag/gtm
category:modules/contrib/google_tag/js/gtag.js||google_tag/gtag
modules/contrib/google_tag/js/gtag.ajax.js||google_tag/gtag.ajax

Ensure the "Advanced" > "Exclude paths" textarea is empty, or in particular these paths are no longer present:

/admin
/admin/*
/node/add*
/node/*/*

NB: This step might not be necessary, but it's an easy way to trigger the bug consistently.

Now attempt to view a page in the admin, e.g. the content listing page (/admin/content) or a node with multiple revisions' revision page (/node/%nid/revisions), the page will appear broken.

The following exception is shown in the watchdog logs when Drupal tries to load the aggregated JavaScript:

Symfony\Component\HttpKernel\Exception\BadRequestHttpException: Invalid filename. in Drupal\system\Controller\AssetControllerBase->getGroup() (line 224 of /app/application/web/core/modules/system/src/Controller/AssetControllerBase.php).

It doesn't seem to be theme specific, but it affects Claro and Seven admin themes.

Proposed resolution

Ensure we manipulate the final JS only once the page has been fully built and all assets have been attached, but before aggregation is ran. hook_js_alter() is triggered after the aggregation is generated, so its implement is mostly redundant.

Remaining tasks

User interface changes

N/A

API changes

Introduced a new response event subscriber and a new _eu_cookie_compliance_process_disabled_javascripts() function that acts on response arrays and attachment responses.

Data model changes

N/A

Release notes snippet

TBD

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States markusa

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

Merge Requests

Comments & Activities

  • Issue created by @markusa
  • πŸ‡ΊπŸ‡ΈUnited States markusa

    When I "View Source" of admin vs. front end pages ..
    On admin pages:
    I see an aggregated js file, then 2 gtm js scripts NOT aggregated and then a couple more aggregated JS files
    On the front end, I don't see the un-aggregated gtm scripts in the source.

    So whatever its doing, when the gtm scripts are NOT in the aggregated scripts, the condition occurs.

    If I configure the Google Tag module to exclude /admin/* pages, the condition disappears.

    What bothers is why on the admin theme only, is there some 3rd component interacting?

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    This only appears to work if you Exclude from Admin Pages in the cookie consent settings.

  • πŸ‡¬πŸ‡§United Kingdom Finn Lewis

    We were seeing exactly the same problem.

    Enabling: "Exclude admin pages." and "Don't show the banner for site administrators (including UID 1)." seems to resolve it for us.

  • πŸ‡ΊπŸ‡ΈUnited States kevinquillen

    Yes, this is still a problem. This should be disabled entirely for administrative paths by default.

  • I ran into this issue myself with the google_tag module.

    It seems to be because google_tag's hook_page_attachments() implementation is ran after eu_cookie_compliance's.

    This means that the disabled_javascripts check doesn't necessarily work as expected.

    I notice that we also implement eu_cookie_compliance_page_attachments_alter() but don't use that to manipulate the attached library definitions. Is there any particular reason we don't handle the disabling of all assets in the alter hook?

    It might also be worth implementing a hook_module_implements_alter() to ensure that the module acts on the attachments last.

    Thoughts are more than welcome!

  • Pipeline finished with Success
    3 months ago
    Total: 48s
    #126134
  • Pipeline finished with Success
    3 months ago
    Total: 47s
    #128497
  • Status changed to Needs review 3 months ago
  • Updated the logic so that all the duplicated logic is handled by a single function, as well as removed all the disable logic from hook_page_attachments() to hook_page_attachments_alter() so that it can detect dynamic libraries such as the google_tag one that might be added.

    Provided a MR which implements the fix in an event subscriber rather than through hooks.

  • Pipeline finished with Success
    3 months ago
    Total: 46s
    #128517
Production build 0.69.0 2024