Problem/Motivation
Content Security Policy throws a deprecation warning for Entity Embed editor toolbar buttons.
Sample report:
{
"age": 104,
"body": {
"columnNumber": 8426,
"id": "XMLHttpRequestSynchronousInNonWorkerOutsideBeforeUnload",
"lineNumber": 1,
"message": "Synchronous `XMLHttpRequest` on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https:\/\/xhr.spec.whatwg.org\/.",
"sourceFile": "https:\/\/test.localhost\/modules\/composer\/entity_embed\/js\/build\/drupalentity.js?tpe0n8"
},
"type": "deprecation",
"url": "https:\/\/test.localhost\/node\/2\/edit",
"user_agent": "Mozilla\/5.0 (Macintosh; Mac OS X XXX) AppleWebKit\/XXX (KHTML, like Gecko) Chrome\/XXX Safari\/XXX"
}
It appears to be caused by this section of the built JS file:
if(l.icon.endsWith("svg")){const t=new XMLHttpRequest;t.open("GET",l.icon,!1),t.send(null),u=t.response}
Which corresponds to this logic in the source JS js/ckeditor5_plugins/drupalentity/src/ui.js line 46:
if (button.icon.endsWith('svg')) {
const XMLrequest = new XMLHttpRequest();
XMLrequest.open('GET', button.icon, false);
XMLrequest.send(null);
icon = XMLrequest.response;
}
The third argument to open() is false which makes the request synchronous.
Steps to reproduce
- Install a Drupal 11 site with the demo_umami profile.
- Require and enable these contrib modules: entity_embed, csp, and reporting (and their dependencies).
- Use Firefox as your web browser. (In my testing, it more reliably submits CSP reports using the Reporting API on local sites.)
- Log in as an administrator.
- On the Reporting settings page under Configuration -> System, edit the Default endpoint and save it without making any changes.
- In the settings for the CSP module under Configuration -> System, enable reporting by selecting Reporting Endpoint (Default) for both Enforce and Report Only. Save the configuration.
- Configure the Basic HTML toolbar: Drag the "E" button (for node embedding) into the active toolbar. Check the box to enable the "Display embedded entities" filter. Save the configuration.
- Open the Add page for any node type that has a field that uses the Basic HTML format. Leave the edit form open for a minute or two.
- Check admin/reports/reporting. Note there is a deprecation report.
Proposed resolution
Is it necessary to load the icons synchronously? What would the alternative be?
Remaining tasks
Determine whether this needs fixing and the preferred approach.
Patch and test.
User interface changes
None.
API changes
None.
Data model changes
None.
Comments
Comment #2
cboyden commentedComment #3
dave reidPinpointing the line in
ui.jsmade this quick to track down. The request doesn't need to be synchronous, and ideally there's no request at all: the button's SVG can be sent with the editor's configuration, so the icon is there when the toolbar renders, with no extra request per button and no flash of the default icon.Embed owns embed buttons and their icons, so we'd like to solve this in Embed for every module that provides embed buttons, then use it here. We'll link the Embed issue here once it's filed.
If that takes a while, a smaller fix for 8.x-1.x is to create the button with the default icon, load the SVG with
fetch(), and replace the icon when it arrives.