WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 3 months ago

#54090 closed defect (bug) (duplicate)

block editor styles enqueued during ajax requests

Reported by: mnelson4 Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Script Loader Keywords:
Focuses: javascript, css Cc:

Description

Block editor styles (like wp-includes/css/dist/block-library/editor.css and admin/css/common.min.css) are getting enqueued during WP ajax requests when a custom block is registered with an "editor_style" whose dependencies include "wp-edit-blocks".

This is a problem because AJAX requests are usually expected to be treated like front-end requests, or at least NOT like a block editor page; and this affects plugins/themes where the themes enqueued during an AJAX request matter.

For example, my plugin Print My Blog compiles a ton of posts into a single HTML page and temporarily writes it to the server. I send one or more AJAX requests to generate this HTML page (sometimes it can contain thousands of posts, so I can't always do it in a single request). Each request writes to wp-uploads temporarily. It usually works just fine. Plus, so far it usually enqueued the same CSS and JS as a regular frontend request. But if there is another plugin that registers a custom block like this...

<?php
wp_register_style(
    'yarpp-block-style',
    plugins_url( 'style/yarpp-block-editor.css', dirname( __FILE__ ) ),
    array( 'wp-edit-blocks' ), // <-- style depends on "wp-edit-blocks"
    $version
);
register_block_type( 
    'yarpp/yarpp-block',
    array(
    ...
    'editor_style'    => 'yarpp-block-style', // <-- style is used for editing the block
    'render_callback' => array( $this, 'yarpp_block_render_callback' )
    ...
);

(that example is from YARPP, but there's a similar block in WooCommerce for example, so it seems pretty standard)

...then the AJAX request gets admin/css/common.min.css wp-includes/css/dist/block-library/editor.css enqueued so my plugin generates the HTML page with a bunch of styles intended for the admin. This means, for example, the page suddenly has a gray background (like in the admin), and different font sizes, among other styles.

It seems buggy that block editor styles and scripts are enqueued during AJAX requests. I understand this doesn't usually matter because they're usually they're not used. But in the case of my plugin, and any other plugin that might use the enqueued styles and scripts during a AJAX request, it creates a bit of a problem.

I would expect block editor styles and scripts would only be enqueued on admin pages that actually use the block editor.

Here's a short video demonstrating the issue: https://drive.google.com/file/d/1GbjRFlpkuVz7kZWyGiVw0bLZ3Bt1rCsv/view

Does that make sense?

Change History (1)

#1 @sabernhardt
3 months ago

  • Component changed from Bootstrap/Load to Script Loader
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #54089.

Note: See TracTickets for help on using tickets.