Improve Drupal\Core\Ajax\AddCssCommand to accept an array of CSS assets

Created on 1 February 2020, over 4 years ago
Updated 16 January 2024, 5 months ago

Problem/Motivation

Adding new CSS assets to the page by appending a rendered HTML snippet conflicts with a Content-Security-Policy that does not include style-src 'unsafe-inline' #3100151: Adding JS/CSS assets in AJAX responses requires 'unsafe-inline' Content Security Policy β†’

Drupal 9.5 / 10.0 added a new AddJsCommand for JavaScript assets, in order to resolve issues with out-of-order execution of new JS files, but the loadjs library also adds assets to the page in a way that does not conflict with CSP inline code restrictions and can be used for CSS files as well.

Drupal\Core\Ajax\AddCssCommand was introduced for IE <= 9, because they didn't support @import statements if they were in a PrependCommand.
Drupal 8 no longer uses @import statements ( #2897408: Remove IE9 support from CssCollectionRenderer and provide it in contrib instead β†’ ), and has now also removed the IE9 support code from AddCssCommand ( #3100147: Remove @import parsing from Drupal.AjaxCommands.prototype.add_css β†’ ), so AddCssCommand now effectively operates the same as PrependCommand.

Proposed resolution

Alter \Drupal\Core\Ajax\AddCssCommand to accept an array of CSS assets in addition to a rendered HTML string (retained for backwards compatibility until Drupal 11.0.0), adding the assets to the page with the loadjs library.

Remaining tasks

User interface changes

None

API changes

Passing a rendered HTML string to Drupal\Core\Ajax\AddCssCommand is deprecated.

Data model changes

None

Release notes snippet

Passing a string to AddCssCommand is now deprecated β†’ , instead an array of attributes is expected like for AddJsCommand. CSS files added with Ajax commands are now loaded with loadjs and Ajax commands wait for all CSS files to load before executing the next commands.

πŸ“Œ Task
Status

Fixed

Version

10.1 ✨

Component
AjaxΒ  β†’

Last updated about 8 hours ago

Created by

πŸ‡¨πŸ‡¦Canada gapple

Live updates comments and jobs are added and updated live.
  • JavaScript

    Affects the content, performance, or handling of Javascript.

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