claro.jquery.ui css assets may be added the page multiple times

Created on 31 July 2023, over 1 year ago
Updated 28 August 2024, 4 months ago

Problem/Motivation

This regression was introduced by ๐Ÿ“Œ Improve Drupal\Core\Ajax\AddCssCommand to accept an array of CSS assets Fixed

TLDR; In both Drupal 10.0 and 10.1, duplicate CSS files can be added by add_css commands. However, in 10.1, due to the above fix - CSS files added via add_css are no longer prepended, meaning the duplicate CSS files can be added after existing stylesheets, breaking weighting and overriding styles in unexpected ways.

Background
Our client reported a strange, very specific styling issue on our 10.1 upgraded environment. Entity Embed modals inside paragraphs when on a Paragraph Library add form were showing broken styling for the buttons. These styles were coming from /core/assets/vendor/jquery.ui/themes/base/button.css and /core/themes/claro/css/components/jquery.ui/theme.css (styles for .ui-button and .ui-widget).

Strangely, this did not happen on other pages (e.g node/add) with paragraphs and entity embed modals.

I managed to reproduce this issue on a "vanilla" (in quotes because it does require some contrib to trigger the styling issue, but there's probably an easier way to reproduce it)

Steps to reproduce

  1. Install standard profile on 10.1.x
  2. Install paragraphs module
  3. Turn off CSS aggregation
  4. Create a paragraph type with a Text (formatted, long) field (it seems like CKEditor fields are needed to trigger this)
  5. Create a new content type, delete the body field, and add an ERR field referencing the previously created paragraph type
  6. Edit the form display of this content type and set the Default paragraph type to None
  7. Go to the add form of the above content type
  8. Open your inspector and search for /core/themes/claro/css/components/jquery.ui/theme.css
  9. Notice only 1 exists
  10. Click the "Add" button on your paragraph field
  11. Search for the CSS file again
  12. Notice 2 exist.

It seems to be triggered by no CKEditor being on the page, and the AJAX request adding one. This happens with both CKE5 and CKE4 (using the contrib module). This is because it only triggers when there is no existing CKEditor on the page (i.e why we remove the body field), AND the paragraph type contains a CKEditor field.

Proposed resolution

Absolutely no idea.

๐Ÿ› Bug report
Status

Fixed

Version

10.1 โœจ

Component
Claroย  โ†’

Last updated about 18 hours ago

Created by

๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

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

Comments & Activities

  • Issue created by @acbramley
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada gapple

    ๐Ÿค” is something preventing AjaxResponseAttachmentsProcessor::buildAttachmentsCommands() from filtering out assets from libraries that were already loaded on the page?

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    @gapple yeah I was debugging a lot through there, it's hard to figure out which dependency (of a dependency of a dependency) is adding the libraries in the first place but I was trying to follow the theme.css file which comes from the claro.jquery.ui library.

    That library is added via libraries-extend for core/drupal.autocomplete, core/drupal.dialog and core/drupal.tabbingmanager

    core/drupal.autocomplete is in drupalSettings.ajaxPageState on page load, and it's in the already attached libraries in that processor.

    I have no idea what library is adding the CSS file again on the AJAX request, but it smells like something related to libraries-extend.

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom nlisgo

    A fix and a test to avoid future regression.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    Back to debugging this as itโ€™s causing a lot of issues with our 10.1 upgrade.

    Looking into AssetResolver::getCssAssets I can see 2 libraries adding core/themes/claro/css/components/jquery.ui/theme.css (btw itโ€™s not specific to this file this is just the one wrecking the buttons) on initial page load:

    • core/drupal.dialog
    • core/drupal.tabbingmanager

    Only 1 is added because the $css array is keyed by the asset url.

    On the AJAX request, core/drupal.autocomplete is then added which also adds that CSS file.

    getLibrariesToLoad only diffs the libraries, it doesnโ€™t care about what CSS files have been added.

    IMO this is a pretty major regression for 10.1. Again, this already happened in prior versions but because of the changes in ๐Ÿ“Œ Improve Drupal\Core\Ajax\AddCssCommand to accept an array of CSS assets Fixed these files are no longer prepended meaning they can override existing styles.

    Other things that are affected by this (for us at least):

    1. Using layout_builder_modal, the close button on the modal dialog has broken hover styles.
    2. Also getting some whacky overlay issues with layout_builder_modal + a block with a WYSIWYG field in it when opening the Linkit dialog. The overlay doesn't disappear on save, meaning the page is not interactable.
  • Assigned to gapple
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada gapple

    I think I see - Claro's use of libraries-extend is merging the assets from claro/claro.jquery.ui to multiple libraries rather than adding it as a dependency of those libraries. (Looks like that happened here: #3113400-50: Deprecate more jQuery UI library definitions โ†’ ).

    While the change to add_css's behaviour seems to be exposing this issue, I don't think it's particularly the fault of add_css - and I can see different problems if the behaviour was changed to prepend css assets to the head element (e.g. Theme CSS being inserted via AJAX response before module CSS already on the page, so that selectors with equal specificity aren't applied in the right order).

  • last update over 1 year ago
    30,056 pass
  • @gapple opened merge request.
  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada gapple

    I'm hoping the MR will address this particular issue of duplicate assets by allowing proper library resolution.

    I think how/where assets from AJAX responses are added in relation to existing assets is a bigger, messier issue, that's going to have issues in certain cases with both prepending or appending new assets to the document head.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada gapple
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    Amazing @gapple! That has fixed the first issue for me. I think the layout_builder_modal thing may be similar but is probably a bit more nuanced (the module renders the modal in the admin theme, the CSS file that's breaking the styles is core/assets/vendor/jquery.ui/themes/base/theme.css so not Claro)

    I don't think we need to worry about prepended/appending, we need to stop duplicate files being added altogether. Rather than diffing libraries, it needs to diff the actual files attached (which, yes, will be very messy).

  • Status changed to RTBC over 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada gapple

    Great! I think that's enough for RTBC - I don't think a test would be necessary or helpful for this specific bug.

    ----

    duplicate files would only be an issue if a file is added to multiple libraries, which I don't think should be happening if the library system is being used correctly - common files should be placed in a common dependency.

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia acbramley

    Agreed this would be great to get in.

    I just tracked down another issue with duplicate CSS files, this time assets/vendor/jquery.ui/themes/base/core.css

    which I don't think should be happening if the library system is being used correctly

    That's the issue, it's not. Look at drupal.autocomplete and drupal.dialog - lots of dupes there. Which is exactly what caused this 2nd issue (core.css duplicated and overriding z-index styles).

    Slack monologue https://drupal.slack.com/archives/C1BMUQ9U6/p1692681337738279

    • catch โ†’ committed c9c7819e on 10.1.x
      Issue #3378341 by gapple, acbramley: claro.jquery.ui css assets may be...
    • catch โ†’ committed ce21f642 on 11.x
      Issue #3378341 by gapple, acbramley: claro.jquery.ui css assets may be...
  • Status changed to Fixed over 1 year ago
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    Agreed a test wouldn't be useful here, we'd just be testing the Claro library definitions match a certain pattern. Looks like we should have done some kind of library.name.deprecated trick with the jquery_ui deprecations instead of my hack to directly include the files :/

    Committed/pushed to 11.x and cherry-picked to 10.1.x, thanks!

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed about 1 year ago
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave
  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia mstrelan

    I reopened ๐Ÿ› Background colour of UI widgets get overridden on Ajax load. Active because there is another underlying issue that this hasn't solved. We don't get the css loaded multiple times, but we do get inconsistent order of css files loaded, and that results in the same issue with missing button backgrounds.

Production build 0.71.5 2024